diff --git a/API.md b/API.md index e94855a..6d90d67 100644 --- a/API.md +++ b/API.md @@ -23,6 +23,12 @@ Content-Type: application/json DELETE /api/config/server/:name ``` +## Get all servers configuration + +```http +GET /api/config/server +``` + ## Start a server ```http diff --git a/cli.go b/cli.go index 3127fa1..cf344e5 100644 --- a/cli.go +++ b/cli.go @@ -20,13 +20,15 @@ type Cli struct { } func (cli *Cli) List(format string) { - var state ServeFullState + var state map[string]ServerConfig + var runningState ServerItemList // Build URL based on config - requestUrl := fmt.Sprintf("http://%s:%d/api/state", cli.config.Settings.ListenAddress, cli.config.Settings.ListenPort) + requestUrl := fmt.Sprintf("http://%s:%d/api/config/server", cli.config.Settings.ListenAddress, cli.config.Settings.ListenPort) // Do request to running instance of program res, err := http.Get(requestUrl) + defer res.Body.Close() if err != nil { log.Printf("Failed to connect to running instance of program: %s\n", err) @@ -40,23 +42,46 @@ func (cli *Cli) List(format string) { } // Read the body content - defer res.Body.Close() body, _ := io.ReadAll(res.Body) // Parse the json json.Unmarshal(body, &state) + // Build URL based on config + requestUrl = fmt.Sprintf("http://%s:%d/api/state", cli.config.Settings.ListenAddress, cli.config.Settings.ListenPort) + + // Get the state + res, err = http.Get(requestUrl) + defer res.Body.Close() + + if err != nil { + log.Printf("Failed to connect to running instance of program: %s\n", err) + os.Exit(1) + } + + // Validate status code + if res.StatusCode != http.StatusOK { + log.Printf("Unexpected status code when fetching active config: %d\n", res.StatusCode) + os.Exit(2) + } + + // Read the body content + body, _ = io.ReadAll(res.Body) + + // Parse the json + json.Unmarshal(body, &runningState) + switch format { case "table": output := table.NewWriter() output.SetOutputMirror(os.Stdout) output.AppendHeader(table.Row{"Name", "Running", "Directory", "Command"}) - for _, val := range state.Config.Servers { + for _, val := range state { isRunning := false - if _, ok := state.RunnerState[val.Name]; ok { - isRunning = true + if _, ok := runningState.Servers[val.Name]; ok { + isRunning = runningState.Servers[val.Name].IsRunning } output.AppendRow([]interface{}{val.Name, isRunning, val.Directory, val.Command}) @@ -70,11 +95,11 @@ func (cli *Cli) List(format string) { output.Write([]string{"Name", "Running", "Directory", "Command"}) - for _, val := range state.Config.Servers { + for _, val := range state { isRunning := false - if _, ok := state.RunnerState[val.Name]; ok { - isRunning = true + if _, ok := runningState.Servers[val.Name]; ok { + isRunning = runningState.Servers[val.Name].IsRunning } output.Write([]string{val.Name, fmt.Sprintf("%t", isRunning), val.Directory, val.Command}) diff --git a/serve.go b/serve.go index 5da8202..c03e079 100644 --- a/serve.go +++ b/serve.go @@ -152,6 +152,12 @@ func (serve *Serve) newRouter() *mux.Router { json.NewEncoder(w).Encode(resp) }).Methods(http.MethodDelete) + // Method to fetch all servers configurations + router.HandleFunc("/api/config/server", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(serve.config.Servers) + }).Methods(http.MethodGet) + // // Endpoints to manage running state of servers //