wfc-server/api/motd.go
ppeb 41f7fddc4d
Revert "Revert motd, clear, and user action response"
This reverts commit 3144bd7cf8.

The reason this commit had to be made was because certain apis were PR'd
upstream, but were unwanted and thus removed in a separate commit. It
would likely have been better to drop the commits pushed upstream but
it's too late to fix that, so now we have a revert of a revert.
2025-03-05 20:33:20 -06:00

90 lines
2.1 KiB
Go

package api
import (
"encoding/json"
"io"
"net/http"
"strconv"
"wwfc/gpcm"
)
func HandleMotd(w http.ResponseWriter, r *http.Request) {
var motd string
var success bool
var err string
var statusCode int
if r.Method == http.MethodPost {
success, err, statusCode = handleMotdImpl(r)
} else if r.Method == http.MethodGet {
_motd, motdErr := gpcm.GetMessageOfTheDay()
if motdErr != nil {
err = motdErr.Error()
statusCode = http.StatusInternalServerError
} else {
motd = _motd
success = true
statusCode = http.StatusOK
}
} else if r.Method == http.MethodOptions {
statusCode = http.StatusNoContent
w.Header().Set("Access-Control-Allow-Methods", "POST, GET")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
} else {
err = "Incorrect request. POST and GET only."
statusCode = http.StatusMethodNotAllowed
w.Header().Set("Allow", "POST, GET")
}
w.Header().Set("Access-Control-Allow-Origin", "*")
var jsonData []byte
if statusCode != http.StatusNoContent {
w.Header().Set("Content-Type", "application/json")
jsonData, _ = json.Marshal(MotdResponse{motd, success, err})
}
w.Header().Set("Content-Length", strconv.Itoa(len(jsonData)))
w.WriteHeader(statusCode)
w.Write(jsonData)
}
type MotdRequestSpec struct {
Secret string
Motd string
}
type MotdResponse struct {
Motd string
Success bool
Error string
}
func handleMotdImpl(r *http.Request) (bool, string, int) {
// TODO: Actual authentication rather than a fixed secret
body, err := io.ReadAll(r.Body)
if err != nil {
return false, "Unable to read request body", http.StatusBadRequest
}
var req MotdRequestSpec
err = json.Unmarshal(body, &req)
if err != nil {
return false, err.Error(), http.StatusBadRequest
}
if apiSecret == "" || req.Secret != apiSecret {
return false, "Invalid API secret in request", http.StatusUnauthorized
}
err = gpcm.SetMessageOfTheDay(req.Motd)
if err != nil {
return false, err.Error(), http.StatusInternalServerError
}
// Don't return empty JSON, this is placeholder for now.
return true, "", http.StatusOK
}