/
main.go
71 lines (62 loc) · 1.36 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"context"
"errors"
"github.com/e-zhydzetski/rpm-server/internal/rpmserver"
"github.com/go-chi/chi"
"golang.org/x/sync/errgroup"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
ctx := context.Background()
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ctx.Done():
return ctx.Err()
case sig := <-c:
return errors.New("graceful shutdown by " + sig.String())
}
})
cfg := rpmserver.Config{
ListenAddr: os.Getenv("LISTEN_ADDR"),
AccessToken: os.Getenv("ACCESS_TOKEN"),
PushRepoPath: os.Getenv("PUSH_PATH"),
ReposRootPath: os.Getenv("REPOS_ROOT"),
}
if cfg.ListenAddr == "" {
cfg.ListenAddr = ":8080"
}
r := chi.NewRouter()
handler := rpmserver.NewHandler(cfg)
r.Mount("/api", handler)
r.Mount("/repos",
http.StripPrefix("/repos",
rpmserver.NewFileServer(cfg.ReposRootPath),
),
)
server := &http.Server{
Addr: cfg.ListenAddr,
Handler: r,
}
server.BaseContext = func(_ net.Listener) context.Context {
return ctx
}
g.Go(func() error {
<-ctx.Done()
return server.Shutdown(context.Background())
})
g.Go(func() error {
log.Println("start listening at", cfg.ListenAddr, "...")
return server.ListenAndServe()
})
err := g.Wait()
log.Println("server stopped:", err)
}