-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (55 loc) · 1.62 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
// teyit.link
package main
import (
"context"
"github.com/noddigital/teyit.link/database"
"github.com/noddigital/teyit.link/handlers"
"github.com/noddigital/teyit.link/utils"
"log"
"net/http"
"os"
"os/signal"
"time"
)
//go:generate statik -src=./public/
func main() {
config := utils.InitConfig()
db := database.InitDB(config.DbDialect, config.DbUri)
database.Migrate(db)
defer db.Close()
srv := &http.Server{
Addr: config.ServerAddr,
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
// Pass our instance of gorilla/mux in.
// Handler: CSRF(handlers.CreateRoutes()),
Handler: handlers.CreateRoutes(),
}
// Run our server in a goroutine so that it doesn't block.
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Panic(err)
} else {
log.Printf("Server started")
}
}()
c := make(chan os.Signal, 1)
// We'll 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
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), config.GracefulShutdown)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Println("shutting down")
os.Exit(0)
}