-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 1.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/joho/godotenv"
"github.com/Hamedblue1381/restaurant-reserve/config"
"github.com/Hamedblue1381/restaurant-reserve/routers"
"github.com/Hamedblue1381/restaurant-reserve/routers/api"
v1 "github.com/Hamedblue1381/restaurant-reserve/routers/api/v1"
)
func main() {
// Load the env
err := godotenv.Load(".env")
if err != nil {
log.Println("Error loading .env file")
}
// Setup database connection
db := config.SetupDBConnection()
if db == nil {
log.Fatal("Failed to connect to database!")
}
v1.InitializeReservationHandler(db)
v1.InitializedFoodHandler(db)
v1.InitializedMealTypeHandler(db)
v1.InitializedSidesHandler(db)
v1.InitializedUserHandler(db)
api.InitializedAuthHandler(db)
// Initialize router
r := routers.UseRouter()
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
server := &http.Server{
Addr: ":" + os.Getenv("PORT"),
Handler: r,
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
<-ctx.Done()
stop()
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
fmt.Println("Gracefully shutting down server..., press Ctrl+C again to force shutdown")
defer cancel()
if err := server.Shutdown(timeoutCtx); err != nil {
log.Fatal("Server forced to shutdown:", err)
}
}