-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 1.08 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"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
)
func main() {
checkAPIKey()
port := getPort()
mux := http.NewServeMux()
s := http.Server{
Addr: port,
Handler: mux,
ReadTimeout: readR,
WriteTimeout: writeR,
IdleTimeout: keepA,
}
fs := http.FileServer(http.Dir("assets"))
mux.Handle("/assets/", http.StripPrefix("/assets/", fs))
mux.HandleFunc("/search", searchHandler)
mux.HandleFunc("/", indexHandler)
log.Printf("starting http server on port: %s", port)
go func() {
if err := s.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("error starting server: %v", err)
}
}()
log.Printf("server started with %s", runtime.Version())
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-done
log.Print("signal closing server received")
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Printf("server shutdown failed: %v", err)
}
log.Print("server shutdown gracefully")
}