mirror of
https://github.com/nikdoof/hg612-exporter.git
synced 2025-12-14 10:42:16 +00:00
init
This commit is contained in:
46
internal/rest/server.go
Normal file
46
internal/rest/server.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
httpSrv *http.Server
|
||||
}
|
||||
|
||||
func New(bind string) *Server {
|
||||
var s Server
|
||||
|
||||
r := http.NewServeMux()
|
||||
r.Handle("/prometheus", promhttp.Handler())
|
||||
|
||||
server := http.Server{
|
||||
Addr: bind,
|
||||
Handler: handlers.LoggingHandler(os.Stdout, r),
|
||||
}
|
||||
|
||||
s.httpSrv = &server
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s Server) Start() {
|
||||
go func() {
|
||||
err := s.httpSrv.ListenAndServe()
|
||||
if err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s Server) Stop() {
|
||||
err := s.httpSrv.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
logrus.Warn(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user