drive-health/main.go

64 lines
1.3 KiB
Go
Raw Permalink 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-22 00:36:05 +02:00
"github.com/JustKato/drive-health/lib/config"
"github.com/JustKato/drive-health/lib/svc"
"github.com/JustKato/drive-health/lib/web"
"github.com/joho/godotenv"
2024-01-20 00:21:59 +02:00
)
func main() {
// Load .env file if it exists
if err := godotenv.Load(); err != nil {
log.Println("[🟨] No .env file found")
}
// Init the database
svc.InitDB()
cfg := config.GetConfiguration()
2024-01-20 00:21:59 +02:00
router := web.SetupRouter()
srv := &http.Server{
Addr: cfg.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("[🛑] listening failed: %s\n", err)
}
}()
// Run the hardware service
svc.RunLoggerService()
// Run the cleanup service
svc.RunCleanupService()
// 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
}