-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 1.16 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"
"net/http"
"os"
"os/signal"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
routes "github.com/ignaciochemes/beammp-server-info-api/src/Routes"
)
func main() {
var wait time.Duration
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
flag.Parse()
r := mux.NewRouter()
r.HandleFunc("/servers", routes.GetDataFromBMPBackendRoute)
r.HandleFunc("/files/update", routes.GetNewVersionOfServer)
r.HandleFunc("/files", routes.SendBeamMPServerFiles)
cors := handlers.AllowedOrigins([]string{"*"})
srv := &http.Server{
Addr: "0.0.0.0:3003",
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: handlers.CORS(cors)(r),
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
srv.Shutdown(ctx)
log.Println("shutting down")
os.Exit(0)
}