mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 16:34:30 +01:00
http: clean up Bind to better use middleware
This commit is contained in:
parent
e489a101f6
commit
ab7dfe0c87
@ -12,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/rclone/rclone/cmd"
|
"github.com/rclone/rclone/cmd"
|
||||||
"github.com/rclone/rclone/cmd/serve/http/data"
|
"github.com/rclone/rclone/cmd/serve/http/data"
|
||||||
"github.com/rclone/rclone/fs"
|
"github.com/rclone/rclone/fs"
|
||||||
@ -67,7 +68,7 @@ control the stats printing.
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Bind("/*", router)
|
s.Bind(router)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@ -93,18 +94,17 @@ func newServer(f fs.Fs, templatePath string) *server {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Bind(pattern string, router chi.Router) {
|
func (s *server) Bind(router chi.Router) {
|
||||||
router.Get(pattern, s.handler)
|
router.Use(
|
||||||
router.Head(pattern, s.handler)
|
middleware.SetHeader("Accept-Ranges", "bytes"),
|
||||||
|
middleware.SetHeader("Server", "rclone/"+fs.Version),
|
||||||
|
)
|
||||||
|
router.Get("/*", s.handler)
|
||||||
|
router.Head("/*", s.handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handler reads incoming requests and dispatches them
|
// handler reads incoming requests and dispatches them
|
||||||
func (s *server) handler(w http.ResponseWriter, r *http.Request) {
|
func (s *server) handler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Accept-Ranges", "bytes")
|
|
||||||
w.Header().Set("Server", "rclone/"+fs.Version)
|
|
||||||
|
|
||||||
//rctx := chi.RouteContext(r.Context())
|
|
||||||
|
|
||||||
isDir := strings.HasSuffix(r.URL.Path, "/")
|
isDir := strings.HasSuffix(r.URL.Path, "/")
|
||||||
remote := strings.Trim(r.URL.Path, "/")
|
remote := strings.Trim(r.URL.Path, "/")
|
||||||
if isDir {
|
if isDir {
|
||||||
|
@ -37,7 +37,7 @@ func startServer(t *testing.T, f fs.Fs) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err.Error())
|
t.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
httpServer.Bind("/*", router)
|
httpServer.Bind(router)
|
||||||
testURL = httplib.URL()
|
testURL = httplib.URL()
|
||||||
|
|
||||||
// try to connect to the test server
|
// try to connect to the test server
|
||||||
|
Loading…
Reference in New Issue
Block a user