/
main.go
52 lines (46 loc) · 1.5 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
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"time"
"github.com/OpenCoursePlatform/OpenCoursePlatform-Go/server"
_ "github.com/go-sql-driver/mysql"
)
func main() {
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Minute, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
flag.Parse()
server := server.Make()
log.Println("======================================")
log.Println("Starting Open Course Platform")
go func() {
if err := server.ListenAndServe(); err != nil {
log.Println(err)
}
}()
log.Println("Started")
log.Println("======================================")
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(), wait)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
server.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("======================================")
log.Println("Closing Open Course Platform")
log.Println("Shutting down")
log.Println("======================================")
os.Exit(0)
}