-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
50 lines (40 loc) · 990 Bytes
/
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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"time"
// "time"
"github.com/adamveld12/svelte-go-template/client/public"
"github.com/adamveld12/svelte-go-template/internal"
)
var (
httpAddr = flag.String("http-address", ":8000", "Bind address for http server")
)
func main() {
flag.Parse()
httpApiHandler := internal.NewAPIHandler(public.Assets)
httpServer := &http.Server{
Addr: *httpAddr,
Handler: httpApiHandler,
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
go runServer(httpServer)
<-signals
log.Println("Shutting down http server...")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*9)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
log.Fatalf("could not cleanly shutdown http server: %v", err)
}
}
func runServer(server *http.Server) {
log.Printf("Listening @ %s", *httpAddr)
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}