Provide HTTP server and registration function

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-08-25 20:53:04 +02:00
parent 47f314af4e
commit 404ece80ed
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
7 changed files with 25 additions and 0 deletions

View File

@ -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,

View File

@ -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"`

1
go.mod
View File

@ -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

1
go.sum
View File

@ -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=

View File

@ -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 {

View File

@ -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

View File

@ -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