File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -291,6 +291,16 @@ func (r *Router) ServeFiles(path string, rootPath string) {
291
291
if len (path ) < 10 || path [len (path )- 10 :] != "/*filepath" {
292
292
panic ("path must end with /*filepath in path '" + path + "'" )
293
293
}
294
+
295
+ if r .beginPath != "/" {
296
+ path = r .beginPath + path
297
+ }
298
+
299
+ if r .parent != nil {
300
+ r .parent .ServeFiles (path , rootPath )
301
+ return
302
+ }
303
+
294
304
prefix := path [:len (path )- 10 ]
295
305
296
306
fileHandler := fasthttp .FSHandler (rootPath , strings .Count (prefix , "/" ))
Original file line number Diff line number Diff line change @@ -370,6 +370,8 @@ func TestRouterGroup(t *testing.T) {
370
370
barHit = true
371
371
ctx .SetStatusCode (fasthttp .StatusOK )
372
372
})
373
+ r6 .ServeFiles ("/static/*filepath" , "./" )
374
+
373
375
s := & fasthttp.Server {
374
376
Handler : r1 .Handler ,
375
377
}
@@ -499,6 +501,27 @@ func TestRouterGroup(t *testing.T) {
499
501
t .FailNow ()
500
502
}
501
503
504
+ // testing multiple sub-router group - r6 (grouped from r5) to serve files
505
+ rw .r .WriteString ("GET /moo/foo/foo/static/router.go HTTP/1.1\r \n \r \n " )
506
+ go func () {
507
+ ch <- s .ServeConn (rw )
508
+ }()
509
+ select {
510
+ case err := <- ch :
511
+ if err != nil {
512
+ t .Fatalf ("return error %s" , err )
513
+ }
514
+ case <- time .After (100 * time .Millisecond ):
515
+ t .Fatalf ("timeout" )
516
+ }
517
+ if err := resp .Read (br ); err != nil {
518
+ t .Fatalf ("Unexpected error when reading response: %s" , err )
519
+ }
520
+ if ! (resp .Header .StatusCode () == fasthttp .StatusOK && barHit ) {
521
+ t .Errorf ("Chained routing failed with subrouter grouping." )
522
+ t .FailNow ()
523
+ }
524
+
502
525
rw .r .WriteString ("POST /qax HTTP/1.1\r \n \r \n " )
503
526
go func () {
504
527
ch <- s .ServeConn (rw )
You can’t perform that action at this time.
0 commit comments