tbh i forgot

This commit is contained in:
Maximilian Wagner
2025-12-27 19:22:51 +01:00
parent 9e65bd3916
commit b600ec5267
6 changed files with 43 additions and 48 deletions

View File

@@ -81,6 +81,14 @@ func VerifyLogRequest(request *http.Request) (err error) {
return nil
}
// Writes a configuration file populated with defaults
func createConfigWithDefaults(path string) error {
config, _ := defaultConfig.ReadFile("default_config.json")
err := os.WriteFile(path, config, 0644)
return err
}
// Helper for configReader() that does the actual reading
//
// Also validates the populated fields
@@ -88,7 +96,7 @@ func configReaderParse(filePath string, configOptionStorage *globals.Config) err
jsonData, err := os.ReadFile(filePath)
if err == nil {
if err = json.Unmarshal(jsonData, &configOptionStorage); err != nil {
LogLine("Configuration file is invalid JSON", 5)
LogLine("Configuration file is invalid JSON", 4)
return errors.New("Config file could not be read")
}
} else {
@@ -112,17 +120,10 @@ func configReaderParse(filePath string, configOptionStorage *globals.Config) err
return nil
}
// tbd what exactly happens here
func configValidator(config globals.Config) error {
return nil
}
func configPopulator(config globals.Config) error {
return nil
}
// Creates the configuration file populated with defaults
func createConfigWithDefaults() {
// populates the appconfig with default values
func configPopulator(configOptionStorage *globals.Config) {
jsonData, _ := defaultConfig.ReadFile("default_config.json")
json.Unmarshal(jsonData, &configOptionStorage)
}
// Reads config file and stores the options in configOptionStorage
@@ -132,29 +133,39 @@ func createConfigWithDefaults() {
// %LOCALAPPDATA%\servtex\configFileName
// ~/.config/servtex/configFileName
func ConfigReader(configFileName string, configOptionStorage *globals.Config) error {
var defaultPath string
// populate default config
configPopulator(configOptionStorage)
configRead := false
// read config file from disk
exePath, err := os.Executable()
if err == nil {
path := filepath.Join(filepath.Dir(exePath), configFileName)
err = configReaderParse(path, configOptionStorage)
if err == nil { return nil }
defaultPath = filepath.Join(filepath.Dir(exePath), configFileName)
err = configReaderParse(defaultPath, configOptionStorage)
if err == nil { configRead = true }
}
localappdata := os.Getenv("LOCALAPPDATA")
if localappdata != "" {
if !configRead && localappdata != "" {
path := filepath.Join(localappdata, "servtex", configFileName)
err = configReaderParse(path, configOptionStorage)
if err == nil { return nil }
}
path := filepath.Join("~", ".config", "servtex", configFileName)
err = configReaderParse(path, configOptionStorage)
if err != nil { return err }
if !configRead {
path := filepath.Join("~", ".config", "servtex", configFileName)
err = configReaderParse(path, configOptionStorage)
err = configPopulator(*configOptionStorage)
if err != nil { return err }
err = configValidator(*configOptionStorage)
if err != nil { return err }
// create default config file for user to edit
if err != nil && defaultPath != "" {
LogLine(fmt.Sprintf("Configuration file does not exist. Creating with defaults at %s", defaultPath), 4)
err = createConfigWithDefaults(defaultPath)
if err != nil {
LogLine("Configuration file could not be created", 4)
}
}
}
return nil
}
@@ -286,18 +297,6 @@ func LatexCompile() error {
return nil
}
// Returns intersection of two slices
func sliceIntersection[T comparable](left []T, right []T) (intersection []T) {
for _, leftItem := range left {
for _, rightItem := range right {
if leftItem == rightItem {
intersection = append(intersection, leftItem)
}
}
}
return intersection
}
// Checks whether the proxy is trusted. Returns trusted status and the proxy.
//
// If X-Forwarded-For chain is passed, only the last in chain will be considered

View File

@@ -1,5 +1,5 @@
{
"timezone": "Europe/Berlin"
"timezone": "Europe/Berlin",
"logFilePath": "./servtex.log",
"logLevel": "warning",