2024-01-20 00:21:59 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-20 01:27:10 +02:00
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2024-01-20 00:21:59 +02:00
|
|
|
|
2024-01-20 01:27:10 +02:00
|
|
|
"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() {
|
2024-01-20 01:27:10 +02:00
|
|
|
// Load existing snapshots from file
|
|
|
|
svc.UpdateHardwareSnapshotsFromFile()
|
2024-01-20 00:21:59 +02:00
|
|
|
|
2024-01-20 01:27:10 +02:00
|
|
|
router := web.SetupRouter()
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: ":8080",
|
|
|
|
Handler: router,
|
2024-01-20 00:21:59 +02:00
|
|
|
}
|
|
|
|
|
2024-01-20 01:27:10 +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
|
|
|
}
|
|
|
|
|
2024-01-20 01:27:10 +02:00
|
|
|
log.Println("Server exiting")
|
2024-01-20 00:21:59 +02:00
|
|
|
}
|