Simple setup example
Our nginx vhost:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
upstream ango { server unix:/tmp/ango.sock; } # the nginx server instance server { listen 80; server_name go.dev *.go.dev; error_log /var/log/nginx/go_error.log; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_http_version 1.1; # for keep-alive proxy_pass http://ango/; proxy_redirect off; } } |
The Go app
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package main import ( "fmt" "github.com/gorilla/mux" "net" "net/http" ) var ( router *mux.Router = mux.NewRouter() ) func main() { // This is the route and an anonymous or first class function to return <h1>Hello World</h1> router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "<h1>Hello World</h1>") }) // This here is an example of Chuck Han https://groups.google.com/forum/#!topic/golang-nuts/41TPj4PWBI8 // Listen on unix socket /tmp/ango.sock l, err := net.Listen("unix", "/tmp/ango.sock") if err != nil { fmt.Printf("%s\n", err) } else { err := http.Serve(l, router) if err != nil { panic(err) } } } |