router simple groups
This commit is contained in:
parent
5d6adb2a9a
commit
0776156d33
|
|
@ -228,16 +228,18 @@ func Run(conf *domain.Config, opts DiscordOptions) error {
|
|||
h.RejectPM,
|
||||
}
|
||||
|
||||
// Handle commands
|
||||
r.
|
||||
Route("ping", r.Wrapped(h.Ping, commonMw...)).
|
||||
Route("project", r.Wrapped(h.CreateProject, commonMw...)).
|
||||
Route("info", r.Wrapped(h.ProjectInfo, commonMw...)).
|
||||
Route("repo", r.Wrapped(h.CreateGit, commonMw...)).
|
||||
Route("folder", r.Wrapped(h.CreateFolder, commonMw...)).
|
||||
Route("init_project", r.Wrapped(h.InitChannelAsProject, commonMw...)).
|
||||
Route("coda_ticket", r.Wrapped(h.CreateCoda, commonMw...)).
|
||||
r.Use(commonMw...).
|
||||
Route("ping", h.Ping).
|
||||
Route("project", h.CreateProject).
|
||||
Route("info", h.ProjectInfo).
|
||||
Route("repo", h.CreateGit).
|
||||
Route("folder", h.CreateFolder).
|
||||
Route("init_project", h.InitChannelAsProject).
|
||||
Route("coda_ticket", h.CreateCoda)
|
||||
|
||||
// and components
|
||||
r.
|
||||
/*Use().*/ // Combining into group duplicates replies
|
||||
Route("task_start", h.HandleTaskButtons).
|
||||
Route("task_close", h.HandleTaskButtons)
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ func (re *RouteEntry) Match(i *discordgo.InteractionCreate) bool {
|
|||
type Router struct {
|
||||
session *discordgo.Session
|
||||
routes []RouteEntry
|
||||
group []Group
|
||||
}
|
||||
|
||||
func NewApp(s *discordgo.Session) *Router {
|
||||
|
|
@ -52,23 +53,49 @@ func (r *Router) Serve(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||
e.Handler(s, i)
|
||||
}
|
||||
}
|
||||
//[ ] Is there something like 404?!
|
||||
|
||||
for _, g := range r.group {
|
||||
for _, e := range g.routes {
|
||||
ok := e.Match(i)
|
||||
if ok {
|
||||
if len(g.middleware) < 1 {
|
||||
e.Handler(s, i)
|
||||
}
|
||||
|
||||
wrapped := e.Handler
|
||||
|
||||
// loop in reverse to preserve middleware order
|
||||
for i := len(g.middleware) - 1; i >= 0; i-- {
|
||||
wrapped = g.middleware[i](wrapped)
|
||||
}
|
||||
wrapped(s, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Middleware func(HandlerFunc) HandlerFunc
|
||||
|
||||
func (r *Router) Wrapped(f HandlerFunc, m ...Middleware) HandlerFunc {
|
||||
|
||||
if len(m) < 1 {
|
||||
return f
|
||||
}
|
||||
|
||||
wrapped := f
|
||||
|
||||
// loop in reverse to preserve middleware order
|
||||
for i := len(m) - 1; i >= 0; i-- {
|
||||
wrapped = m[i](wrapped)
|
||||
}
|
||||
|
||||
return wrapped
|
||||
type Group struct {
|
||||
routes []RouteEntry
|
||||
middleware []Middleware
|
||||
}
|
||||
|
||||
func (r *Router) Use(m ...Middleware) *Group {
|
||||
|
||||
r.group = append(r.group, Group{
|
||||
routes: []RouteEntry{},
|
||||
middleware: m,
|
||||
})
|
||||
|
||||
return &r.group[len(r.group)-1]
|
||||
}
|
||||
|
||||
func (g *Group) Route(cmd string, handlerFunc HandlerFunc) *Group {
|
||||
|
||||
g.routes = append(g.routes, RouteEntry{
|
||||
CommandName: cmd,
|
||||
Handler: handlerFunc,
|
||||
})
|
||||
return g
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue