Skip to content

Commit 1769e7b

Browse files
committed
feat: added ServeStaticFiles function to Router interface and implementation
1 parent 399300e commit 1769e7b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

http/route/router.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type (
4343
RelativePath() string
4444
FullPath() string
4545
Method() string
46+
ServeStaticFiles(pattern, path string)
4647
}
4748

4849
// Router is the route group struct
@@ -333,3 +334,20 @@ func (r *Router) FullPath() string {
333334
func (r *Router) Method() string {
334335
return r.method
335336
}
337+
338+
// ServeStaticFiles serves the static files
339+
func (r *Router) ServeStaticFiles(
340+
pattern,
341+
path string,
342+
) {
343+
// Check if the pattern contains a trailing slash and add it
344+
if len(pattern) == 0 || pattern[len(pattern)-1] != '/' {
345+
pattern = pattern + "/"
346+
}
347+
348+
// Serve the static files
349+
r.mux.HandleFunc(
350+
pattern,
351+
http.StripPrefix(pattern, http.FileServer(http.Dir(path))).ServeHTTP,
352+
)
353+
}

0 commit comments

Comments
 (0)