-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.go
More file actions
62 lines (55 loc) · 1.23 KB
/
Copy pathserve.go
File metadata and controls
62 lines (55 loc) · 1.23 KB
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
package middleware
import (
"net/http"
"strings"
"github.com/evorax/middleware/internal"
)
func (e *Engine) AddRoute(method string, pattern string, handler HandlerFunc) {
e.routes = append(e.routes, &route{
method: method,
pattern: pattern,
handler: handler,
})
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
internal.Info(r.Method, r.URL.Path)
for _, route := range e.routes {
/*if route.method == r.Method {
err := Method(route, w, r)
if err != nil {
internal.Error(err)
}
} else {
err := Method(route, w, r)
if err != nil {
internal.Error(err)
}
}*/
err := Method(route, w, r)
if err != nil {
internal.Error(err)
}
}
if e.staticDir != "" {
dir := strings.ReplaceAll(e.staticDir, ".", "")
if !strings.HasSuffix(dir, "/") {
dir += "/"
}
http.StripPrefix(strings.ReplaceAll(e.staticDir, ".", ""), http.FileServer(http.Dir(e.staticDir))).ServeHTTP(w, r)
} else {
http.NotFound(w, r)
}
}
func Method(route *route, w http.ResponseWriter, r *http.Request) error {
params, err := internal.MatchPath(route.pattern, r.URL.Path)
if err != nil {
return err
}
ctx := &Context{
Writer: w,
Request: r,
Params: params,
}
route.handler(ctx)
return nil
}