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,
|
h.RejectPM,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle commands
|
r.Use(commonMw...).
|
||||||
r.
|
Route("ping", h.Ping).
|
||||||
Route("ping", r.Wrapped(h.Ping, commonMw...)).
|
Route("project", h.CreateProject).
|
||||||
Route("project", r.Wrapped(h.CreateProject, commonMw...)).
|
Route("info", h.ProjectInfo).
|
||||||
Route("info", r.Wrapped(h.ProjectInfo, commonMw...)).
|
Route("repo", h.CreateGit).
|
||||||
Route("repo", r.Wrapped(h.CreateGit, commonMw...)).
|
Route("folder", h.CreateFolder).
|
||||||
Route("folder", r.Wrapped(h.CreateFolder, commonMw...)).
|
Route("init_project", h.InitChannelAsProject).
|
||||||
Route("init_project", r.Wrapped(h.InitChannelAsProject, commonMw...)).
|
Route("coda_ticket", h.CreateCoda)
|
||||||
Route("coda_ticket", r.Wrapped(h.CreateCoda, commonMw...)).
|
|
||||||
// and components
|
// and components
|
||||||
|
r.
|
||||||
|
/*Use().*/ // Combining into group duplicates replies
|
||||||
Route("task_start", h.HandleTaskButtons).
|
Route("task_start", h.HandleTaskButtons).
|
||||||
Route("task_close", h.HandleTaskButtons)
|
Route("task_close", h.HandleTaskButtons)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ func (re *RouteEntry) Match(i *discordgo.InteractionCreate) bool {
|
||||||
type Router struct {
|
type Router struct {
|
||||||
session *discordgo.Session
|
session *discordgo.Session
|
||||||
routes []RouteEntry
|
routes []RouteEntry
|
||||||
|
group []Group
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApp(s *discordgo.Session) *Router {
|
func NewApp(s *discordgo.Session) *Router {
|
||||||
|
|
@ -52,23 +53,49 @@ func (r *Router) Serve(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
e.Handler(s, i)
|
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
|
type Middleware func(HandlerFunc) HandlerFunc
|
||||||
|
|
||||||
func (r *Router) Wrapped(f HandlerFunc, m ...Middleware) HandlerFunc {
|
type Group struct {
|
||||||
|
routes []RouteEntry
|
||||||
if len(m) < 1 {
|
middleware []Middleware
|
||||||
return f
|
}
|
||||||
}
|
|
||||||
|
func (r *Router) Use(m ...Middleware) *Group {
|
||||||
wrapped := f
|
|
||||||
|
r.group = append(r.group, Group{
|
||||||
// loop in reverse to preserve middleware order
|
routes: []RouteEntry{},
|
||||||
for i := len(m) - 1; i >= 0; i-- {
|
middleware: m,
|
||||||
wrapped = m[i](wrapped)
|
})
|
||||||
}
|
|
||||||
|
return &r.group[len(r.group)-1]
|
||||||
return wrapped
|
}
|
||||||
|
|
||||||
|
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