Adds power exporter
This commit is contained in:
72
cmd/export/httpServer.go
Normal file
72
cmd/export/httpServer.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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")
|
||||
}
|
||||
|
||||
func (config *config) httpServer() {
|
||||
path := http.NewServeMux()
|
||||
|
||||
connection := &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%d", config.WebSrvIP, config.WebSrvPort),
|
||||
Handler: path,
|
||||
ReadTimeout: time.Duration(config.WebSrvReadTimeout) * time.Second,
|
||||
WriteTimeout: time.Duration(config.WebSrvWriteTimeout) * time.Second,
|
||||
IdleTimeout: time.Duration(config.WebSrvIdleTimeout) * time.Second,
|
||||
}
|
||||
|
||||
// metrics
|
||||
path.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
|
||||
httpAccessLog(r)
|
||||
crossSiteOrigin(w)
|
||||
promhttp.Handler().ServeHTTP(w, r)
|
||||
})
|
||||
// healthcheck
|
||||
path.HandleFunc("/healthcheck", webHealthCheck)
|
||||
// root
|
||||
path.HandleFunc("/", webRoot)
|
||||
|
||||
if err := connection.ListenAndServe(); err != nil {
|
||||
log.Fatalf("[ERROR] %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func webRoot(w http.ResponseWriter, r *http.Request) {
|
||||
httpAccessLog(r)
|
||||
crossSiteOrigin(w)
|
||||
|
||||
if strings.ToLower(r.Method) == "get" {
|
||||
tmpltWebRoot(w)
|
||||
} else {
|
||||
log.Printf("[DEBUG] Request to '/' was made using the wrong method: expected %s, got %s\n", "GET", strings.ToUpper(r.Method))
|
||||
tmpltError(w, http.StatusBadRequest, "Invalid http method.")
|
||||
}
|
||||
}
|
||||
|
||||
func webHealthCheck(w http.ResponseWriter, r *http.Request) {
|
||||
httpAccessLog(r)
|
||||
crossSiteOrigin(w)
|
||||
|
||||
if strings.ToLower(r.Method) == "get" {
|
||||
tmpltHealthCheck(w)
|
||||
} else {
|
||||
log.Printf("[DEBUG] Request to '/healthcheck' was made using the wrong method: expected %s, got %s\n", "GET", strings.ToUpper(r.Method))
|
||||
tmpltError(w, http.StatusBadRequest, "Invalid http method.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user