planner/sync-service/handler.go

160 lines
3.6 KiB
Go
Raw Normal View History

2024-09-08 10:09:54 +02:00
package main
2024-08-21 16:35:44 +02:00
import (
"encoding/json"
"fmt"
2024-08-26 07:45:41 +02:00
"io"
2024-08-28 07:21:02 +02:00
"log/slog"
2024-08-21 16:35:44 +02:00
"net/http"
2024-08-28 07:21:02 +02:00
"path"
"strings"
2024-08-22 07:18:50 +02:00
"time"
2024-08-21 16:35:44 +02:00
)
2024-08-28 07:21:02 +02:00
type Server struct {
2024-09-08 10:09:54 +02:00
syncer Syncer
2024-09-07 12:10:48 +02:00
apiKey string
2024-08-28 07:21:02 +02:00
logger *slog.Logger
}
2024-09-08 10:09:54 +02:00
func NewServer(syncer Syncer, apiKey string, logger *slog.Logger) *Server {
2024-08-28 07:21:02 +02:00
return &Server{
syncer: syncer,
2024-09-07 12:10:48 +02:00
apiKey: apiKey,
2024-08-28 07:21:02 +02:00
logger: logger,
}
2024-08-21 16:35:44 +02:00
}
2024-08-28 07:21:02 +02:00
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2024-09-07 12:10:48 +02:00
w.Header().Set("Content-Type", "application/json")
2024-08-28 07:21:02 +02:00
if r.URL.Path == "/" {
Index(w, r)
return
}
2024-09-07 12:10:48 +02:00
if r.Header.Get("Authorization") != fmt.Sprintf("Bearer %s", s.apiKey) {
2024-09-09 07:56:01 +02:00
msg := "not authorized"
http.Error(w, fmtError(msg), http.StatusUnauthorized)
s.logger.Info(msg)
2024-09-07 12:10:48 +02:00
return
}
2024-08-28 07:21:02 +02:00
head, tail := ShiftPath(r.URL.Path)
switch {
case head == "sync" && tail != "/":
2024-09-07 12:10:48 +02:00
http.Error(w, `{"error":"not found"}`, http.StatusNotFound)
2024-08-28 07:21:02 +02:00
case head == "sync" && r.Method == http.MethodGet:
s.SyncGet(w, r)
case head == "sync" && r.Method == http.MethodPost:
s.SyncPost(w, r)
default:
2024-09-09 07:56:01 +02:00
msg := "not found"
http.Error(w, fmtError(msg), http.StatusNotFound)
s.logger.Info(msg)
2024-08-28 07:21:02 +02:00
}
2024-08-26 07:45:41 +02:00
}
2024-08-28 07:21:02 +02:00
func (s *Server) SyncGet(w http.ResponseWriter, r *http.Request) {
timestamp := time.Time{}
tsStr := r.URL.Query().Get("ts")
if tsStr != "" {
var err error
if timestamp, err = time.Parse(time.RFC3339, tsStr); err != nil {
2024-09-09 07:56:01 +02:00
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
2024-08-28 07:21:02 +02:00
return
2024-08-26 07:45:41 +02:00
}
2024-08-23 10:52:17 +02:00
}
2024-08-28 07:21:02 +02:00
items, err := s.syncer.Updated(timestamp)
if err != nil {
2024-09-09 07:56:01 +02:00
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg)
2024-08-28 07:21:02 +02:00
return
}
body, err := json.Marshal(items)
if err != nil {
2024-09-09 07:56:01 +02:00
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg)
2024-08-28 07:21:02 +02:00
return
}
fmt.Fprint(w, string(body))
2024-09-08 11:17:49 +02:00
s.logger.Info("served get sync")
2024-08-28 07:21:02 +02:00
}
func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
2024-09-09 07:56:01 +02:00
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
2024-08-28 07:21:02 +02:00
return
}
defer r.Body.Close()
2024-09-09 07:36:05 +02:00
var items []Item
2024-08-28 07:21:02 +02:00
if err := json.Unmarshal(body, &items); err != nil {
2024-09-09 07:56:01 +02:00
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
2024-08-28 07:21:02 +02:00
return
}
for _, item := range items {
2024-09-09 07:41:34 +02:00
if item.ID == "" {
2024-09-09 07:56:01 +02:00
msg := "item without an id"
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
2024-09-09 07:41:34 +02:00
return
}
if item.Kind == "" {
2024-09-09 07:56:01 +02:00
msg := fmt.Sprintf("item %s does not have a kind", item.ID)
http.Error(w, fmtError(msg), http.StatusBadRequest)
s.logger.Info(msg)
2024-09-09 07:41:34 +02:00
return
}
if item.Body == "" {
2024-09-09 07:56:01 +02:00
msg := fmt.Sprintf(`{"error":"item %s does not have a body"}`, item.ID)
http.Error(w, msg, http.StatusBadRequest)
s.logger.Info(msg)
2024-09-09 07:41:34 +02:00
return
}
2024-08-28 07:21:02 +02:00
item.Updated = time.Now()
if err := s.syncer.Update(item); err != nil {
2024-09-09 07:56:01 +02:00
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg)
2024-08-28 07:21:02 +02:00
return
}
}
w.WriteHeader(http.StatusNoContent)
2024-09-08 11:17:49 +02:00
s.logger.Info("served get sync")
2024-08-28 07:21:02 +02:00
}
// ShiftPath splits off the first component of p, which will be cleaned of
// relative components before processing. head will never contain a slash and
// tail will always be a rooted path without trailing slash.
// See https://blog.merovius.de/posts/2017-06-18-how-not-to-use-an-http-router/
func ShiftPath(p string) (head, tail string) {
p = path.Clean("/" + p)
i := strings.Index(p[1:], "/") + 1
if i <= 0 {
return p[1:], "/"
}
return p[1:i], p[i:]
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"status":"ok"}`)
2024-08-21 16:35:44 +02:00
}
2024-09-07 12:10:48 +02:00
2024-09-09 07:56:01 +02:00
func fmtError(msg string) string {
return fmt.Sprintf(`{"error":%q}`, msg)
2024-09-07 12:10:48 +02:00
}