Skip to content

Commit a61da84

Browse files
authored
Update server.go
1 parent 240a680 commit a61da84

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

server.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,33 @@ func (s *Server) Delete(path string, handler HandlerFunc) {
5252

5353
// static file server
5454
func (s *Server) Static(routePath, dir string) {
55-
if !strings.HasSuffix(routePath, "/") {
56-
routePath += "/"
57-
}
58-
59-
s.router.Handle(http.MethodGet, routePath+"*", func(ctx *Context) {
60-
http.ServeFile(ctx.Writer, ctx.Request, dir+strings.TrimPrefix(ctx.Request.URL.Path, routePath))
61-
})
55+
// routePath sonunda / yoksa ekle
56+
if !strings.HasSuffix(routePath, "/") {
57+
routePath += "/"
58+
}
59+
60+
s.router.Handle(http.MethodGet, routePath+"*", func(ctx *Context) {
61+
// İstek yolu
62+
reqPath := ctx.Request.URL.Path
63+
64+
// routePath’i kırp
65+
relPath := strings.TrimPrefix(reqPath, routePath)
66+
67+
// Güvenli dosya yolu: dir + relPath
68+
fullPath := filepath.Join(dir, relPath)
69+
70+
// Dosya yoksa 404 dön
71+
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
72+
http.NotFound(ctx.Writer, ctx.Request)
73+
return
74+
}
75+
76+
// ServeFile ile gönder
77+
http.ServeFile(ctx.Writer, ctx.Request, fullPath)
78+
})
6279
}
6380

81+
6482
// NotFound override
6583
func (s *Server) SetNotFound(handler HandlerFunc) {
6684
s.notFound = handler

0 commit comments

Comments
 (0)