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-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"
|
2024-01-21 21:42:58 +02:00
|
|
|
"github.com/joho/godotenv"
|
2024-01-20 00:21:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-01-21 21:42:58 +02:00
|
|
|
// Load .env file if it exists
|
|
|
|
if err := godotenv.Load(); err != nil {
|
|
|
|
log.Println("[🟨] No .env file found")
|
|
|
|
}
|
|
|
|
|
2024-01-20 18:35:50 +02:00
|
|
|
// Init the database
|
|
|
|
svc.InitDB()
|
2024-01-21 18:40:40 +02:00
|
|
|
cfg := config.GetConfiguration()
|
2024-01-20 00:21:59 +02:00
|
|
|
|
2024-01-20 01:27:10 +02:00
|
|
|
router := web.SetupRouter()
|
|
|
|
|
|
|
|
srv := &http.Server{
|
2024-01-21 18:40:40 +02:00
|
|
|
Addr: cfg.Listen,
|
2024-01-20 01:27:10 +02:00
|
|
|
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 {
|
2024-01-21 21:42:58 +02:00
|
|
|
log.Fatalf("[🛑] listening failed: %s\n", err)
|
2024-01-20 01:27:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Run the hardware service
|
2024-01-20 18:35:50 +02:00
|
|
|
svc.RunLoggerService()
|
2024-01-21 21:42:58 +02:00
|
|
|
// Run the cleanup service
|
|
|
|
svc.RunCleanupService()
|
2024-01-20 01:27:10 +02:00
|
|
|
|
|
|
|
// Setting up signal capturing
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
// Block until a signal is received
|
|
|
|
<-quit
|
2024-01-21 21:42:58 +02:00
|
|
|
log.Println("[🦝] Shutting down server...")
|
2024-01-20 01:27:10 +02:00
|
|
|
|
|
|
|
// Graceful shutdown
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := srv.Shutdown(ctx); err != nil {
|
2024-01-21 21:42:58 +02:00
|
|
|
log.Fatal("[🛑] Server forced to shutdown:", err)
|
2024-01-20 00:21:59 +02:00
|
|
|
}
|
|
|
|
|
2024-01-21 21:42:58 +02:00
|
|
|
log.Println("[🦝] Server exiting")
|
2024-01-20 00:21:59 +02:00
|
|
|
}
|