Skip to content

Commit

Permalink
fix handling the root paths for serving files
Browse files Browse the repository at this point in the history
also adds a helper HandleFiles and a bunch of tests to make sure all works
  • Loading branch information
umputun committed Jan 3, 2025
1 parent 2d67c2f commit a9dbd75
Show file tree
Hide file tree
Showing 3 changed files with 921 additions and 21 deletions.
21 changes: 12 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
linters-settings:
govet:
check-shadowing: true
shadow: true
gocyclo:
min-complexity: 15
maligned:
Expand All @@ -22,35 +22,38 @@ linters-settings:

linters:
enable:
- megacheck
- staticcheck
- revive
- govet
- unconvert
- gas
- gosec
- misspell
- unparam
- typecheck
- ineffassign
- stylecheck
- gochecknoinits
- exportloopref
- copyloopvar
- gocritic
- nakedret
- gosimple
- prealloc
- unused
fast: false
disable-all: true

run:
output:
format: tab
skip-dirs:
- vendor

run:
concurrency: 4

issues:
exclude-rules:
- text: "G114: Use of net/http serve function that has no support for setting timeouts"
linters:
- gosec
- linters:
- unparam
- revive
path: _test\.go$
text: "unused-parameter"
exclude-use-default: false
32 changes: 32 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package routegroup
import (
"net/http"
"regexp"
"strings"
"sync"
)

Expand Down Expand Up @@ -96,9 +97,40 @@ func (b *Bundle) With(middleware func(http.Handler) http.Handler, more ...func(h

// Handle adds a new route to the Group's mux, applying all middlewares to the handler.
func (b *Bundle) Handle(pattern string, handler http.Handler) {
// for file server paths (ending with /), preserve the pattern and disable root not-found handler
if strings.HasSuffix(pattern, "/") {
if pattern == "/" {
b.rootRegistered.disableRootNotFoundHandler = true
}
fullPath := b.basePath + pattern
b.mux.Handle(fullPath, b.wrapMiddleware(handler))
return
}
b.register(pattern, handler.ServeHTTP)
}

// HandleFiles is a helper to serve static files from a directory
func (b *Bundle) HandleFiles(pattern string, root http.FileSystem) {
// normalize pattern to always have trailing slash
if !strings.HasSuffix(pattern, "/") {
pattern += "/"
}

// build the full path for registration
fullPath := b.basePath + pattern

if pattern == "/" && b.basePath == "" {
// root case - serve directly without stripping
b.rootRegistered.disableRootNotFoundHandler = true
b.mux.Handle("/", b.wrapMiddleware(http.FileServer(root)))
return
}

// for both mounted groups and prefixed paths, strip the fullPath
handler := http.StripPrefix(strings.TrimSuffix(fullPath, "/"), http.FileServer(root))
b.mux.Handle(fullPath, b.wrapMiddleware(handler))
}

// HandleFunc registers the handler function for the given pattern to the Group's mux.
// The handler is wrapped with the Group's middlewares.
func (b *Bundle) HandleFunc(pattern string, handler http.HandlerFunc) {
Expand Down
Loading

0 comments on commit a9dbd75

Please sign in to comment.