In the go program, I want to run two web servers at the same time,
obviously they will serve two different ports (and, if necessary, IP addresses),
the problem is calling http.handle , when I try to register a handler for '/' for the second server, it panics and says that there is already a handler associated with '/',
I think I need to create a multiplexer in addition to DefaultServeMux , and I tried to do it with gorillaMux , but could not figure out
Is there something fundamentally wrong when starting two web servers in the same program / process.
To make it more understandable, one of the two web servers is used as a regular web server, I need the second to act as an RPC server for exchanging information between instances of the program running on different nodes of the cluster,
EDIT: to make it more understandable, this is not real code, but it represents
myMux := http.NewServeMux() myMux.HandleFunc("/heartbeat", heartBeatHandler) http.Handle("/", myMux) server := &http.Server{ Addr: ":3400", Handler: myMux, } go server.ListenAndServe() gorillaMux := mux.NewRouter() gorillaMux.HandleFunc("/", indexHandler) gorillaMux.HandleFunc("/book", bookHandler) http.Handle("/", gorillaMux) server := &http.Server{ Addr: ":1234", Handler: gorillaMux, } log.Fatal(server.ListenAndServe())
Ali
source share