-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (40 loc) · 1.03 KB
/
main.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
package main
import (
"fmt"
"log"
"moon/moon"
"net/http"
)
func main() {
m, err := moon.New(
moon.WithNotFound(
func(ctx moon.Context) error {
return ctx.SendString("route not registered", http.StatusNotFound)
},
),
// moon.WithErrorHandler(
// func(err error, ctx moon.Context) {
// ctx.SendString("error happended", http.StatusInternalServerError)
// }),
)
// m.Use(moon.MiddlewareA, moon.MiddlewareB, moon.MiddlewareC)
if err != nil {
log.Fatal(err)
}
m.GET("/", func(ctx moon.Context) error {
return ctx.SendString("Home Page", 200)
})
m.GET("/error", func(ctx moon.Context) error {
return fmt.Errorf("error")
})
m.GET("/users/:id/:name", func(ctx moon.Context) error {
return ctx.SendString(ctx.PathParam("id")+" "+ctx.PathParam("name")+" "+ctx.FirstQueryParam("q"), http.StatusOK)
})
http.ListenAndServe(":3000", m)
}
func MiddlewareB(next moon.HandlerFunc) moon.HandlerFunc {
return func(c moon.Context) error {
fmt.Println("Middleware B: Checking authorization")
return next(c)
}
}