-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
81 lines (69 loc) · 1.83 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package goober
import (
"fmt"
"log"
"mime"
"os"
"path/filepath"
"strings"
)
type Group struct {
Prefix string
Routes []Route
Middleware []MiddlewareFunc
}
func NewGroup(prefix string) *Group {
return &Group{Prefix: prefix}
}
func (g *Group) GET(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.addRoute("GET", pattern, handler, middleware...)
}
func (g *Group) Use(middleware ...MiddlewareFunc) bool {
g.Middleware = append(g.Middleware, middleware[0])
return true
}
func (g *Group) POST(pattern string, handler HandlerFunc) {
g.addRoute("POST", pattern, handler)
}
func (g *Group) DELETE(pattern string, handler HandlerFunc) {
g.addRoute("DELETE", pattern, handler)
}
func (g *Group) PUT(pattern string, handler HandlerFunc) {
g.addRoute("PUT", pattern, handler)
}
func (g *Group) PATCH(pattern string, handler HandlerFunc) {
g.addRoute("PATCH", pattern, handler)
}
func (g *Group) addRoute(method, pattern string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Routes = append(g.Routes, Route{Method: method, Pattern: pattern, Handler: handler, Middleware: middleware})
}
func (g *Group) ServeStatic(urlPrefix, directory string) {
if !strings.HasSuffix(urlPrefix, "/") {
urlPrefix += "/"
}
entries, err := os.ReadDir(directory)
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
fmt.Println(e.Name())
g.Routes = append(g.Routes, Route{
Method: "GET",
Pattern: urlPrefix + e.Name(),
Handler: func(ctx *Context) {
ctx.SendFile(directory+"/"+e.Name(), g.getContentType(e.Name()))
},
})
}
}
func (g *Group) getContentType(filename string) string {
ext := filepath.Ext(filename)
if ext == "" {
return "application/octet-stream"
}
contentType := mime.TypeByExtension(ext)
if contentType == "" {
return "application/octet-stream"
}
return contentType
}