1
0
mirror of synced 2024-11-28 16:06:04 +03:00
mg-transport-api-client-go/examples/telegram/config.go

43 lines
999 B
Go
Raw Normal View History

2023-12-28 19:32:03 +03:00
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)
}