mirror of
https://github.com/absmach/supermq.git
synced 2026-06-23 07:20:19 +00:00
SMQ-2801 - Add health check endpoint for CoAP adapter (#3022)
Continuous Delivery / Build and Push (push) Has been cancelled
Check the consistency of generated files / check-generated-files (push) Has been cancelled
Check License Header / check-license (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
Continuous Delivery / Build and Push (push) Has been cancelled
Check the consistency of generated files / check-generated-files (push) Has been cancelled
Check License Header / check-license (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
Signed-off-by: Felix Gateru <felix.gateru@gmail.com>
This commit is contained in:
@@ -4,7 +4,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -72,6 +74,17 @@ func (h *CoAPHandler) ServeCOAP(w mux.ResponseWriter, m *mux.Message) {
|
||||
}
|
||||
defer h.sendResp(w, resp)
|
||||
|
||||
path, err := m.Path()
|
||||
if err != nil {
|
||||
h.logger.Warn(fmt.Sprintf("Error reading path: %s", err))
|
||||
resp.SetCode(codes.BadRequest)
|
||||
return
|
||||
}
|
||||
if strings.TrimPrefix(path, "/") == messaging.HealthTopicPrefix {
|
||||
h.handleHealthCheck(w, m)
|
||||
return
|
||||
}
|
||||
|
||||
msg, err := h.decodeMessage(m)
|
||||
if err != nil {
|
||||
h.logger.Warn(fmt.Sprintf("Error decoding message: %s", err))
|
||||
@@ -174,6 +187,25 @@ func (h *CoAPHandler) sendResp(w mux.ResponseWriter, resp *pool.Message) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *CoAPHandler) handleHealthCheck(w mux.ResponseWriter, m *mux.Message) {
|
||||
pd := messaging.HealthInfo{
|
||||
Status: messaging.StatusOK,
|
||||
Protocol: protocol,
|
||||
Timestamp: time.Now().UTC(),
|
||||
}
|
||||
pdBytes, err := json.Marshal(pd)
|
||||
if err != nil {
|
||||
h.logger.Warn(fmt.Sprintf("Error marshaling health info: %s", err))
|
||||
}
|
||||
resp := w.Conn().AcquireMessage(m.Context())
|
||||
defer w.Conn().ReleaseMessage(resp)
|
||||
resp.SetCode(codes.Content)
|
||||
resp.SetToken(m.Token())
|
||||
resp.SetContentFormat(message.AppJSON)
|
||||
resp.SetBody(bytes.NewReader(pdBytes))
|
||||
h.sendResp(w, resp)
|
||||
}
|
||||
|
||||
func parseKey(msg *mux.Message) (string, error) {
|
||||
authKey, err := msg.Options().GetString(message.URIQuery)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) Abstract Machines
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package messaging
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
HealthTopicPrefix = "hc"
|
||||
StatusOK = "ok"
|
||||
)
|
||||
|
||||
type HealthInfo struct {
|
||||
// Status contains adapter status.
|
||||
Status string `json:"status"`
|
||||
|
||||
// Protocol contains the protocol used.
|
||||
Protocol string `json:"protocol"`
|
||||
|
||||
// Timestamp of health check.
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
Reference in New Issue
Block a user