reCoreD-UI/controllers/settings.go

77 lines
1.9 KiB
Go
Raw Normal View History

2024-04-09 08:28:18 +00:00
package controllers
import (
"reCoreD-UI/database"
"reCoreD-UI/models"
"strings"
2024-04-11 02:51:50 +00:00
"github.com/sirupsen/logrus"
2024-04-09 08:28:18 +00:00
)
const dnsSep = ","
type settingsDAO struct {
2024-04-11 02:51:50 +00:00
database.BaseDAO[models.ISettings]
2024-04-09 08:28:18 +00:00
}
func SetupDNS(dns ...string) error {
settings := models.Settings{Key: models.SettingsKeyDNSServer, Value: strings.Join(dns, dnsSep)}
2024-04-11 02:51:50 +00:00
if _, err := (settingsDAO{}).UpdateOrCreate(database.Client, &settings, models.Settings{Key: models.SettingsKeyDNSServer}); err != nil {
2024-04-09 08:28:18 +00:00
return err
}
return nil
}
func GetDNS() ([]string, error) {
settings, err := (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyDNSServer})
if err != nil {
return nil, err
}
2024-04-11 02:51:50 +00:00
return strings.Split(settings.(models.Settings).Value, dnsSep), nil
2024-04-09 08:28:18 +00:00
}
func SetupAdmin(username, password string) error {
2024-04-11 02:51:50 +00:00
logrus.Debugf("got %s:%s", username, password)
2024-04-09 08:28:18 +00:00
settingUsername := models.Settings{
Key: models.SettingsKeyAdminUsername,
Value: username,
}
settingPassword := models.Settings{
Key: models.SettingsKeyAdminPassword,
Value: password,
}
tx := database.Client.Begin()
2024-04-11 02:51:50 +00:00
if _, err := (settingsDAO{}).UpdateOrCreate(tx, &settingUsername, models.Settings{Key: models.SettingsKeyAdminUsername}); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-11 02:51:50 +00:00
if _, err := (settingsDAO{}).UpdateOrCreate(tx, &settingPassword, models.Settings{Key: models.SettingsKeyAdminPassword}); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
}
2024-04-11 02:51:50 +00:00
return tx.Commit().Error
2024-04-09 08:28:18 +00:00
}
func GetAdmin() (string, string, error) {
settings, err := (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyAdminUsername})
if err != nil {
return "", "", err
}
2024-04-11 02:51:50 +00:00
username := settings.(models.Settings).Value
2024-04-09 08:28:18 +00:00
settings, err = (settingsDAO{}).GetOne(database.Client, models.Settings{Key: models.SettingsKeyAdminPassword})
if err != nil {
return "", "", err
}
2024-04-11 02:51:50 +00:00
password := settings.(models.Settings).Value
2024-04-09 08:28:18 +00:00
return username, password, nil
}