Skip to content

Commit

Permalink
feat(paginate-ws): Use a Struct to parse the subscribe message
Browse files Browse the repository at this point in the history
  • Loading branch information
etu committed Jul 4, 2024
1 parent cb081e0 commit 089d2a5
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type ServeMessageResponse struct {
Message string `json:"message"`
}

type ServerSubscribeMessage struct {
Name string `json:"name"`
}

func NewServe(config *Config, runner *Runner) *Serve {
return &Serve{
config: config,
Expand Down Expand Up @@ -254,23 +258,22 @@ func (serve *Serve) newRouter() *mux.Router {
}

// Parse the subscription message
var subscription map[string]string
var subscription ServerSubscribeMessage
if err := json.Unmarshal(message, &subscription); err != nil {
log.Println("Unmarshal:", err)
continue
}

if name, ok := subscription["name"]; ok {
serve.clientSubscriptions[conn] = name
serve.clientSubscriptions[conn] = subscription.Name

// Send initial state for the subscribed server
serverState := serve.GetServerLogs(name)
// Send initial state for the subscribed server
serverState := serve.GetServerLogs(subscription.Name)

// Only send logs if there are any
if len(serverState.Logs) > 0 {
serve.sendMessage(conn, serverState)
}
// Only send logs if there are any
if len(serverState.Logs) > 0 {
serve.sendMessage(conn, serverState)
}

}
}()

Expand Down

0 comments on commit 089d2a5

Please sign in to comment.