Skip to content

Commit

Permalink
feat(serve): Break the API and return a simpler overview of the state
Browse files Browse the repository at this point in the history
  • Loading branch information
etu committed Jun 9, 2024
1 parent 4a94265 commit a3145e3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ POST /api/runner/:name
DELETE /api/runner/:name
```

## Fetch full state about all servers
## Fetch overview of state of all servers

```http
GET /api/state
Expand Down
80 changes: 65 additions & 15 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main // import "github.com/TaserudConsulting/goprocmgr"
import (
"embed"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
Expand All @@ -23,6 +24,18 @@ type ServeFullState struct {
RunnerState map[string]ServeRunnerResponseItem `json:"runners"`
}

type ServerItem struct {
Name string `json:"name"`
IsRunning bool `json:"is_running"`
Port uint `json:"port"`
StdoutCount uint `json:"stdout_count"`
StderrCount uint `json:"stderr_count"`
}

type ServerItemList struct {
Servers map[string]ServerItem `json:"servers"`
}

type ServeMessageResponse struct {
Message string `json:"message"`
}
Expand Down Expand Up @@ -177,24 +190,11 @@ func (serve *Serve) newRouter() *mux.Router {
}).Methods(http.MethodDelete)

//
// Endpoint to fetch the full state
// Endpoint to fetch an overview of the state of all servers
//
router.HandleFunc("/api/state", func(w http.ResponseWriter, r *http.Request) {
var state = ServeFullState{
Config: serve.config,
RunnerState: make(map[string]ServeRunnerResponseItem),
}

for key, value := range serve.runner.ActiveProcesses {
state.RunnerState[key] = ServeRunnerResponseItem{
Name: key,
Port: value.Port,
Logs: value.Logs,
}
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(state)
json.NewEncoder(w).Encode(serve.GetServerList())
}).Methods(http.MethodGet)

//
Expand Down Expand Up @@ -250,3 +250,53 @@ func (serve *Serve) newRouter() *mux.Router {

return router
}

func (serve *Serve) GetServer(name string) (ServerItem, error) {
var serverItem ServerItem

// Check if name is a valid entry in serve.config.Servers, if
// it isn't, return error.
if _, ok := serve.config.Servers[name]; ok == false {
return serverItem, errors.New("Undefined server requested '" + name + "'")
}

serverItem.Name = name
serverItem.IsRunning = false

if serve.runner.ActiveProcesses[name] != nil {
serverItem.IsRunning = true
serverItem.Port = serve.runner.ActiveProcesses[name].Port

// Count the logs for each server by output
for _, logEntry := range serve.runner.ActiveProcesses[name].Logs {
if logEntry.Output == "stdout" {
serverItem.StdoutCount++
} else if logEntry.Output == "stderr" {
serverItem.StderrCount++
}
}
}

return serverItem, nil
}

func (serve *Serve) GetServerList() ServerItemList {
servers := ServerItemList{
Servers: make(map[string]ServerItem),
}

// Go through all configured servers
for serverName := range serve.config.Servers {
server, err := serve.GetServer(serverName)

if err != nil {
log.Println(err)
continue
}

// Add the server to the list
servers.Servers[serverName] = server
}

return servers
}

0 comments on commit a3145e3

Please sign in to comment.