starting webserver
This commit is contained in:
44
cmd/go-temper/config.go
Normal file
44
cmd/go-temper/config.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/logutils"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
type configStructure struct {
|
||||
// time configuration
|
||||
TimeFormat string `json:"time_format"`
|
||||
TimeZone *time.Location
|
||||
TimeZoneUTC *time.Location
|
||||
|
||||
// logging
|
||||
Log *logutils.LevelFilter
|
||||
|
||||
// webserver
|
||||
WebSrvPort int `json:"webserver_port"`
|
||||
WebSrvIP string `json:"webserver_ip"`
|
||||
WebSrvReadTimeout int `json:"webserver_read_timeout"`
|
||||
WebSrvWriteTimeout int `json:"webserver_write_timeout"`
|
||||
WebSrvIdleTimeout int `json:"webserver_idle_timeout"`
|
||||
|
||||
// prometheus
|
||||
Prometheus configPrometheus
|
||||
}
|
||||
|
||||
type configPrometheus struct {
|
||||
Temperature prometheus.Gauge
|
||||
}
|
||||
|
||||
var config = configStructure{
|
||||
TimeFormat: "2006-01-02 15:04:05",
|
||||
Log: &logutils.LevelFilter{
|
||||
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARNING", "ERROR"},
|
||||
Writer: os.Stderr,
|
||||
},
|
||||
WebSrvReadTimeout: 5,
|
||||
WebSrvWriteTimeout: 2,
|
||||
WebSrvIdleTimeout: 2,
|
||||
}
|
46
cmd/go-temper/httpServer.go
Normal file
46
cmd/go-temper/httpServer.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"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("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
httpAccessLog(r)
|
||||
w.Write([]byte("web-root"))
|
||||
})
|
||||
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)
|
||||
}
|
||||
}
|
@@ -8,6 +8,8 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
go httpServer("0.0.0.0", 8080)
|
||||
|
||||
for {
|
||||
temp, err := temper.GetTemperature()
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user