This repository was archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
84 lines (66 loc) · 1.88 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
// @title Valkyrie API
// @version 1.0
// @description Valkyrie REST API Specs. This service uses sessions for authentication
// @license.name Apache 2.0
// @host localhost:<PORT>
// @BasePath /api
func main() {
log.Println("Starting server...")
// Load dev env from .env file
if gin.Mode() != gin.ReleaseMode {
err := godotenv.Load()
if err != nil {
log.Fatalln("Error loading .env file")
}
}
// initialize data sources
ds, err := initDS()
if err != nil {
log.Fatalf("Unable to initialize data sources: %v\n", err)
}
router, err := inject(ds)
if err != nil {
log.Fatalf("Failure to inject data sources: %v\n", err)
}
srv := &http.Server{
Addr: ":" + os.Getenv("PORT"),
Handler: router,
}
// Graceful server shutdown - https://github.com/gin-gonic/examples/blob/master/graceful-shutdown/graceful-shutdown/server.go
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Failed to initialize server: %v\n", err)
}
}()
log.Printf("Listening on port %v\n", srv.Addr)
// Wait for kill signal of channel
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
// This blocks until a signal is passed into the quit channel
<-quit
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// shutdown data sources
if err := ds.close(); err != nil {
log.Fatalf("A problem occurred gracefully shutting down data sources: %v\n", err)
}
// Shutdown server
log.Println("Shutting down server...")
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v\n", err)
}
}