hello every body .
i have three questions .
How can i run multi golang web servers on one physical server or vps ?(run one on 192.168.100.150 and the other one on 192.168.100.151)
how set domains on them?
Can we set and run bouth of them on port:80?
geosoft1
(George Calianu)
November 22, 2018, 9:07pm
2
Well, i didn’t tried but ListenAndServe can handle the complete address (meaning ip or domain and port). I guess you can use this twice (one in a go routine) or in separate applications on different ip’s but the same port. Also you need to have two IPs on the interface.
1 Like
Thank’s you so mach.
please write the codes here too.
i am just a beginner .
NobbZ
(Norbert Melzer)
November 22, 2018, 9:36pm
4
Please do not put a server in the public if you are unable to answer the questions you asked yourself or you are not able to code the basic after you have been linked to the necessary parts of the documentation.
You’ll end up in a botnet or get otherwise taken over…
1 Like
geosoft1
(George Calianu)
November 23, 2018, 6:48am
6
A simple server listening on two IPs and the same port can look like this:
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world!"))
})
go func() {
http.ListenAndServe("192.168.100.150:8080", nil)
}()
http.ListenAndServe("192.168.100.151:8080", nil)
}
In this simple case i used the same handler for both servers. If you want separate handle funcs for each server i suggest using gorilla mux .
1 Like
Hi
You don’t need gorilla mux (not for this at least). Because standard http package can create multiple multiplexers
package main
import (
"io"
"net/http"
)
func index1(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello from server 1")
}
func index2(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello from server 2")
}
func main() {
mux1 := http.NewServeMux()
mux1.HandleFunc("/", index1)
mux2 := http.NewServeMux()
mux2.HandleFunc("/", index2)
go http.ListenAndServe("localhost:8001", mux1)
http.ListenAndServe("localhost:8002", mux2)
}
5 Likes
system
(system)
Closed
February 21, 2019, 10:38am
9
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.