Do a little cleanup on the api
This commit is contained in:
+7
-7
@@ -121,12 +121,7 @@ func (api *APIHandler) resolveRelayMembers(id string) ([]string, error) {
|
|||||||
return members, nil
|
return members, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
configPath := api.configPath(id)
|
instance, err := MakeInstance(api.configName(id))
|
||||||
if err := api.checkConfigExists(configPath); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
instance, err := MakeInstanceFromPath(configPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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"})
|
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
|
// configPath returns the full path for a config file
|
||||||
func (api *APIHandler) configPath(id string) string {
|
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
|
// checkConfigExists checks if a config file exists
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ func LoadConfig(filename string) (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfigFromPath(path string) (*Config, error) {
|
func LoadConfigFromPath(path string) (*Config, error) {
|
||||||
|
|
||||||
var config Config
|
var config Config
|
||||||
if _, err := toml.DecodeFile(path, &config); err != nil {
|
if _, err := toml.DecodeFile(path, &config); err != nil {
|
||||||
return nil, fmt.Errorf("Failed to parse config file %s: %w", path, err)
|
return nil, fmt.Errorf("Failed to parse config file %s: %w", path, err)
|
||||||
|
|||||||
@@ -31,15 +31,6 @@ func MakeInstance(filename string) (*Instance, error) {
|
|||||||
return makeInstance(config, filename)
|
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) {
|
func makeInstance(config *Config, source string) (*Instance, error) {
|
||||||
relay := khatru.NewRelay()
|
relay := khatru.NewRelay()
|
||||||
|
|
||||||
|
|||||||
+4
-11
@@ -28,15 +28,6 @@ func Dispatch(hostname string) (*Instance, bool) {
|
|||||||
return instance, exists
|
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() {
|
func Start() {
|
||||||
dataDir := Env("DATA")
|
dataDir := Env("DATA")
|
||||||
if err := os.MkdirAll(dataDir, 0755); err != nil {
|
if err := os.MkdirAll(dataDir, 0755); err != nil {
|
||||||
@@ -72,7 +63,8 @@ func Start() {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to make instance for %s: %v", entry.Name(), err)
|
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())
|
log.Printf("Skipped inactive %s", entry.Name())
|
||||||
} else {
|
} else {
|
||||||
instancesByHost[instance.Config.Host] = instance
|
instancesByHost[instance.Config.Host] = instance
|
||||||
@@ -119,7 +111,8 @@ func Start() {
|
|||||||
instance, err := MakeInstance(filename)
|
instance, err := MakeInstance(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to reload %s: %v", filename, err)
|
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)
|
log.Printf("Skipped inactive %s", filename)
|
||||||
} else {
|
} else {
|
||||||
instancesByHost[instance.Config.Host] = instance
|
instancesByHost[instance.Config.Host] = instance
|
||||||
|
|||||||
Reference in New Issue
Block a user