-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (66 loc) · 1.73 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/NeelavaChatterjee/git-sync/database"
"github.com/NeelavaChatterjee/git-sync/routes"
"github.com/NeelavaChatterjee/git-sync/utilities"
"github.com/joho/godotenv"
)
func init() {
// Loading environment variables
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
}
func main() {
// Writing logs to a file
f, err := os.OpenFile(os.Getenv("LOG_FILENAME"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
}
defer f.Close()
log.SetOutput(f)
fmt.Println("Database is being initialized")
// Initialize Cron
utilities.Initialize()
utilities.Cron.Start()
defer utilities.Cron.Stop()
// Connect to database
database.Connect()
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for the existing connections to finish - e.g. 15s or 1m")
flag.Parse()
router := routes.Router()
srv := &http.Server{
Addr: "127.0.0.1:4000",
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: router,
}
// Run our server in a goroutine so that it doesn't block
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
c := make(chan os.Signal, 1)
// We will accept graceful shutdowns when quit via SIGINT (Ctrl + C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl + /) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
srv.Shutdown(ctx)
log.Println("shutting down")
os.Exit(0)
}