Do a little cleanup on the api

This commit is contained in:
Jon Staab
2026-04-22 13:52:09 -07:00
parent aa0eba1fbe
commit 081c4765ed
4 changed files with 11 additions and 28 deletions
+7 -7
View File
@@ -121,12 +121,7 @@ func (api *APIHandler) resolveRelayMembers(id string) ([]string, error) {
return members, nil
}
configPath := api.configPath(id)
if err := api.checkConfigExists(configPath); err != nil {
return nil, err
}
instance, err := MakeInstanceFromPath(configPath)
instance, err := MakeInstance(api.configName(id))
if err != nil {
return nil, err
}
@@ -407,9 +402,14 @@ func (api *APIHandler) deleteRelay(w http.ResponseWriter, r *http.Request, id st
writeJSON(w, http.StatusOK, map[string]string{"message": "relay deleted successfully"})
}
// configName returns the config file name
func (api *APIHandler) configName(id string) string {
return id+".toml"
}
// configPath returns the full path for a config file
func (api *APIHandler) configPath(id string) string {
return filepath.Join(api.configDir, id+".toml")
return filepath.Join(api.configDir, api.configName(id))
}
// checkConfigExists checks if a config file exists
-1
View File
@@ -68,7 +68,6 @@ func LoadConfig(filename string) (*Config, error) {
}
func LoadConfigFromPath(path string) (*Config, error) {
var config Config
if _, err := toml.DecodeFile(path, &config); err != nil {
return nil, fmt.Errorf("Failed to parse config file %s: %w", path, err)
-9
View File
@@ -31,15 +31,6 @@ func MakeInstance(filename string) (*Instance, error) {
return makeInstance(config, filename)
}
func MakeInstanceFromPath(path string) (*Instance, error) {
config, err := LoadConfigFromPath(path)
if err != nil {
return nil, err
}
return makeInstance(config, path)
}
func makeInstance(config *Config, source string) (*Instance, error) {
relay := khatru.NewRelay()
+4 -11
View File
@@ -28,15 +28,6 @@ func Dispatch(hostname string) (*Instance, bool) {
return instance, exists
}
func cleanupIfInactive(instance *Instance) bool {
if instance != nil && instance.Config != nil && instance.Config.Inactive {
instance.Cleanup()
return true
}
return false
}
func Start() {
dataDir := Env("DATA")
if err := os.MkdirAll(dataDir, 0755); err != nil {
@@ -72,7 +63,8 @@ func Start() {
if err != nil {
log.Printf("Failed to make instance for %s: %v", entry.Name(), err)
} else if cleanupIfInactive(instance) {
} else if instance.Config.Inactive {
instance.Cleanup()
log.Printf("Skipped inactive %s", entry.Name())
} else {
instancesByHost[instance.Config.Host] = instance
@@ -119,7 +111,8 @@ func Start() {
instance, err := MakeInstance(filename)
if err != nil {
log.Printf("Failed to reload %s: %v", filename, err)
} else if cleanupIfInactive(instance) {
} else if instance.Config.Inactive {
instance.Cleanup()
log.Printf("Skipped inactive %s", filename)
} else {
instancesByHost[instance.Config.Host] = instance