justlog/api/admin.go

79 lines
1.8 KiB
Go
Raw Normal View History

2020-09-26 01:45:18 +02:00
package api
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gempir/justlog/config"
)
func (s *Server) authenticateAdmin(w http.ResponseWriter, r *http.Request) bool {
apiKey := r.Header.Get("X-Api-Key")
if apiKey == "" {
http.Error(w, "No I don't think so.", http.StatusForbidden)
return false
}
2020-09-27 12:24:42 +02:00
return apiKey == s.cfg.AdminAPIKey
2020-09-26 01:45:18 +02:00
}
2020-09-26 09:13:47 +02:00
type channelConfigsDeleteRequest struct {
MessageTypes bool `json:"messageTypes,omitempty"`
}
2020-09-26 01:45:18 +02:00
type channelConfigsRequest struct {
config.ChannelConfig
}
func (s *Server) writeConfig(w http.ResponseWriter, r *http.Request) {
2020-09-26 09:13:47 +02:00
if r.Method != http.MethodPost && r.Method != http.MethodDelete {
2020-09-26 01:45:18 +02:00
http.Error(w, "We'll see, we'll see. The winner gets tea.", http.StatusMethodNotAllowed)
return
}
channelID := strings.TrimPrefix(r.URL.String(), "/admin/channelConfigs/")
if _, ok := s.cfg.ChannelConfigs[channelID]; !ok {
http.Error(w, "Uhhhhhh... unkown channel", http.StatusBadRequest)
return
}
2020-09-26 09:13:47 +02:00
if r.Method == http.MethodDelete {
var request channelConfigsDeleteRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, "ANYWAYS: "+err.Error(), http.StatusBadRequest)
return
}
if request.MessageTypes {
s.cfg.ResetMessageTypes(channelID)
s.bot.UpdateMessageTypesToLog()
writeJSON("Doubters? Reset "+channelID+" messageTypes", http.StatusOK, w, r)
return
}
2020-09-26 01:45:18 +02:00
2020-09-26 09:13:47 +02:00
} else {
var request channelConfigsRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, "ANYWAYS: "+err.Error(), http.StatusBadRequest)
return
}
s.cfg.SetMessageTypes(channelID, request.MessageTypes)
s.bot.UpdateMessageTypesToLog()
writeJSON("Doubters? Updated "+channelID+" messageTypes to "+fmt.Sprintf("%v", request.MessageTypes), http.StatusOK, w, r)
2020-09-26 01:45:18 +02:00
return
}
2020-09-26 09:13:47 +02:00
http.Error(w, "Nothing here", http.StatusBadRequest)
return
2020-09-26 01:45:18 +02:00
}