1
0
mirror of synced 2024-11-25 06:26:03 +03:00
mg-transport-api-client-go/examples/telegram/config.go
2023-12-28 19:32:03 +03:00

43 lines
999 B
Go

package main
import (
"encoding/json"
"github.com/go-playground/validator/v10"
"log"
"os"
"strings"
)
var AppConfig Config
type Config struct {
Listen string `json:"listen"`
BaseURL string `json:"baseUrl" validate:"required,url"`
System string `json:"system" validate:"required,url"`
APIKey string `json:"apiKey" validate:"required"`
TGBotToken string `json:"tgBotToken" validate:"required"`
}
func LoadConfig(src string) {
file, err := os.Open(src)
if err != nil {
panic(err)
}
defer func() { _ = file.Close() }()
if err := json.NewDecoder(file).Decode(&AppConfig); err != nil {
panic(err)
}
validate := validator.New(validator.WithRequiredStructEnabled())
err = validate.Struct(AppConfig)
if err != nil {
log.Fatalln(err)
}
if strings.HasSuffix(AppConfig.BaseURL, "/") {
AppConfig.BaseURL = AppConfig.BaseURL[:len(AppConfig.BaseURL)-1]
}
if AppConfig.Listen == "" {
AppConfig.Listen = ":8080"
}
log.Println("loaded configuration from", src)
}