mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 08:23:47 +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"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/rclone/rclone/cmd"
|
||||
"github.com/rclone/rclone/cmd/serve/http/data"
|
||||
"github.com/rclone/rclone/fs"
|
||||
@ -67,7 +68,7 @@ control the stats printing.
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.Bind("/*", router)
|
||||
s.Bind(router)
|
||||
return nil
|
||||
})
|
||||
},
|
||||
@ -93,18 +94,17 @@ func newServer(f fs.Fs, templatePath string) *server {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *server) Bind(pattern string, router chi.Router) {
|
||||
router.Get(pattern, s.handler)
|
||||
router.Head(pattern, s.handler)
|
||||
func (s *server) Bind(router chi.Router) {
|
||||
router.Use(
|
||||
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
|
||||
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, "/")
|
||||
remote := strings.Trim(r.URL.Path, "/")
|
||||
if isDir {
|
||||
|
@ -37,7 +37,7 @@ func startServer(t *testing.T, f fs.Fs) {
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
httpServer.Bind("/*", router)
|
||||
httpServer.Bind(router)
|
||||
testURL = httplib.URL()
|
||||
|
||||
// try to connect to the test server
|
||||
|
Loading…
Reference in New Issue
Block a user