-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gogf/gf/v2/frame/g" | ||
"github.com/gogf/gf/v2/net/ghttp" | ||
) | ||
|
||
// pathMap is used for URL mapping | ||
var pathMap = map[string]string{ | ||
"/aaa/": "/tmp/", | ||
} | ||
|
||
// ServeFile serves the file to the response. | ||
func ServeFile(r *ghttp.Request) { | ||
truePath := r.URL.Path | ||
hasPrefix := false | ||
// Replace the path prefix. | ||
for k, v := range pathMap { | ||
if strings.HasPrefix(truePath, k) { | ||
truePath = strings.Replace(truePath, k, v, 1) // Replace only once. | ||
hasPrefix = true | ||
break | ||
} | ||
} | ||
|
||
if !hasPrefix { | ||
r.Response.WriteStatus(http.StatusForbidden) | ||
return | ||
} | ||
|
||
r.Response.ServeFile(truePath) | ||
} | ||
|
||
func main() { | ||
s := g.Server() | ||
s.Use(ghttp.MiddlewareHandlerResponse) | ||
s.BindHandler("/*", ServeFile) | ||
s.SetPort(8080) | ||
s.Run() | ||
} | ||
|
||
// http://127.0.0.1:8080/aaa/main.go |