-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (72 loc) · 2.07 KB
/
Copy pathmain.go
File metadata and controls
89 lines (72 loc) · 2.07 KB
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
85
86
87
88
89
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"os/user"
"time"
middleware "github.com/gorilla/handlers"
"github.com/searchspring/asanaban/api"
"github.com/searchspring/asanaban/config"
)
func main() {
_config := config.Load()
user, _ := user.Current()
log.Printf("Current user: %v\n", user.Username)
server := createNewServer(_config)
startServer(server)
waitForSigInt(server)
}
func startServer(server *http.Server) {
go func() {
log.Printf("Asanaban listening at %s\n", server.Addr)
if err := server.ListenAndServe(); err != nil {
log.Println(err)
}
}()
}
func createNewServer(_config *config.Config) *http.Server {
router := createNewRouter()
return &http.Server{
Addr: fmt.Sprintf(":%d", _config.HttpPort),
WriteTimeout: time.Minute * 60,
ReadTimeout: time.Minute * 60,
IdleTimeout: time.Minute * 60,
Handler: router,
}
}
func createNewRouter() http.Handler {
router, err := api.CreateRouter()
if err != nil {
panic(err)
}
router.PathPrefix("").Handler(http.FileServer(http.Dir("./public")))
return middleware.LoggingHandler(os.Stdout, router)
}
func waitForSigInt(httpServer *http.Server) {
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()
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.
//nolint
httpServer.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)
}