mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-04-21 00:37:33 -05:00
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.
90 lines
2.1 KiB
Go
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
|
|
}
|