-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.go
More file actions
47 lines (39 loc) · 1.12 KB
/
route.go
File metadata and controls
47 lines (39 loc) · 1.12 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
package mmbot
import (
"net/http"
"github.com/fukata/golang-stats-api-handler"
)
// RouteHandlerFunc is route action function.
type RouteHandlerFunc func(*Robot, http.ResponseWriter, *http.Request)
// Route is a HTTP route.
type Route struct {
// HTTP method (e.g. "GET", "POST").
// All methods are allowed if empty.
Methods []string
// Route pattern (e.g. "/articles/{category}/{id:[0-9]+}")
// Pattern can have variables that are defined by "{name}" or "{name:regexp}" format.
// Variables can be retrieved calling Robot.RouteVars().
Pattern string
// Route action.
Action RouteHandlerFunc
}
// NewPingRoute returns the route "ping".
func NewPingRoute(pattern string) Route {
return Route{
Methods: []string{"GET"},
Pattern: pattern,
Action: func(bot *Robot, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
},
}
}
// NewStatsRoute returns the route for statistics of the process.
func NewStatsRoute(pattern string) Route {
return Route{
Methods: []string{"GET"},
Pattern: pattern,
Action: func(bot *Robot, w http.ResponseWriter, r *http.Request) {
stats_api.Handler(w, r)
},
}
}