-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (44 loc) · 1.09 KB
/
main.go
File metadata and controls
52 lines (44 loc) · 1.09 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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"boot.dev/linko/internal/store"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
httpPort := flag.Int("port", 8899, "port to listen on")
dataDir := flag.String("data", "./data", "directory to store data")
flag.Parse()
status := run(ctx, cancel, *httpPort, *dataDir)
cancel()
os.Exit(status)
}
func run(ctx context.Context, cancel context.CancelFunc, httpPort int, dataDir string) int {
st, err := store.New(dataDir)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create store: %v\n", err)
return 1
}
s := newServer(*st, httpPort, cancel)
var serverErr error
go func() {
serverErr = s.start()
}()
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.shutdown(shutdownCtx); err != nil {
fmt.Fprintf(os.Stderr, "failed to shutdown server: %v\n", err)
return 1
}
if serverErr != nil {
fmt.Fprintf(os.Stderr, "server error: %v\n", serverErr)
return 1
}
return 0
}