planner/handler/handler.go

127 lines
2.9 KiB
Go
Raw Normal View History

2024-08-21 16:35:44 +02:00
package handler
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-26 07:45:41 +02:00
"code.ewintr.nl/planner/planner"
2024-08-21 16:35:44 +02:00
"code.ewintr.nl/planner/storage"
)
2024-08-28 07:21:02 +02:00
type Server struct {
syncer storage.Syncer
2024-09-07 12:10:48 +02:00
apiKey string
2024-08-28 07:21:02 +02:00
logger *slog.Logger
}
2024-09-07 12:10:48 +02:00
func NewServer(syncer storage.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) {
http.Error(w, `{"error":"not authorized"}`, http.StatusUnauthorized)
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-07 12:10:48 +02:00
http.Error(w, `{"error":"not found"}`, http.StatusNotFound)
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 {
http.Error(w, err.Error(), http.StatusBadRequest)
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-07 12:10:48 +02:00
http.Error(w, fmtError(err), http.StatusInternalServerError)
2024-08-28 07:21:02 +02:00
return
}
body, err := json.Marshal(items)
if err != nil {
2024-09-07 12:10:48 +02:00
http.Error(w, fmtError(err), http.StatusInternalServerError)
2024-08-28 07:21:02 +02:00
return
}
fmt.Fprint(w, string(body))
}
func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
2024-09-07 12:10:48 +02:00
http.Error(w, fmtError(err), http.StatusBadRequest)
2024-08-28 07:21:02 +02:00
return
}
defer r.Body.Close()
var items []planner.Syncable
if err := json.Unmarshal(body, &items); err != nil {
2024-09-07 12:10:48 +02:00
http.Error(w, fmtError(err), http.StatusBadRequest)
2024-08-28 07:21:02 +02:00
return
}
for _, item := range items {
item.Updated = time.Now()
if err := s.syncer.Update(item); err != nil {
2024-09-07 12:10:48 +02:00
http.Error(w, fmtError(err), http.StatusInternalServerError)
2024-08-28 07:21:02 +02:00
return
}
}
w.WriteHeader(http.StatusNoContent)
}
// 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
func fmtError(err error) string {
return fmt.Sprintf(`{"error":%q}`, err.Error())
}