mirror of
https://github.com/Forceu/Gokapi.git
synced 2025-12-30 13:29:34 -06:00
Refactored config to json
This commit is contained in:
@@ -75,12 +75,7 @@ func save() {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
configJson, err := json.MarshalIndent(serverSettings, "", " ")
|
||||
if err != nil {
|
||||
fmt.Println("Error encoding configuration:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
_, err = io.Copy(file, bytes.NewReader(configJson))
|
||||
_, err = io.Copy(file, bytes.NewReader(serverSettings.ToJson()))
|
||||
if err != nil {
|
||||
fmt.Println("Error writing configuration:", err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
)
|
||||
|
||||
// Configuration is a struct that contains the global configuration
|
||||
type Configuration struct {
|
||||
Authentication AuthenticationConfig `json:"Authentication"`
|
||||
@@ -13,3 +18,21 @@ type Configuration struct {
|
||||
UseSsl bool `json:"UseSsl"`
|
||||
MaxFileSizeMB int `json:"MaxFileSizeMB"`
|
||||
}
|
||||
|
||||
// ToJson returns an idented JSon representation
|
||||
func (c Configuration) ToJson() []byte {
|
||||
result, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
log.Fatal("Error encoding configuration:", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// ToString returns the object as an unidented Json string used for test units
|
||||
func (c Configuration) ToString() string {
|
||||
result, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
42
internal/models/Configuration_test.go
Normal file
42
internal/models/Configuration_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"Gokapi/internal/test"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testConfig = Configuration{
|
||||
Authentication: AuthenticationConfig{
|
||||
Method: 0,
|
||||
SaltAdmin: "saltadmin",
|
||||
SaltFiles: "saltfiles",
|
||||
Username: "admin",
|
||||
Password: "adminpwhashed",
|
||||
HeaderKey: "",
|
||||
OauthProvider: "",
|
||||
OAuthClientId: "",
|
||||
OAuthClientSecret: "",
|
||||
HeaderUsers: nil,
|
||||
OauthUsers: nil,
|
||||
},
|
||||
Port: ":12345",
|
||||
ServerUrl: "https://testserver.com/",
|
||||
RedirectUrl: "https://test.com",
|
||||
ConfigVersion: 11,
|
||||
LengthId: 5,
|
||||
DataDir: "test",
|
||||
MaxMemory: 50,
|
||||
UseSsl: true,
|
||||
MaxFileSizeMB: 20,
|
||||
}
|
||||
|
||||
func TestConfiguration_ToJson(t *testing.T) {
|
||||
test.IsEqualBool(t, strings.Contains(string(testConfig.ToJson()), "\"SaltAdmin\": \"saltadmin\""), true)
|
||||
}
|
||||
|
||||
func TestConfiguration_ToString(t *testing.T) {
|
||||
test.IsEqualString(t, testConfig.ToString(), exptectedUnidentedOutput)
|
||||
}
|
||||
|
||||
const exptectedUnidentedOutput = "{\"Authentication\":{\"Method\":0,\"SaltAdmin\":\"saltadmin\",\"SaltFiles\":\"saltfiles\",\"Username\":\"admin\",\"Password\":\"adminpwhashed\",\"HeaderKey\":\"\",\"OauthProvider\":\"\",\"OAuthClientId\":\"\",\"OAuthClientSecret\":\"\",\"HeaderUsers\":null,\"OauthUsers\":null},\"Port\":\":12345\",\"ServerUrl\":\"https://testserver.com/\",\"RedirectUrl\":\"https://test.com\",\"ConfigVersion\":11,\"LengthId\":5,\"DataDir\":\"test\",\"MaxMemory\":50,\"UseSsl\":true,\"MaxFileSizeMB\":20}"
|
||||
Reference in New Issue
Block a user