2024-09-18 07:40:50 +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"
|
2024-09-11 07:33:22 +02:00
|
|
|
"slices"
|
2024-08-28 07:21:02 +02:00
|
|
|
"strings"
|
2024-08-22 07:18:50 +02:00
|
|
|
"time"
|
2024-09-18 07:55:14 +02:00
|
|
|
|
2024-09-20 07:09:30 +02:00
|
|
|
"go-mod.ewintr.nl/planner/item"
|
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-09-18 07:55:14 +02:00
|
|
|
ks := make([]item.Kind, 0)
|
2024-09-11 07:33:22 +02:00
|
|
|
ksStr := r.URL.Query().Get("ks")
|
|
|
|
if ksStr != "" {
|
|
|
|
for _, k := range strings.Split(ksStr, ",") {
|
2024-09-18 07:55:14 +02:00
|
|
|
if !slices.Contains(item.KnownKinds, item.Kind(k)) {
|
2024-09-11 07:33:22 +02:00
|
|
|
msg := fmt.Sprintf("unknown kind: %s", k)
|
|
|
|
http.Error(w, fmtError(msg), http.StatusBadRequest)
|
|
|
|
s.logger.Info(msg)
|
|
|
|
return
|
|
|
|
}
|
2024-09-18 07:55:14 +02:00
|
|
|
ks = append(ks, item.Kind(k))
|
2024-09-11 07:33:22 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-28 07:21:02 +02:00
|
|
|
|
2024-09-11 07:33:22 +02:00
|
|
|
items, err := s.syncer.Updated(ks, timestamp)
|
2024-08-28 07:21:02 +02:00
|
|
|
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))
|
2025-01-05 12:46:01 +01:00
|
|
|
s.logger.Info("served sync get", "count", len(items), "remoteAddr", r.RemoteAddr)
|
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-18 07:55:14 +02:00
|
|
|
var items []item.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
|
|
|
|
}
|
|
|
|
|
2024-09-18 07:55:14 +02:00
|
|
|
for _, it := range items {
|
|
|
|
if it.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
|
|
|
|
}
|
2024-09-18 07:55:14 +02:00
|
|
|
if it.Kind == "" {
|
|
|
|
msg := fmt.Sprintf("item %s does not have a kind", it.ID)
|
2024-09-09 07:56:01 +02:00
|
|
|
http.Error(w, fmtError(msg), http.StatusBadRequest)
|
|
|
|
s.logger.Info(msg)
|
2024-09-09 07:41:34 +02:00
|
|
|
return
|
|
|
|
}
|
2024-09-18 07:55:14 +02:00
|
|
|
if !slices.Contains(item.KnownKinds, it.Kind) {
|
|
|
|
msg := fmt.Sprintf("items %s does not have a know kind", it.ID)
|
2024-09-11 07:33:22 +02:00
|
|
|
http.Error(w, fmtError(msg), http.StatusBadRequest)
|
|
|
|
s.logger.Info(msg)
|
|
|
|
return
|
|
|
|
}
|
2024-09-18 07:55:14 +02:00
|
|
|
if it.Body == "" {
|
|
|
|
msg := fmt.Sprintf(`{"error":"item %s does not have a body"}`, it.ID)
|
2024-09-09 07:56:01 +02:00
|
|
|
http.Error(w, msg, http.StatusBadRequest)
|
|
|
|
s.logger.Info(msg)
|
2024-09-09 07:41:34 +02:00
|
|
|
return
|
|
|
|
}
|
2024-12-01 10:22:47 +01:00
|
|
|
if err := s.syncer.Update(it, time.Now()); 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)
|
|
|
|
|
2025-01-05 12:46:01 +01:00
|
|
|
s.logger.Info("served sync post", "count", len(items), "remoteAddr", r.RemoteAddr)
|
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
|
|
|
}
|