package main import ( "bytes" "log" "strconv" "time" "html/template" "net/http" "github.com/prometheus/client_golang/prometheus/promhttp" ) func httpAccessLog(req *http.Request) { log.Printf("[TRACE] %s - %s - %s\n", req.Method, req.RemoteAddr, req.RequestURI) } func crossSiteOrigin(w http.ResponseWriter) { w.Header().Add("Access-Control-Allow-Origin", "*") w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") w.Header().Add("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization, X-API-Token") } func httpServer(host string, port int) { path := http.NewServeMux() connection := &http.Server{ Addr: host + ":" + strconv.FormatInt(int64(port), 10), Handler: path, ReadTimeout: time.Duration(config.WebSrvReadTimeout) * time.Second, WriteTimeout: time.Duration(config.WebSrvWriteTimeout) * time.Second, IdleTimeout: time.Duration(config.WebSrvIdleTimeout) * time.Second, } path.HandleFunc("/", webRoot) path.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) { httpAccessLog(r) crossSiteOrigin(w) promhttp.Handler().ServeHTTP(w, r) }) if err := connection.ListenAndServe(); err != nil { log.Fatalf("[ERROR] %s\n", err) } } func webRoot(w http.ResponseWriter, r *http.Request) { outputData := struct { Title string ErrorCode int }{ Title: "forbidden", ErrorCode: http.StatusForbidden, } tmplt, err := template.New("webRoot").Parse(webErrorTmplt()) if err != nil { log.Fatalf("[FATAL] Unable to parse webRoot template: %v\n", err) } var output bytes.Buffer if err := tmplt.Execute(&output, outputData); err != nil { log.Fatalf("[FATAL] Unable to process webRoot template: %v\n", err) } w.Write(output.Bytes()) }