vanity-tester-backend-go/cmd/vanity-tester-backend/config.go

60 lines
1.1 KiB
Go
Raw Normal View History

2024-12-27 14:04:18 -03:00
package main
import (
"encoding/json"
2025-01-07 23:42:16 -03:00
"os"
2024-12-27 14:04:18 -03:00
)
type HTTPServer struct {
Port string `json:"port"`
Addr string `json:"addr"`
}
type Redis struct {
Addr string `json:"addr"`
DB int `json:"db"`
}
2025-01-08 20:16:19 -03:00
type GqlConfig struct {
2025-01-07 23:42:16 -03:00
ClientID string `json:"clientId"`
}
2024-12-27 14:04:18 -03:00
type Config struct {
HTTPServer HTTPServer `json:"http_server"`
Redis Redis `json:"redis"`
2025-01-08 20:16:19 -03:00
Gql GqlConfig `json:"gql"`
DbPath string `json:"dbpath"`
2024-12-27 14:04:18 -03:00
}
func loadConfig(configPath string) Config {
// Default config in case a value is missing in the config file
var config = Config{
HTTPServer: HTTPServer{
2025-01-07 23:42:16 -03:00
Addr: "127.0.0.1",
Port: "8080",
2024-12-27 14:04:18 -03:00
},
Redis: Redis{
Addr: "127.0.0.1:6379",
DB: 0,
},
2025-01-08 20:16:19 -03:00
Gql: GqlConfig{
2025-01-07 23:42:16 -03:00
ClientID: "ue6666qo983tsx6so1t0vnawi233wa",
},
2025-01-08 20:16:19 -03:00
DbPath: "./database.sqlite3",
2025-01-07 23:42:16 -03:00
}
file, err := os.ReadFile(configPath)
if err != nil {
logger.Error().Msg("Failed to read config file. The default config will be used")
return config
2024-12-27 14:04:18 -03:00
}
err = json.Unmarshal(file, &config)
if err != nil {
2025-01-07 23:42:16 -03:00
logger.Error().Msg("Failed to parse config file. The default config will be used")
return config
2024-12-27 14:04:18 -03:00
}
return config
}