2021-08-19 13:33:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-25 18:53:04 +00:00
|
|
|
"fmt"
|
2021-09-22 13:36:45 +00:00
|
|
|
"net/http"
|
2021-08-25 18:53:04 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/internal/actors/ban"
|
|
|
|
"github.com/Luzifer/twitch-bot/internal/actors/delay"
|
|
|
|
deleteactor "github.com/Luzifer/twitch-bot/internal/actors/delete"
|
2021-09-10 17:45:31 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/internal/actors/modchannel"
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/internal/actors/raw"
|
|
|
|
"github.com/Luzifer/twitch-bot/internal/actors/respond"
|
|
|
|
"github.com/Luzifer/twitch-bot/internal/actors/timeout"
|
|
|
|
"github.com/Luzifer/twitch-bot/internal/actors/whisper"
|
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-09-10 15:57:04 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/twitch"
|
2021-08-28 15:27:24 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-08-19 13:33:56 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var coreActorRegistations = []plugins.RegisterFunc{
|
|
|
|
ban.Register,
|
|
|
|
delay.Register,
|
|
|
|
deleteactor.Register,
|
2021-09-10 17:45:31 +00:00
|
|
|
modchannel.Register,
|
2021-08-19 13:33:56 +00:00
|
|
|
raw.Register,
|
|
|
|
respond.Register,
|
|
|
|
timeout.Register,
|
|
|
|
whisper.Register,
|
|
|
|
}
|
|
|
|
|
2021-09-10 17:45:31 +00:00
|
|
|
func initCorePlugins() error {
|
2021-08-19 13:33:56 +00:00
|
|
|
args := getRegistrationArguments()
|
|
|
|
for _, rf := range coreActorRegistations {
|
|
|
|
if err := rf(args); err != nil {
|
2021-09-10 17:45:31 +00:00
|
|
|
return errors.Wrap(err, "registering core plugin")
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-10 17:45:31 +00:00
|
|
|
return nil
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 15:27:24 +00:00
|
|
|
func registerRoute(route plugins.HTTPRouteRegistrationArgs) error {
|
|
|
|
r := router.
|
|
|
|
PathPrefix(fmt.Sprintf("/%s/", route.Module)).
|
|
|
|
Subrouter()
|
|
|
|
|
2021-09-22 13:36:45 +00:00
|
|
|
var hdl http.Handler = route.HandlerFunc
|
|
|
|
if route.RequiresEditorsAuth {
|
|
|
|
hdl = botEditorAuthMiddleware(hdl)
|
|
|
|
}
|
|
|
|
|
2021-08-28 15:27:24 +00:00
|
|
|
if route.IsPrefix {
|
|
|
|
r.PathPrefix(route.Path).
|
2021-09-22 13:36:45 +00:00
|
|
|
Handler(hdl).
|
2021-08-28 15:27:24 +00:00
|
|
|
Methods(route.Method)
|
|
|
|
} else {
|
2021-09-22 13:36:45 +00:00
|
|
|
r.Handle(route.Path, hdl).
|
2021-08-28 15:27:24 +00:00
|
|
|
Methods(route.Method)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !route.SkipDocumentation {
|
|
|
|
return errors.Wrap(registerSwaggerRoute(route), "registering documentation")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func getRegistrationArguments() plugins.RegistrationArguments {
|
|
|
|
return plugins.RegistrationArguments{
|
2021-09-22 13:36:45 +00:00
|
|
|
FormatMessage: formatMessage,
|
|
|
|
GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) },
|
|
|
|
GetTwitchClient: func() *twitch.Client { return twitchClient },
|
|
|
|
RegisterActor: registerAction,
|
|
|
|
RegisterActorDocumentation: registerActorDocumentation,
|
|
|
|
RegisterAPIRoute: registerRoute,
|
|
|
|
RegisterCron: cronService.AddFunc,
|
|
|
|
RegisterRawMessageHandler: registerRawMessageHandler,
|
|
|
|
RegisterTemplateFunction: tplFuncs.Register,
|
|
|
|
SendMessage: sendMessage,
|
2021-08-19 13:33:56 +00:00
|
|
|
}
|
|
|
|
}
|