drive-health/main.go

55 lines
1.1 KiB
Go
Raw Normal View History

2024-01-20 00:21:59 +02:00
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
2024-01-20 00:21:59 +02:00
2024-01-20 02:09:10 +02:00
"tea.chunkbyte.com/kato/drive-health/lib/config"
"tea.chunkbyte.com/kato/drive-health/lib/svc"
"tea.chunkbyte.com/kato/drive-health/lib/web"
2024-01-20 00:21:59 +02:00
)
func main() {
// Load existing snapshots from file
svc.UpdateHardwareSnapshotsFromFile()
2024-01-20 00:21:59 +02:00
router := web.SetupRouter()
srv := &http.Server{
2024-01-20 02:09:10 +02:00
Addr: config.GetConfiguration().Listen,
Handler: router,
2024-01-20 00:21:59 +02:00
}
// Run the server in a goroutine
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Run the hardware service
svc.RunService()
// Setting up signal capturing
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
// Block until a signal is received
<-quit
log.Println("Shutting down server...")
// Graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown:", err)
2024-01-20 00:21:59 +02:00
}
log.Println("Server exiting")
2024-01-20 00:21:59 +02:00
}