Skip to content

Commit 7dda68f

Browse files
committed
handle static routes
1 parent 105343c commit 7dda68f

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

router.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package routing
22

33
import (
4+
"fmt"
45
"github.com/valyala/fasthttp"
56
"strings"
7+
"time"
68
)
79

810
type Handler func(ctx *Context) error
@@ -19,6 +21,14 @@ type Router struct {
1921
middlewares map[string][]Middleware
2022
}
2123

24+
type Static struct {
25+
Root string
26+
Compress bool
27+
ByteRange bool
28+
IndexName string
29+
CacheDuration time.Duration
30+
}
31+
2232
func New() *Router {
2333
return &Router{
2434
routes: make(map[string][]*Route),
@@ -132,3 +142,60 @@ func (r *Route) match(path string) (bool, map[string]string) {
132142

133143
return true, params
134144
}
145+
146+
func (options *Static) notFoundHandler(ctx *fasthttp.RequestCtx) {
147+
ctx.SetStatusCode(fasthttp.StatusNotFound)
148+
ctx.SetContentType("text/plain; charset=utf-8")
149+
_, err := fmt.Fprintf(ctx, "404 Not Found")
150+
if err != nil {
151+
return
152+
}
153+
}
154+
155+
func (options *Static) pathRewrite(ctx *fasthttp.RequestCtx) []byte {
156+
path := ctx.Path()
157+
158+
if len(path) > 1 && path[len(path)-1] == '/' {
159+
path = path[:len(path)-1]
160+
}
161+
162+
// Remove the last part of the path
163+
parts := strings.Split(string(path), "/")
164+
if len(parts) > 1 {
165+
parts = parts[:len(parts)-1]
166+
}
167+
path = []byte(strings.Join(parts, "/"))
168+
169+
if options.IndexName != "" {
170+
// Append the index file name to the path
171+
path = append(path, '/')
172+
path = append(path, options.IndexName...)
173+
}
174+
175+
return path
176+
}
177+
178+
func (r *Router) Static(prefix, root string, options *Static) {
179+
if options == nil {
180+
options = &Static{}
181+
}
182+
if options.Root == "" {
183+
options.Root = root
184+
}
185+
fs := fasthttp.FS{
186+
Root: options.Root,
187+
IndexNames: []string{options.IndexName},
188+
PathRewrite: options.pathRewrite,
189+
GenerateIndexPages: false,
190+
Compress: options.Compress,
191+
AcceptByteRange: options.ByteRange,
192+
CacheDuration: options.CacheDuration,
193+
PathNotFound: options.notFoundHandler, // Set custom error handler for undefined routes
194+
}
195+
196+
r.Get(prefix, func(c *Context) error {
197+
fsHandler := fs.NewRequestHandler()
198+
fsHandler(c.RequestCtx)
199+
return nil
200+
})
201+
}

router_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,19 @@ func TestContext_SetHeader(t *testing.T) {
9191
return
9292
}
9393
}
94+
95+
func TestRouter_Static(t *testing.T) {
96+
router := New()
97+
98+
router.Static("/", "./static", &Static{
99+
Compress: true,
100+
ByteRange: false,
101+
IndexName: "index.html",
102+
CacheDuration: 24 * time.Hour,
103+
})
104+
105+
err := fasthttp.ListenAndServe(":8083", RouterHandler(router))
106+
if err != nil {
107+
return
108+
}
109+
}

static/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>My First Web Page</title>
4+
</head>
5+
<body>
6+
<h1>My First Web Page</h1>
7+
<p>My first paragraph.</p>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)