SimpleTutorialHosting/cmd/server/main.go
Mitchell Thompson 56635fc145 first commit
2024-10-27 07:12:56 -04:00

65 lines
1.3 KiB
Go

package main
import (
"SimpleTutorialHosting/internal/config"
"SimpleTutorialHosting/internal/server"
"SimpleTutorialHosting/internal/storage"
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
cfg, err := config.Load(ctx)
if err != nil {
log.Fatalf("Failed to load config: %v\n", err)
}
store, err := storage.NewS3Client(ctx, cfg.BucketName)
if err != nil {
log.Fatalf("Failed to create S3 client: %v\n", err)
}
srv, err := server.New(cfg, store)
if err != nil {
log.Fatalf("Failed to create server: %v\n", err)
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err := srv.Start(); err != nil {
log.Printf("Server error: %v\n", err)
cancel()
}
}()
log.Printf("Server started on port %d\n", cfg.Port)
select {
case sig := <-done:
log.Printf("Received signal: %v\n", sig)
case <-ctx.Done():
log.Printf("Context cancelled: %v\n", ctx.Err())
}
log.Print("Shutting down server")
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("Error during shutdown: %v\n", err)
os.Exit(1)
}
log.Print("Server exited properly")
}