48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"render231011/internal/thestore"
|
|
"strings"
|
|
)
|
|
|
|
type Server struct {
|
|
store *thestore.Store
|
|
}
|
|
|
|
func NewServer() Server {
|
|
return Server{
|
|
store: thestore.NewStore(),
|
|
}
|
|
}
|
|
|
|
func (server Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
// todo middleware: rate limit
|
|
// todo middleware: timeout requests
|
|
// todo: replace with an actual router
|
|
if strings.HasPrefix(r.URL.Path, "/event") && r.Method == http.MethodPost {
|
|
server.servePostEvent(w, r)
|
|
} else if r.URL.Path == "/events" && r.Method == http.MethodGet {
|
|
server.serveGetEvents(w, r)
|
|
}
|
|
}
|
|
|
|
func (server Server) servePostEvent(w http.ResponseWriter, r *http.Request) {
|
|
var newEvent thestore.Event
|
|
if err := json.NewDecoder(r.Body).Decode(&newEvent); err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
// todo error msg pls
|
|
return
|
|
}
|
|
|
|
// todo check if action==updated service-id already exists
|
|
server.store.Push(newEvent)
|
|
log.Printf("ingested event %+v", newEvent)
|
|
}
|
|
|
|
func (server Server) serveGetEvents(w http.ResponseWriter, r *http.Request) {
|
|
// todo my circular queue magic
|
|
}
|