8
"github.com/gookit/ini/v2"
11
type AppConfig struct {
13
CDNBaseURL string `app_config:"CDN_BASE_URL"`
14
DatabaseDSN string `app_config:"DB"`
15
Host string `app_config:"HOST"`
16
Port string `app_config:"PORT"`
17
BackendApiUrl string `app_config:"BACKEND_API_URL"`
18
BackendApiUser string `app_config:"BACKEND_API_USER"`
19
BackendApiWsseKey string `app_config:"BACKEND_API_WSSE_KEY"`
20
TelegramToken string `app_config:"TELEGRAM_TOKEN"`
21
TelegramAdminID int `app_config:"TELEGRAM_ADMIN_ID"`
22
SessionHashKey string `app_config:"SESSION_HASH_KEY"`
23
SessionBlockKey string `app_config:"SESSION_BLOCK_KEY"`
25
OAuthYandexID string `app_config:"OAUTH_CLIENT_ID_YANDEX"`
26
OAuthYandexSecret string `app_config:"OAUTH_SECRET_YANDEX"`
27
OAuthVkID string `app_config:"OAUTH_CLIENT_ID_VK"`
28
OAuthVkSecret string `app_config:"OAUTH_SECRET_VK"`
33
func Load(file string) error {
34
cnf = AppConfig{AppData: loadAppData()}
39
func GetConfig() AppConfig {
43
func IsDevMode() bool {
47
func GetBuildTag() (tag string) {
57
func configStringValue(paramName string) (string, error) {
59
if _, ok := ini.GetValue(paramName); ok {
60
value = ini.String(paramName)
62
return value, errors.New("app.ini: Undefined parameter \"" + paramName + "\"")
68
func configIntValue(paramName string) (int, error) {
70
if _, ok := ini.GetValue(paramName); ok {
71
value = ini.Int(paramName)
73
return value, errors.New("app.ini: Undefined parameter \"" + paramName + "\"")
79
type structField struct {
82
fieldType reflect.Kind
85
func (c *AppConfig) load(file string) error {
86
err := ini.LoadExists(file)
91
rv := reflect.ValueOf(c)
93
return fmt.Errorf("config: load error %v", reflect.TypeOf(c))
96
return setupFields(rv, parseFields(reflect.TypeOf(*c)))
99
func setupFields(rv reflect.Value, fields []structField) error {
100
if rv.Kind() != reflect.Pointer {
101
return fmt.Errorf("config: argument is not a pointer")
104
confValue := rv.Elem()
105
for _, field := range fields {
106
f := confValue.FieldByName(field.name)
107
if f.IsValid() && f.CanSet() {
108
switch field.fieldType {
110
intVal, err := configIntValue(field.parameterName)
115
if !f.OverflowInt(x) {
118
return fmt.Errorf("config: int overflow for field %s", field.name)
121
stringVal, err := configStringValue(field.parameterName)
125
f.SetString(stringVal)
127
return fmt.Errorf("config: undefined type %s of field %s", field.fieldType, field.name)
135
func parseFields(t reflect.Type) []structField {
136
var fields = make([]structField, 0)
137
for i := 0; i < t.NumField(); i++ {
139
tag := f.Tag.Get("app_config")
141
fields = append(fields, structField{
143
fieldType: f.Type.Kind(),