-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (45 loc) · 945 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"
"log"
"net/http"
"os"
"os/signal"
"path"
"syscall"
"time"
"gopkg.in/natefinch/lumberjack.v2"
)
func clean(srv *server, cancel context.CancelFunc) {
cancel()
}
func run() error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
log.SetOutput(&lumberjack.Logger{
Filename: path.Join(home, "logs/motorhead/access.log"),
MaxSize: 100,
MaxBackups: 10,
})
srv := NewServer()
go func() {
log.Println("Starting up server...")
if err := srv.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
<-done
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer clean(srv, cancel)
log.Println("Shutting down server...")
return srv.Server.Shutdown(ctx)
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}