jasmine/server.go

100 lines
2.5 KiB
Go
Raw Permalink Normal View History

2024-05-08 01:10:37 +01:00
package jasmine
import (
2024-05-11 02:33:13 +01:00
"git.sr.ht/~sircmpwn/tokidoki/storage"
2024-05-13 16:45:23 +01:00
"github.com/1f349/cardcaldav"
2024-05-11 02:33:13 +01:00
"github.com/emersion/go-webdav"
2024-05-08 01:10:37 +01:00
"github.com/emersion/go-webdav/caldav"
"net/http"
2024-05-11 02:33:13 +01:00
"path/filepath"
2024-05-08 01:10:37 +01:00
"time"
)
type Conf struct {
Listen string `json:"listen"`
2024-05-13 16:45:23 +01:00
DB string `json:"db"`
2024-05-08 01:10:37 +01:00
}
2024-05-11 02:33:13 +01:00
type jasmineHandler struct {
2024-05-14 11:12:37 +01:00
auth AuthProvider
2024-05-11 02:33:13 +01:00
backend caldav.Backend
}
func (j *jasmineHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
principlePath, err := j.auth.CurrentUserPrincipal(req.Context())
if err != nil {
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
var homeSets []webdav.BackendSuppliedHomeSet
path, err := j.backend.CalendarHomeSetPath(req.Context())
if err != nil {
http.Error(rw, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
homeSets = append(homeSets, caldav.NewCalendarHomeSet(path))
if req.URL.Path == principlePath {
opts := webdav.ServePrincipalOptions{
CurrentUserPrincipalPath: principlePath,
HomeSets: homeSets,
Capabilities: []webdav.Capability{
caldav.CapabilityCalendar,
},
}
webdav.ServePrincipal(rw, req, &opts)
return
}
if req.URL.Path == "/" {
http.Error(rw, "Jasmine API Endpoint", http.StatusOK)
return
}
http.NotFound(rw, req)
}
2024-05-14 11:12:37 +01:00
type AuthProvider interface {
cardcaldav.ProviderMiddleware
webdav.UserPrincipalBackend
}
2024-05-08 01:10:37 +01:00
func NewHttpServer(conf Conf, wd string) *http.Server {
2024-05-13 16:45:23 +01:00
cardcaldav.SetupLogger(Logger)
2024-05-14 11:12:37 +01:00
principle := NullAuth(cardcaldav.NewAuth(conf.DB, Logger))
2024-05-11 02:33:13 +01:00
calStorage, _, err := storage.NewFilesystem(filepath.Join(wd, "storage"), "/calendar/", "/contacts/", principle)
if err != nil {
Logger.Fatal("Failed to load storage backend", "err", err)
}
calHandler := &caldav.Handler{Backend: calStorage}
handler := &jasmineHandler{
auth: principle,
backend: calStorage,
2024-05-08 01:10:37 +01:00
}
2024-05-11 02:33:13 +01:00
r := http.NewServeMux()
r.Handle("/", handler)
r.Handle("/.well-known/caldav", calHandler)
r.Handle("/{user}/calendar/", calHandler)
r2 := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
t := time.Now()
r.ServeHTTP(rw, req)
td := time.Since(t)
Logger.Debug("Request", "method", req.Method, "url", req.URL.String(), "remote", req.RemoteAddr, "dur", td.String())
})
2024-05-08 01:10:37 +01:00
return &http.Server{
Addr: conf.Listen,
2024-05-11 02:33:13 +01:00
Handler: principle.Middleware(r2),
2024-05-08 01:10:37 +01:00
ReadTimeout: time.Minute,
ReadHeaderTimeout: time.Minute,
WriteTimeout: time.Minute,
IdleTimeout: time.Minute,
MaxHeaderBytes: 2500,
}
}