server: set SERVER header on all requests (even SIO ones), display version on startup

This commit is contained in:
mappu 2017-02-06 16:35:25 +13:00
parent 5da61d5922
commit 6999069fd7

31
main.go
View File

@ -12,6 +12,10 @@ import (
"github.com/googollee/go-socket.io" "github.com/googollee/go-socket.io"
) )
const (
VERSION = `nmdc-webfrontend/1.1.2`
)
type App struct { type App struct {
cfg *Config cfg *Config
} }
@ -224,6 +228,9 @@ func (this *App) StaticRequestHandler(w http.ResponseWriter, r *http.Request) {
func (this *App) RunServer() { func (this *App) RunServer() {
// Inner mux {{
innerMux := http.NewServeMux()
// Socket.io handler // Socket.io handler
server, err := socketio.NewServer(nil) server, err := socketio.NewServer(nil)
if err != nil { if err != nil {
@ -233,27 +240,39 @@ func (this *App) RunServer() {
server.On("error", func(so socketio.Socket, err error) { server.On("error", func(so socketio.Socket, err error) {
log.Println("error:", err) log.Println("error:", err)
}) })
http.Handle("/socket.io/", server) innerMux.Handle("/socket.io/", server)
// Custom favicon handler
if this.cfg.Web.CustomFavicon { 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 { if this.cfg.Web.ExternalWebroot {
http.Handle("/", http.FileServer(http.Dir("client"))) innerMux.Handle("/", http.FileServer(http.Dir("client")))
} else { } 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 // Listen and serve
bindAddr := fmt.Sprintf("%s:%d", this.cfg.Web.BindTo, this.cfg.Web.Port) bindAddr := fmt.Sprintf("%s:%d", this.cfg.Web.BindTo, this.cfg.Web.Port)
log.Printf("Serving at %s...", bindAddr) log.Printf("Serving at %s...", bindAddr)
log.Fatal(http.ListenAndServe(bindAddr, nil)) log.Fatal(http.ListenAndServe(bindAddr, outerMux))
} }
func main() { func main() {
log.Println(VERSION)
a, err := NewApp("nmdc-webfrontend.conf") a, err := NewApp("nmdc-webfrontend.conf")
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())