This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
102 lines (85 loc) · 2.58 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//go:generate statik -src=./public
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
log "github.com/inconshreveable/log15"
)
const (
githubGraphqlEndpoint = "https://api.github.com/graphql"
)
var (
terminate = make(chan os.Signal, 1)
version = "dev"
)
type gitreleasesEnv struct {
addr string
token string
metricsUsername string
metricsPassword string
}
func getEnv() gitreleasesEnv {
addr := os.Getenv("LISTEN_ADDR")
token := os.Getenv("GITHUB_TOKEN")
metricsUsername := os.Getenv("METRICS_USERNAME")
metricsPassword := os.Getenv("METRICS_PASSWORD")
if addr == "" {
panic("LISTEN_ADDR is required")
}
if token == "" {
panic("GITHUB_TOKEN is required")
}
if metricsUsername == "" {
panic("METRICS_USERNAME is required")
}
if metricsPassword == "" {
panic("METRICS_PASSWORD is required")
}
return gitreleasesEnv{addr: addr, token: token, metricsUsername: metricsUsername, metricsPassword: metricsPassword}
}
func main() {
logger := log.New("module", "gitrelases/main", "version", version)
// FIXME: change to another format before deployment
handler := log.StreamHandler(os.Stdout, log.TerminalFormat())
logger.SetHandler(handler)
logger.Info("Starting up application")
env := getEnv()
cacheTTL := 5 * time.Minute
tickerInterval := 10 * time.Minute
cache := NewCache(1000, int(cacheTTL.Seconds()), tickerInterval)
httpClient := NewOauthClient(context.Background(), env.token)
client := NewGitHubClient(githubGraphqlEndpoint, httpClient, cache, logger.New("module", "gitreleases/github"))
apiServer := NewAPIServer(env.addr, env.metricsUsername, env.metricsPassword, version, client, logger.New("module", "gitreleases/api"))
// Catch SIGINT and SIGTERM.
signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM)
go func() {
err := apiServer.Start()
if err == http.ErrServerClosed {
logger.Info("server closed")
} else {
logger.Error("cannot start server", "err", err)
terminate <- syscall.SIGABRT
}
}()
// Wait for SIGINT or SIGTERM as stop signal (or SIGABRT if the server
// could not be started).
sig := <-terminate
if sig == syscall.SIGABRT {
return
}
logger.Info("termination signal received", "signal", sig.String())
// Graceful shutdown of the HTTP server. Give 500ms to finish
// current messages being handled.
grace, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
err := apiServer.Shutdown(grace)
if err != nil {
logger.Info("server shutdown with problems", "err", err)
} else {
logger.Info("server shut down properly", "err", err)
}
}