This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (145 loc) · 3.78 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"fmt"
"strconv"
"sync"
"time"
"github.com/juliankoehn/enlight"
"github.com/juliankoehn/enlight/middleware"
"github.com/valyala/fasthttp"
)
type (
App struct {
Enlight *enlight.Enlight
}
TestStruct struct {
Message string `json:"message"`
}
Stats struct {
Uptime time.Time `json:"uptime"`
RequestCount uint64 `json:"requestCount"`
Statuses map[string]int `json:"statuses"`
mutex sync.RWMutex
}
)
func NewStats() *Stats {
return &Stats{
Uptime: time.Now(),
Statuses: map[string]int{},
}
}
// AddDynamicRoutes middleware to add dynamic routes before "routing happens"
func (a *App) AddDynamicRoutes(next enlight.HandleFunc) enlight.HandleFunc {
return func(c enlight.Context) error {
a.Enlight.GET("/dynamic", DynamicRoute)
return next(c)
}
}
// CleanupDynamicRoutes removes dynamic routes after serve
func (a *App) CleanupDynamicRoutes(next enlight.HandleFunc) enlight.HandleFunc {
return func(c enlight.Context) error {
a.Enlight.Drop("GET", "/dynamic")
return next(c)
}
}
func DynamicRoute(c enlight.Context) error {
return c.String(200, "Hello from dynamic Route")
}
func PanicRoute(c enlight.Context) error {
panic("Panic!")
}
// ServerHeader middleware adds a `Server` header to the response.
func ServerHeader(next enlight.HandleFunc) enlight.HandleFunc {
return func(c enlight.Context) error {
c.Response().Header.Set(enlight.HeaderServer, "Enlight/3.0")
return next(c)
}
}
// ProcessStats is the middleware function.
func (s *Stats) ProcessStats(next enlight.HandleFunc) enlight.HandleFunc {
return func(c enlight.Context) error {
if err := next(c); err != nil {
c.Error(err)
}
s.mutex.Lock()
defer s.mutex.Unlock()
s.RequestCount++
status := strconv.Itoa(c.Response().StatusCode())
s.Statuses[status]++
return nil
}
}
// RouteBasedMiddleware demonstrates route based middleware
func RouteBasedMiddleware(next enlight.HandleFunc) enlight.HandleFunc {
return func(c enlight.Context) error {
c.Response().Header.Set(enlight.HeaderServer, "RouteBased")
return next(c)
}
}
// Handle is the endpoint to get stats
func (s *Stats) Handle(c enlight.Context) error {
s.mutex.RLock()
defer s.mutex.RUnlock()
return c.JSON(fasthttp.StatusOK, s)
}
func getAutoHandler(c enlight.Context) error {
m := TestStruct{"Hello from AutoHandler"}
fmt.Println("getAutoHandler")
return c.JSON(200, m)
}
func showHTML(c enlight.Context) error {
return c.HTML(200, "<h1>Hello World</h1>")
}
func userHandler(c enlight.Context) error {
cookieValue := c.Cookie("test")
c.SetCookie("test", "my value")
hello := c.QueryParamDefault("name", "Joseph")
name := c.Param("name")
return c.JSON(200, enlight.Map{
"status": 200,
"nameFromParam": name,
"nameFromQuery": hello,
"cookieValue": cookieValue,
})
}
func postUser(c enlight.Context) error {
firstname := c.FormValue("firstname")
return c.JSON(200, enlight.Map{
"status": "posted",
"firstname": firstname,
})
}
func serve() (err error) {
app := &App{}
e := enlight.New()
app.Enlight = e
e.Use(middleware.Recover())
e.GET("/", showHTML)
e.GET("/auto", getAutoHandler, RouteBasedMiddleware)
// testing Static
e.Static("/public", "")
s := NewStats()
e.Use(s.ProcessStats)
e.GET("/stats", s.Handle)
e.GET("/panic", PanicRoute)
e.GET("/user/:name", userHandler)
e.POST("/user", postUser)
// Cookie testing
e.GET("/cookie/:name/:value", setCookie)
e.GET("/cookie/:name", retrieveCookie)
e.DELETE("/cookie/:name", deleteCookie)
// Server header
e.Use(ServerHeader)
e.Before(app.AddDynamicRoutes)
e.After(app.CleanupDynamicRoutes)
if err = e.Start(":8085"); err != nil {
fmt.Printf("listen:%+s\n", err)
return err
}
return
}
func main() {
if err := serve(); err != nil {
fmt.Printf("failed to serve:+%v\n", err)
}
}