Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func corsMiddleware(handler http.Handler) http.Handler {
return
}
}
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
handler.ServeHTTP(w, r)
})
}
Expand Down
12 changes: 12 additions & 0 deletions microservices/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func NewDashboard(tracker *tracker.Tracker) *Dashboard {
}

func (d *Dashboard) HandleDashboard(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}

if r.Method == http.MethodOptions {
w.Header().Del("Access-Control-Allow-Methods")
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.WriteHeader(http.StatusOK)
return
}

w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Refresh", "5")
clients := d.tracker.GetServers()
Expand Down
12 changes: 12 additions & 0 deletions microservices/homepage/homepage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func NewHomepage() *Homepage {
}

func (h *Homepage) HandleHomepage(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}

if r.Method == http.MethodOptions {
w.Header().Del("Access-Control-Allow-Methods")
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.WriteHeader(http.StatusOK)
return
}

homepageTempl().Render(r.Context(), w)
}

Expand Down
35 changes: 25 additions & 10 deletions microservices/webdavservice/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ type WebDavService struct {
}

func (s *WebDavService) RegisterGinHandlers(router *gin.Engine) {
h := &webdav.Handler{
Prefix: "/dav",
FileSystem: s.fs,
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Printf("webdav: %s %s: %v", r.Method, r.URL.Path, err)
} else {
log.Printf("webdav: %s %s", r.Method, r.URL.Path)
}
h := corsWebdavMethodFixerWrapper{
W: &webdav.Handler{
Prefix: "/dav",
FileSystem: s.fs,
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Printf("webdav: %s %s: %v", r.Method, r.URL.Path, err)
} else {
log.Printf("webdav: %s %s", r.Method, r.URL.Path)
}
},
},
}

Expand All @@ -40,3 +42,16 @@ func (s *WebDavService) RegisterGinHandlers(router *gin.Engine) {
router.Handle(method, "/dav/*filepath", gin.WrapH(h))
}
}

type corsWebdavMethodFixerWrapper struct {
W *webdav.Handler
}

// ServeHTTP implements [http.Handler].
func (c corsWebdavMethodFixerWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Del("Access-Control-Allow-Methods")
w.Header().Set("Access-Control-Allow-Methods", "COPY, DELETE, GET, HEAD, LOCK, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, TRACE, UNLOCK")
c.W.ServeHTTP(w, r)
}

var _ http.Handler = corsWebdavMethodFixerWrapper{}