From 6999069fd77a19f28b1dc2a1dfb565529feedee4 Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 6 Feb 2017 16:35:25 +1300 Subject: [PATCH] server: set SERVER header on all requests (even SIO ones), display version on startup --- main.go | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index a60b4b1..09db542 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,10 @@ import ( "github.com/googollee/go-socket.io" ) +const ( + VERSION = `nmdc-webfrontend/1.1.2` +) + type App struct { cfg *Config } @@ -224,6 +228,9 @@ func (this *App) StaticRequestHandler(w http.ResponseWriter, r *http.Request) { func (this *App) RunServer() { + // Inner mux {{ + innerMux := http.NewServeMux() + // Socket.io handler server, err := socketio.NewServer(nil) if err != nil { @@ -233,27 +240,39 @@ func (this *App) RunServer() { server.On("error", func(so socketio.Socket, err error) { log.Println("error:", err) }) - http.Handle("/socket.io/", server) + innerMux.Handle("/socket.io/", server) + // Custom favicon handler if this.cfg.Web.CustomFavicon { - http.HandleFunc("/favicon.ico", this.customFaviconHandler) + innerMux.HandleFunc("/favicon.ico", this.customFaviconHandler) } - // Other files: asset handler + // Asset handler if this.cfg.Web.ExternalWebroot { - http.Handle("/", http.FileServer(http.Dir("client"))) + innerMux.Handle("/", http.FileServer(http.Dir("client"))) } else { - http.HandleFunc("/", this.StaticRequestHandler) + innerMux.HandleFunc("/", this.StaticRequestHandler) } + // }} + + // Wrapper mux {{ + outerMux := http.NewServeMux() + outerMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Server", VERSION) + innerMux.ServeHTTP(w, r) + }) + // }} // Listen and serve bindAddr := fmt.Sprintf("%s:%d", this.cfg.Web.BindTo, this.cfg.Web.Port) log.Printf("Serving at %s...", bindAddr) - log.Fatal(http.ListenAndServe(bindAddr, nil)) + log.Fatal(http.ListenAndServe(bindAddr, outerMux)) } func main() { + log.Println(VERSION) + a, err := NewApp("nmdc-webfrontend.conf") if err != nil { log.Fatal(err.Error())