status updates
This commit is contained in:
@@ -17,6 +17,47 @@ import (
|
||||
"golang.org/x/text/encoding/unicode"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
const (
|
||||
ConfigurationJson = "configuration.json"
|
||||
AssistRulesJson = "assistRules.json"
|
||||
EventJson = "event.json"
|
||||
EventRulesJson = "eventRules.json"
|
||||
SettingsJson = "settings.json"
|
||||
)
|
||||
|
||||
var decodeMap = map[string]func(string) (interface{}, error){
|
||||
ConfigurationJson: func(f string) (interface{}, error) {
|
||||
return readAndDecode[model.Configuration](f, ConfigurationJson)
|
||||
},
|
||||
AssistRulesJson: func(f string) (interface{}, error) {
|
||||
return readAndDecode[model.AssistRules](f, AssistRulesJson)
|
||||
},
|
||||
EventJson: func(f string) (interface{}, error) {
|
||||
return readAndDecode[model.EventConfig](f, EventJson)
|
||||
},
|
||||
EventRulesJson: func(f string) (interface{}, error) {
|
||||
return readAndDecode[model.EventRules](f, EventRulesJson)
|
||||
},
|
||||
SettingsJson: func(f string) (interface{}, error) {
|
||||
return readAndDecode[model.ServerSettings](f, SettingsJson)
|
||||
},
|
||||
}
|
||||
|
||||
func DecodeFileName(fileName string) func(path string) (interface{}, error) {
|
||||
if decoder, ok := decodeMap[fileName]; ok {
|
||||
return decoder
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mustDecode[T any](fileName, path string) (T, error) {
|
||||
result, err := DecodeFileName(fileName)(path)
|
||||
if err != nil {
|
||||
var zero T
|
||||
return zero, err
|
||||
}
|
||||
return result.(T), nil
|
||||
}
|
||||
|
||||
type ConfigService struct {
|
||||
repository *repository.ConfigRepository
|
||||
@@ -77,17 +118,32 @@ func (as ConfigService) UpdateConfig(ctx *fiber.Ctx, body *map[string]interface{
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
newDataUTF16, err := EncodeUTF16LEBOM(newData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
context := ctx.UserContext()
|
||||
|
||||
if (configFile == ConfigurationJson) {
|
||||
config, err := DecodeToMap[model.Configuration](newDataUTF16)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if (server.BroadcastPort != config.UdpPort || server.Port != config.TcpPort) {
|
||||
server.BroadcastPort = config.UdpPort
|
||||
server.Port = config.TcpPort
|
||||
as.serverRepository.UpdateServer(context, server)
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.WriteFile(configPath, newDataUTF16, 0644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Log change
|
||||
return as.repository.UpdateConfig(ctx.UserContext(), &model.Config{
|
||||
return as.repository.UpdateConfig(context, &model.Config{
|
||||
ServerID: uint(serverID),
|
||||
ConfigFile: configFile,
|
||||
OldConfig: string(oldDataUTF8),
|
||||
@@ -103,7 +159,7 @@ func (as ConfigService) UpdateConfig(ctx *fiber.Ctx, body *map[string]interface{
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// string: Application version
|
||||
func (as ConfigService) GetConfig(ctx *fiber.Ctx) (map[string]interface{}, error) {
|
||||
func (as ConfigService) GetConfig(ctx *fiber.Ctx) (interface{}, error) {
|
||||
serverID, _ := ctx.ParamsInt("id")
|
||||
configFile := ctx.Params("file")
|
||||
|
||||
@@ -113,13 +169,7 @@ func (as ConfigService) GetConfig(ctx *fiber.Ctx) (map[string]interface{}, error
|
||||
return nil, fiber.NewError(404, "Server not found")
|
||||
}
|
||||
|
||||
config, err := readFile(server.ConfigPath, configFile)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decoded, err := DecodeToMap(config)
|
||||
decoded, err := DecodeFileName(configFile)(server.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -143,47 +193,27 @@ func (as ConfigService) GetConfigs(ctx *fiber.Ctx) (*model.Configurations, error
|
||||
return nil, fiber.NewError(404, "Server not found")
|
||||
}
|
||||
|
||||
configuration, err := readFile(server.ConfigPath, "configuration.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decodedconfiguration, err := DecodeToMap(configuration)
|
||||
decodedconfiguration, err := mustDecode[model.Configuration](ConfigurationJson, server.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entrylist, err := readFile(server.ConfigPath, "entrylist.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decodedentrylist, err := DecodeToMap(entrylist)
|
||||
decodedAssistRules, err := mustDecode[model.AssistRules](AssistRulesJson, server.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
event, err := readFile(server.ConfigPath, "event.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decodedevent, err := DecodeToMap(event)
|
||||
decodedevent, err := mustDecode[model.EventConfig](EventJson, server.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
eventRules, err := readFile(server.ConfigPath, "eventRules.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decodedeventRules, err := DecodeToMap(eventRules)
|
||||
decodedeventRules, err := mustDecode[model.EventRules](EventRulesJson, server.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
settings, err := readFile(server.ConfigPath, "settings.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
decodedsettings, err := DecodeToMap(settings)
|
||||
decodedsettings, err := mustDecode[model.ServerSettings](SettingsJson, server.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -193,10 +223,24 @@ func (as ConfigService) GetConfigs(ctx *fiber.Ctx) (*model.Configurations, error
|
||||
Event: decodedevent,
|
||||
EventRules: decodedeventRules,
|
||||
Settings: decodedsettings,
|
||||
Entrylist: decodedentrylist,
|
||||
AssistRules: decodedAssistRules,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func readAndDecode[T interface{}](path string, configFile string) (T, error) {
|
||||
settings, err := readFile(path, configFile)
|
||||
var zero T
|
||||
if err != nil {
|
||||
return zero, err
|
||||
}
|
||||
decodedsettings, err := DecodeToMap[T](settings)
|
||||
if err != nil {
|
||||
return zero, err
|
||||
}
|
||||
|
||||
return decodedsettings, nil
|
||||
}
|
||||
|
||||
func readFile(path string, configFile string) ([]byte, error) {
|
||||
configPath := filepath.Join(path, "\\server\\cfg", configFile)
|
||||
oldData, err := os.ReadFile(configPath)
|
||||
@@ -219,19 +263,20 @@ func DecodeUTF16LEBOM(input []byte) ([]byte, error) {
|
||||
return transformBytes(decoder.NewDecoder(), input)
|
||||
}
|
||||
|
||||
func DecodeToMap(input []byte) (map[string]interface{}, error) {
|
||||
func DecodeToMap[T interface{}](input []byte) (T, error) {
|
||||
var zero T
|
||||
if input == nil {
|
||||
return nil, nil
|
||||
return zero, nil
|
||||
}
|
||||
configUTF8 := new(map[string]interface{})
|
||||
configUTF8 := new(T)
|
||||
decoded, err := DecodeUTF16LEBOM(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return zero, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(decoded, configUTF8)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return zero, err
|
||||
}
|
||||
return *configUTF8, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user