-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
70 lines (58 loc) · 1.42 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
package main
import (
"context"
"embed"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/bachtran02/bachtran.go/libs"
"github.com/shurcooL/githubv4"
"golang.org/x/exp/slog"
"golang.org/x/oauth2"
)
var (
//go:embed assets
Assets embed.FS
)
func main() {
cfgPath := flag.String("config", "config.yml", "path to config file")
flag.Parse()
cfg, err := libs.LoadConfig(*cfgPath)
if err != nil {
slog.Error("failed to load config", slog.Any("error", err))
os.Exit(-1)
}
setupLogger(cfg.Log)
slog.Info("Starting bachtran.dev...")
var assets http.FileSystem = http.FS(Assets)
httpClient := &http.Client{
Timeout: 10 * time.Second,
}
githubClient := githubv4.NewClient(oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cfg.GitHub.AccessToken},
)))
s := libs.NewServer("null", cfg, httpClient, githubClient, assets)
go s.Start()
defer s.Close()
slog.Info(fmt.Sprintf("Started bachtran.dev on %s", cfg.ListenAddr))
si := make(chan os.Signal, 1)
signal.Notify(si, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-si
}
func setupLogger(cfg libs.LogConfig) {
opts := &slog.HandlerOptions{
AddSource: cfg.AddSource,
Level: cfg.Level,
}
var handler slog.Handler
if cfg.Format == "json" {
handler = slog.NewJSONHandler(os.Stdout, opts)
} else {
handler = slog.NewTextHandler(os.Stdout, opts)
}
slog.SetDefault(slog.New(handler))
}