-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.go
99 lines (82 loc) · 2.85 KB
/
routes.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package goster
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
type Routes map[string]map[string]Route
func (rs *Routes) prepareStaticRoutes(dir string) (err error) {
staticPaths := engine.Config.StaticFilePaths
for relPath := range staticPaths {
staticPath := staticPaths[relPath]
file, err := os.Open(staticPath)
if err != nil {
fmt.Fprintf(os.Stderr, "cannot open static file `%s`\n", file.Name())
return err
}
// register a GET route that serves the static file
routePath := filepath.Join(dir, relPath)
cleanURLPath(&routePath)
err = rs.New("GET", routePath, func(ctx *Ctx) error {
return staticFileHandler(ctx, file)
})
if err != nil {
fmt.Fprintf(os.Stderr, "couldn't add route `%s`. Most likely there's a duplicate entry\n", routePath)
}
}
return
}
// New creates a new Route for the specified method and url using the provided handler. If the Route already exists an error is returned.
func (rs *Routes) New(method string, url string, handler RequestHandler) (err error) {
for name := range (*rs)[method] {
if name == url {
err = fmt.Errorf("[%s] -> [%s] route already exists", method, url)
return
}
}
routeType := "normal"
if strings.Contains(url, ":") {
routeType = "dynamic"
}
cleanURLPath(&url)
(*rs)[method][url] = Route{Type: routeType, Handler: handler}
return
}
// Get creates a new Route under the GET method for `path`. If the Route aleady exists an error is returned.
func (g *Goster) Get(url string, handler RequestHandler) error {
return g.Routes.New("GET", url, handler)
}
// Post creates a new Route under the POST method for `path`. If the Route aleady exists an error is returned.
func (g *Goster) Post(path string, handler RequestHandler) error {
return g.Routes.New("POST", path, handler)
}
// Patch creates a new Route under the PATCH method for `path`. If the Route aleady exists an error is returned.
func (g *Goster) Patch(path string, handler RequestHandler) error {
return g.Routes.New("PATCH", path, handler)
}
// Put creates a new Route under the PUT method for `path`. If the Route aleady exists an error is returned.
func (g *Goster) Put(path string, handler RequestHandler) error {
return g.Routes.New("PUT", path, handler)
}
// Delete creates a new Route under the DELETE method for `path`. If the Route aleady exists an error is returned.
func (g *Goster) Delete(path string, handler RequestHandler) error {
return g.Routes.New("DELETE", path, handler)
}
func staticFileHandler(ctx *Ctx, file *os.File) (err error) {
// read the file contents
_, err = file.Seek(0, 0)
if err != nil {
return
}
fInfo, _ := file.Stat()
fSize := fInfo.Size()
buffer := make([]byte, fSize)
_, _ = io.ReadFull(file, buffer)
// prepare and write response
contentType := getContentType(file.Name())
ctx.Response.Header().Set("Content-Type", contentType)
_, err = ctx.Response.Write(buffer)
return
}