-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (62 loc) · 2 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"golang.org/x/oauth2"
"time"
"flag"
"github.com/google/go-github/github"
"github.com/smoya/ghtop/pkg/contributor"
"github.com/smoya/ghtop/pkg/logx"
"github.com/smoya/ghtop/pkg/server"
)
var port = flag.Int("port", 8080, "-port=<port> sets the server's listening port. 8080 by default.")
var env = flag.String("env", "prod", "-env=<environment> specifies the environment. prod by default.")
var ghToken = flag.String("gh-token", "", "-gh-token=<token> sets the token for Github API.")
var cacheTTL = flag.Int("ttl", 300, "-ttl=<cache-ttl> sets the ttl in seconds for the repository cache.")
var authUser = flag.String("auth-user", "", "-auth-user=<username> sets the username for basic authentication.")
var authPassword = flag.String("auth-password", "", "-auth-password=<password> sets the password for basic authentication.")
func init() {
flag.Parse()
if *ghToken == "" {
log.Fatal("Missing Github token. -gh-token=<token>")
}
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger, err := logx.NewStdOut("application", "ghtop", *env, logx.LevelInfo)
if err != nil {
log.Fatal(err)
}
ensureInterruptionsStopApplication(cancel, logger)
ts := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: *ghToken,
},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
contributorRepo := contributor.WithCache(contributor.NewGihubRepository(client), time.Duration(*cacheTTL)*time.Second)
config := server.NewConfig(*port, *env, *authUser, *authPassword)
s := server.NewServer(config, logger, contributorRepo)
err = s.Run(ctx)
if err != nil {
log.Fatal(err)
}
}
func ensureInterruptionsStopApplication(cancelFunc context.CancelFunc, logger logx.Logger) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
s := <-c
logger.Info(fmt.Sprintf("Got signal %s. Stopping server...", s))
cancelFunc()
os.Exit(1)
return
}()
}