2020-10-13 21:36:50 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-18 15:35:47 +02:00
|
|
|
"context"
|
|
|
|
"errors"
|
2020-10-13 21:36:50 +02:00
|
|
|
"fmt"
|
2020-10-18 15:35:47 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/hamburghammer/gsave/controller"
|
2020-10-30 10:33:15 +01:00
|
|
|
"github.com/hamburghammer/gsave/controller/middleware"
|
2020-10-18 15:35:47 +02:00
|
|
|
"github.com/hamburghammer/gsave/db"
|
2020-11-11 11:01:13 +01:00
|
|
|
"github.com/jessevdk/go-flags"
|
2020-10-18 18:26:59 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-13 21:36:50 +02:00
|
|
|
)
|
|
|
|
|
2020-10-18 18:26:59 +02:00
|
|
|
var (
|
|
|
|
servePort int
|
|
|
|
logPackage = log.WithField("Package", "main")
|
|
|
|
)
|
2020-10-18 16:40:34 +02:00
|
|
|
|
2020-11-11 11:01:13 +01:00
|
|
|
type arguments struct {
|
|
|
|
Port int `short:"p" long:"port" default:"8080" description:"The port for the HTTP server." env:"GSAVE_PORT"`
|
|
|
|
Token string `short:"t" long:"token" required:"yes" description:"The token for the authentication through HTTP." env:"GSAVE_TOKEN"`
|
|
|
|
Verbose bool `short:"v" long:"verbose" description:"Enable trace logging level output."`
|
|
|
|
Quiet bool `short:"q" long:"quiet" description:"Disable standard logging output and only prints errors."`
|
|
|
|
JSONLogging bool `long:"json" description:"Set the logging format to json."`
|
|
|
|
}
|
|
|
|
|
2020-10-18 16:40:34 +02:00
|
|
|
func init() {
|
2020-11-11 11:01:13 +01:00
|
|
|
args := arguments{}
|
|
|
|
_, err := flags.Parse(&args)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*flags.Error); ok {
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logPackage.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
servePort = args.Port
|
2020-10-18 18:26:59 +02:00
|
|
|
|
|
|
|
log.SetFormatter(&log.TextFormatter{
|
|
|
|
FullTimestamp: true,
|
|
|
|
})
|
|
|
|
|
2020-11-11 11:01:13 +01:00
|
|
|
if args.Verbose {
|
2020-10-30 10:30:20 +01:00
|
|
|
log.SetLevel(log.TraceLevel)
|
2020-10-18 18:26:59 +02:00
|
|
|
}
|
2020-11-11 11:01:13 +01:00
|
|
|
if args.Quiet {
|
2020-10-18 18:26:59 +02:00
|
|
|
log.SetLevel(log.ErrorLevel)
|
|
|
|
}
|
2020-11-11 11:01:13 +01:00
|
|
|
if args.JSONLogging {
|
2020-10-18 18:26:59 +02:00
|
|
|
log.SetFormatter(&log.JSONFormatter{})
|
|
|
|
}
|
2020-10-18 16:40:34 +02:00
|
|
|
}
|
2020-10-18 15:35:47 +02:00
|
|
|
|
2020-10-13 21:36:50 +02:00
|
|
|
func main() {
|
2020-10-18 18:26:59 +02:00
|
|
|
logPackage.Info("Initializing the DB...")
|
2020-10-18 15:35:47 +02:00
|
|
|
stats := []db.Stats{
|
2020-11-06 08:57:00 +01:00
|
|
|
{Hostname: "foo", CPU: 0, Disk: db.Memory{Total: 10, Used: 5}, Mem: db.Memory{Total: 20, Used: 10}, Processes: []db.Process{{Name: "foo", Pid: 1, CPU: 0.5}}},
|
2020-10-18 15:35:47 +02:00
|
|
|
{Hostname: "foo", CPU: 1},
|
|
|
|
{Hostname: "bar", CPU: 0},
|
|
|
|
}
|
|
|
|
hostDB, err := initDB(stats)
|
|
|
|
if err != nil {
|
2020-10-18 18:26:59 +02:00
|
|
|
logPackage.Fatal(err)
|
2020-10-18 15:35:47 +02:00
|
|
|
}
|
|
|
|
|
2020-10-18 18:26:59 +02:00
|
|
|
logPackage.Info("Initializing the routes...")
|
2020-10-18 15:35:47 +02:00
|
|
|
controllers := []controller.Router{
|
|
|
|
controller.NewHostsRouter(hostDB),
|
|
|
|
}
|
|
|
|
router := initRouter(hostDB, controllers)
|
|
|
|
|
2020-10-30 12:15:31 +01:00
|
|
|
auth := middleware.NewAuthMiddleware([]string{"foo"})
|
2020-10-30 10:33:15 +01:00
|
|
|
// Add default middlewares
|
|
|
|
router.Use(middleware.RequestTimeLoggingHandler)
|
|
|
|
router.Use(middleware.PanicRecoverHandler)
|
2020-10-30 12:15:31 +01:00
|
|
|
router.Use(auth.AuthHandler)
|
2020-10-30 10:33:15 +01:00
|
|
|
|
2020-10-18 18:26:59 +02:00
|
|
|
logPackage.Info("Starting the HTTP server...")
|
2020-10-18 15:35:47 +02:00
|
|
|
server := &http.Server{
|
|
|
|
Handler: router,
|
2020-10-18 16:40:34 +02:00
|
|
|
Addr: fmt.Sprintf(":%d", servePort),
|
2020-10-18 15:35:47 +02:00
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
2020-10-27 12:07:30 +01:00
|
|
|
IdleTimeout: 120 * time.Second,
|
2020-10-18 15:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
go startHTTPServer(server, &wg)
|
|
|
|
go listenToStopHTTPServer(server, &wg)
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDB(stats []db.Stats) (db.HostDB, error) {
|
|
|
|
hostDB := db.NewInMemoryDB()
|
|
|
|
for _, stat := range stats {
|
|
|
|
if err := hostDB.InsertStats(stat.Hostname, stat); err != nil {
|
|
|
|
return hostDB, fmt.Errorf("Init db throwed an error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostDB, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initRouter(hostDB db.HostDB, controllers []controller.Router) *mux.Router {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
for _, controller := range controllers {
|
|
|
|
subrouter := router.PathPrefix(controller.GetPrefix()).Name(controller.GetRouteName()).Subrouter()
|
|
|
|
controller.Register(subrouter)
|
|
|
|
}
|
|
|
|
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
func startHTTPServer(server *http.Server, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
2020-10-18 18:26:59 +02:00
|
|
|
logPackage.Infof("The HTTP server is running: http://localhost:%d/hosts\n", servePort)
|
2020-10-18 15:35:47 +02:00
|
|
|
if err := server.ListenAndServe(); err != nil {
|
|
|
|
if errors.Is(err, http.ErrServerClosed) {
|
2020-10-18 18:26:59 +02:00
|
|
|
logPackage.Info("Shutting down the server...")
|
2020-10-18 15:35:47 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-18 18:26:59 +02:00
|
|
|
logPackage.Fatalf("An unexpected error happend while running the HTTP server: %v\n", err)
|
2020-10-18 15:35:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func listenToStopHTTPServer(server *http.Server, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, os.Interrupt, os.Kill)
|
|
|
|
|
|
|
|
<-stop
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := server.Shutdown(ctx); err != nil {
|
2020-10-18 18:26:59 +02:00
|
|
|
logPackage.Errorf("An error happened on the shutdown of the server: %v", err)
|
2020-10-18 15:35:47 +02:00
|
|
|
}
|
2020-10-13 21:36:50 +02:00
|
|
|
}
|