diff --git a/action_core.go b/action_core.go index 4f11a48..c92e47d 100644 --- a/action_core.go +++ b/action_core.go @@ -1,6 +1,8 @@ package main import ( + "fmt" + "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" @@ -9,6 +11,7 @@ import ( "github.com/Luzifer/twitch-bot/internal/actors/timeout" "github.com/Luzifer/twitch-bot/internal/actors/whisper" "github.com/Luzifer/twitch-bot/plugins" + "github.com/gorilla/mux" log "github.com/sirupsen/logrus" ) @@ -34,6 +37,7 @@ func init() { func getRegistrationArguments() plugins.RegistrationArguments { return plugins.RegistrationArguments{ FormatMessage: formatMessage, + GetHTTPRouter: func(name string) *mux.Router { return router.PathPrefix(fmt.Sprintf("/%s/", name)).Subrouter() }, GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) }, RegisterActor: registerAction, RegisterCron: cronService.AddFunc, diff --git a/config.go b/config.go index 6731cbd..1557c43 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ import ( type configFile struct { AutoMessages []*autoMessage `yaml:"auto_messages"` Channels []string `yaml:"channels"` + HTTPListen string `yaml:"http_listen"` PermitAllowModerator bool `yaml:"permit_allow_moderator"` PermitTimeout time.Duration `yaml:"permit_timeout"` RawLog string `yaml:"raw_log"` diff --git a/go.mod b/go.mod index b162c53..c13894f 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/Luzifer/korvike/functions v0.6.1 github.com/Luzifer/rconfig/v2 v2.3.0 github.com/go-irc/irc v2.1.0+incompatible + github.com/gorilla/mux v1.7.4 github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 diff --git a/go.sum b/go.sum index 4ffe237..c0ebcba 100644 --- a/go.sum +++ b/go.sum @@ -106,6 +106,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= diff --git a/main.go b/main.go index 307601e..5b490e6 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,14 @@ package main import ( "fmt" + "net/http" "os" "strings" "sync" "time" "github.com/go-irc/irc" + "github.com/gorilla/mux" "github.com/pkg/errors" "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" @@ -37,6 +39,7 @@ var ( configLock = new(sync.RWMutex) cronService *cron.Cron + router *mux.Router sendMessage func(m *irc.Message) error @@ -77,6 +80,7 @@ func main() { var err error cronService = cron.New() + router = mux.NewRouter() twitchClient = twitch.New(cfg.TwitchClient, cfg.TwitchToken) if err = loadPlugins(cfg.PluginDir); err != nil { @@ -113,6 +117,11 @@ func main() { cronService.Start() + if config.HTTPListen != "" { + // If listen address is configured start HTTP server + go http.ListenAndServe(config.HTTPListen, router) + } + ircDisconnected <- struct{}{} for { diff --git a/plugins/interface.go b/plugins/interface.go index 3e48d22..2765d88 100644 --- a/plugins/interface.go +++ b/plugins/interface.go @@ -2,6 +2,7 @@ package plugins import ( "github.com/go-irc/irc" + "github.com/gorilla/mux" "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" ) @@ -25,6 +26,8 @@ type ( CronRegistrationFunc func(spec string, cmd func()) (cron.EntryID, error) + HTTPRouterCreationFunc func(name string) *mux.Router + LoggerCreationFunc func(moduleName string) *log.Entry MsgFormatter func(tplString string, m *irc.Message, r *Rule, fields map[string]interface{}) (string, error) @@ -35,6 +38,8 @@ type ( RegistrationArguments struct { // FormatMessage is a method to convert templates into strings using internally known variables / configs FormatMessage MsgFormatter + // GetHTTPRouter returns a new mux.Router with `/{name}/` prefix + GetHTTPRouter HTTPRouterCreationFunc // GetLogger returns a sirupsen log.Entry pre-configured with the module name GetLogger LoggerCreationFunc // RegisterActor is used to register a new IRC rule-actor implementing the Actor interface diff --git a/wiki/Home.md b/wiki/Home.md index ba9a8ea..02ddbd4 100644 --- a/wiki/Home.md +++ b/wiki/Home.md @@ -7,6 +7,10 @@ channels: - mychannel +# Enable HTTP server to control plugins / core functionality +# if unset the server is not started, to change the bot must be restarted +http_listen: "127.0.0.1:3000" + # Allow moderators to hand out permits (if set to false only broadcaster can do this) permit_allow_moderator: true # How long to permit on !permit command