-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
105 lines (89 loc) · 2.41 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
103
104
105
package main
import (
"fmt"
"id-generator/constants"
"id-generator/generator"
"id-generator/utils"
"os"
"strings"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/rqlite/gorqlite"
"github.com/spf13/viper"
"go.uber.org/zap"
_ "github.com/rqlite/gorqlite"
)
func newLogger() *zap.Logger {
env := viper.GetString("workspace.env")
var logger *zap.Logger
switch env {
case "DEVELOPMENT":
logger, _ = zap.NewDevelopment()
default:
logger, _ = zap.NewProduction()
}
return logger
}
func initConfigs(env string) {
viper.AddConfigPath("conf")
viper.SetConfigName(fmt.Sprintf("config.%s", env))
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "__")
viper.SetEnvKeyReplacer(replacer)
if err := viper.ReadInConfig(); err != nil {
utils.LogInfo("Error reading config file, %s", err)
}
// viper.SetDefault("rqlite.uri", "http://127.0.0.1:4001")
}
func getMapEnvVars() *map[string]string {
ret := make(map[string]string)
envsOS := os.Environ()
for _, envOS := range envsOS {
items := strings.Split(envOS, "=")
if len(items) > 1 {
ret[items[0]] = items[1]
}
}
return &ret
}
func main() {
envVars := getMapEnvVars()
env := "development"
if value, found := (*envVars)[constants.ApiOsEnv]; found {
env = value
}
utils.LogInfo(fmt.Sprintf("API is running in [%s] mode", env))
initConfigs(env)
fmt.Println(viper.GetString("rqlite.uri"))
route := gin.Default()
logger := newLogger()
route.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"POST", "PUT", "GET", "DELETE"},
AllowHeaders: []string{"Access-Control-Allow-Headers", "Origin", "Accept", "X-Requested-With", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
var conn gorqlite.Connection
rqliteURI := viper.GetString("rqlite.uri")
for true {
conn1, err := gorqlite.Open(rqliteURI)
_, err1 := conn1.Leader()
utils.LogInfo("%s\t%s\t%v", "leader", rqliteURI, err1)
if err != nil {
utils.LogInfo("RETRY")
time.Sleep(1 * time.Second)
} else {
utils.LogInfo("CONNECTED")
conn = conn1
break
}
}
generatorStore := generator.NewGeneratorStore(&conn, logger)
generatorAPI := generator.NewGeneratorAPI(generatorStore, logger)
generatorAPI.InitRoute(route, "/id_generator")
//cleaner := workers.NewCleaner(generatorStore, logger)
//go cleaner.cleaner()
route.Run("0.0.0.0:8080")
}