-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (172 loc) · 5.34 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"github.com/charmbracelet/log"
"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database/sqlite3"
"github.com/golang-migrate/migrate/source/file"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var httpMetricsServer *http.Server
var (
DodudaVersion = "v0.1.0"
DodualmShort = "dodualm - Dofus Almanax API"
DodualmLong = "The Dofus Almanax API."
DodudaVersionHelp = DodualmShort + "\n" + DodudaVersion + "\nhttps://github.com/dofusdude/dodualm"
ApiPort string
ServerTz string
ApiScheme string
ApiHostName string
MeiliHost string
MeiliKey string
Database *Repository
rootCmd = &cobra.Command{
Use: "dodualm",
Short: DodualmShort,
Long: DodualmLong,
SilenceErrors: true,
SilenceUsage: false,
Run: rootCommand,
}
migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Run migrations on the database.",
SilenceErrors: true,
SilenceUsage: false,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Running migrate command")
},
}
migrateDownCmd = &cobra.Command{
Use: "down",
Short: "run migrations for downgrading",
Long: `Command to downgrade database`,
Run: migrateDown,
}
migrateUpCmd = &cobra.Command{
Use: "up",
Short: "run migrations for upgrading",
Long: `Command to upgrade database`,
Run: migrateUp,
}
)
func migrateUp(cmd *cobra.Command, args []string) {
database := NewDatabaseRepository(context.Background(), ".")
dbDriver, err := sqlite3.WithInstance(database.Db, &sqlite3.Config{})
if err != nil {
log.Fatalf("instance error: %v \n", err)
}
fileSource, err := (&file.File{}).Open("file://migrations")
if err != nil {
log.Fatalf("opening file error: %v \n", err)
}
m, err := migrate.NewWithInstance("file", fileSource, "myDB", dbDriver)
if err != nil {
log.Fatalf("migrate error: %v \n", err)
}
if err = m.Up(); err != nil {
log.Fatalf("migrate up error: %v \n", err)
}
log.Print("Migrate up done with success")
}
func migrateDown(cmd *cobra.Command, args []string) {
database := NewDatabaseRepository(context.Background(), ".")
dbDriver, err := sqlite3.WithInstance(database.Db, &sqlite3.Config{})
if err != nil {
log.Fatalf("instance error: %v \n", err)
}
fileSource, err := (&file.File{}).Open("file://migrations")
if err != nil {
log.Fatalf("opening file error: %v \n", err)
}
m, err := migrate.NewWithInstance("file", fileSource, "myDB", dbDriver)
if err != nil {
log.Fatalf("migrate error: %v \n", err)
}
if err = m.Down(); err != nil {
log.Fatalf("migrate down error: %v \n", err)
}
log.Print("Migrate down done with success")
}
func rootCommand(cmd *cobra.Command, args []string) {
if version, _ := cmd.Flags().GetBool("version"); version {
fmt.Println(DodudaVersion)
return
}
metrics, err := cmd.Flags().GetBool("metrics")
if err != nil {
log.Fatal(err)
}
dbdir, err := cmd.Flags().GetString("dbdir")
if err != nil {
log.Fatal(err)
}
gameVersion, err := cmd.Flags().GetString("game-version")
if err != nil {
log.Fatal(err)
}
Database = NewDatabaseRepository(context.Background(), dbdir)
defer Database.Deinit()
almanaxData, err := loadAlmanaxData(gameVersion)
if err != nil {
log.Fatal(err)
}
log.Info("Almanax data loaded", "count", len(almanaxData))
httpDataServer := &http.Server{
Addr: fmt.Sprintf(":%s", ApiPort),
Handler: Router(),
}
if metrics {
apiPort, _ := strconv.Atoi(ApiPort)
metricsPort := apiPort + 1
httpMetricsServer = &http.Server{
Addr: fmt.Sprintf(":%d", metricsPort),
Handler: promhttp.Handler(),
}
go func() {
log.Info("Metrics server started", "port", metricsPort)
if err := httpMetricsServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
}
log.Info("Almanax server started", "port", ApiPort)
if err := httpDataServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}
func main() {
viper.SetDefault("LOG_LEVEL", "info")
viper.AutomaticEnv()
rootCmd.Flags().Bool("version", false, "Print the dodualm version.")
rootCmd.Flags().Bool("metrics", false, "Toggle Prometheus metrics export.")
rootCmd.Flags().String("dbdir", ".", "Database directory")
rootCmd.Flags().String("game-version", "latest", "Specify the game version to use. Default is latest.")
rootCmd.AddCommand(migrateCmd)
migrateCmd.AddCommand(migrateDownCmd)
migrateCmd.AddCommand(migrateUpCmd)
viper.SetDefault("MEILI_PORT", "7700")
viper.SetDefault("MEILI_MASTER_KEY", "masterKey")
viper.SetDefault("MEILI_PROTOCOL", "http")
viper.SetDefault("MEILI_HOST", "127.0.0.1")
viper.SetDefault("API_PORT", "3000")
viper.SetDefault("API_SCHEME", "http")
viper.SetDefault("API_HOSTNAME", "localhost")
viper.SetDefault("SERVER_TZ", "Europe/Berlin")
ApiScheme = viper.GetString("API_SCHEME")
ApiHostName = viper.GetString("API_HOSTNAME")
ApiPort = viper.GetString("API_PORT")
MeiliKey = viper.GetString("MEILI_MASTER_KEY")
MeiliHost = fmt.Sprintf("%s://%s:%s", viper.GetString("MEILI_PROTOCOL"), viper.GetString("MEILI_HOST"), viper.GetString("MEILI_PORT"))
ServerTz = getEnv("SERVER_TZ", "Europe/Berlin")
err := rootCmd.Execute()
if err != nil && err.Error() != "" {
fmt.Fprintln(os.Stderr, err)
}
}