Files
threshold/internal/clientcache/internal/daemon/log_handler.go
T
shadowdaoandClaude Sonnet 5.5 962d708fdd Rename Go module paths to repo.anhonesthost.net/CyberCoveLLC/threshold
Rename root, api and sdk module paths (imports, go.mod, go_package,
ldflags, build scripts, buf configs) and regenerate protobuf/gateway
code with the project's pinned toolchain. Wire identifiers unchanged.
api gets a local replace for sdk; root/api/sdk require v0.1.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 10:33:41 -07:00

51 lines
1.4 KiB
Go

// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: BUSL-1.1
package daemon
import (
"context"
"encoding/json"
"io"
"net/http"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/event"
)
// LogRequest is the request body to this handler.
type LogRequest struct {
// Message is a required field for all requests
Message string `json:"message,omitempty"`
Op string `json:"op,omitempty"`
}
// newLogHandlerFunc creates a handler that logs a system event using the
// daemon's eventer.
func newLogHandlerFunc(ctx context.Context) (http.HandlerFunc, error) {
const op = "daemon.newLogHandlerFunc"
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeError(w, "only method POST allowed", http.StatusMethodNotAllowed)
}
var perReq LogRequest
data, err := io.ReadAll(r.Body)
if err != nil {
writeError(w, "unable to read request body", http.StatusBadRequest)
return
}
if err := json.Unmarshal(data, &perReq); err != nil {
// If, for whatever reason, we can't parse the request body as json
// can still log that the request to log was received and print out
// the body of the request.
event.WriteError(ctx, op, err, event.WithInfo("body", string(data)))
writeError(w, "unable to parse request body", http.StatusBadRequest)
return
}
event.WriteSysEvent(ctx, op, perReq.Message, "requester_op", perReq.Op)
w.WriteHeader(http.StatusNoContent)
}, nil
}