-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (64 loc) · 1.87 KB
/
main.go
File metadata and controls
80 lines (64 loc) · 1.87 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
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 (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/nbtca/saturday/repo"
"github.com/nbtca/saturday/router"
"github.com/nbtca/saturday/service"
"github.com/nbtca/saturday/util"
"github.com/joho/godotenv"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
)
func initConfig() error {
if err := godotenv.Load(); err != nil {
util.Logger.Warnf("Error loading .env file: %v", err)
}
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
consulAddr := viper.GetString("CONSUL_HTTP_ADDR")
consulKey := viper.GetString("CONSUL_KEY")
if consulAddr != "" {
util.Logger.Debug("Using consul config", consulAddr)
viper.AddRemoteProvider("consul", consulAddr, consulKey)
viper.SetConfigType("json") // Need to explicitly set this to json
err := viper.ReadRemoteConfig()
if err != nil {
return fmt.Errorf("failed at reading config from consul: %w", err)
}
go func() {
for {
time.Sleep(time.Second * 5) // delay after each request
// currently, only tested with etcd support
err := viper.WatchRemoteConfig()
if err != nil {
util.Logger.Errorf("unable to read remote config: %v", err)
continue
}
}
}()
}
return nil
}
func main() {
if err := initConfig(); err != nil {
log.Fatalf("Error initializing config: %v", err)
}
util.InitValidator()
util.InitDialer()
util.InitGithubClient()
repo.InitDB()
defer repo.CloseDB()
service.LogtoServiceApp = service.MakeLogtoService(viper.GetString("logto.endpoint"))
util.Logger.Debug("LogtoService initialized with endpoint: " + viper.GetString("logto.endpoint"))
r := router.SetupRouter()
viper.SetDefault("port", 4000)
port := viper.GetInt("port")
util.Logger.Infof("Starting server at %d...", port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), r); err != nil {
log.Fatalf("Server failed to start: %v", err)
}
}