-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (36 loc) · 896 Bytes
/
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
package main
import (
"ett"
"log"
"net/http"
"time"
)
func onlyForV2() ett.HandlerFunc {
return func(c *ett.Context) {
// Start timer
t := time.Now()
// if a server error occurred
c.Fail(500, "Internal Server Error")
// Calculate resolution time
log.Printf("[%d] %s in %v for group v2", c.StatusCode, c.Req.RequestURI, time.Since(t))
}
}
func main() {
r := ett.New()
r.Use(ett.Logger(), ett.Recovery()) // global midlleware
r.GET("/", func(c *ett.Context) {
c.HTML(http.StatusOK, "<h1>Hello ett</h1>")
})
r.GET("/panic", func(ctx *ett.Context) {
names := []string{"test panic"}
ctx.String(http.StatusOK, names[10000]) // raise panic
})
v2 := r.Group("/v2")
v2.Use(onlyForV2()) // v2 group middleware
{
v2.GET("/hello/:name", func(c *ett.Context) {
c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
})
}
r.Run(":9999")
}