2017-08-28 01:13:45 +05:00
|
|
|
// OpenSAPS - Open Slack API server for everyone.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2017, Stanislav N. aka pztrn.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2021-11-21 16:16:10 +05:00
|
|
|
"fmt"
|
2017-10-31 22:50:14 +05:00
|
|
|
"os"
|
|
|
|
"strings"
|
2021-11-21 16:16:10 +05:00
|
|
|
"time"
|
2017-10-31 22:50:14 +05:00
|
|
|
|
2021-11-21 16:16:10 +05:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"go.dev.pztrn.name/flagger"
|
2019-12-15 22:17:01 +05:00
|
|
|
configurationinterface "go.dev.pztrn.name/opensaps/config/interface"
|
|
|
|
parserinterface "go.dev.pztrn.name/opensaps/parsers/interface"
|
|
|
|
pusherinterface "go.dev.pztrn.name/opensaps/pushers/interface"
|
|
|
|
slackapiserverinterface "go.dev.pztrn.name/opensaps/slack/apiserverinterface"
|
|
|
|
slackmessage "go.dev.pztrn.name/opensaps/slack/message"
|
2017-08-28 01:13:45 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
2017-10-31 22:50:14 +05:00
|
|
|
Config configurationinterface.ConfigurationInterface
|
2021-11-21 16:16:10 +05:00
|
|
|
SlackAPIServer slackapiserverinterface.SlackAPIServerInterface
|
2017-10-31 22:50:14 +05:00
|
|
|
Flagger *flagger.Flagger
|
|
|
|
Parsers map[string]parserinterface.ParserInterface
|
|
|
|
Pushers map[string]pusherinterface.PusherInterface
|
2021-11-21 16:16:10 +05:00
|
|
|
Log zerolog.Logger
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Initialize() {
|
2017-10-31 22:50:14 +05:00
|
|
|
c.Parsers = make(map[string]parserinterface.ParserInterface)
|
|
|
|
c.Pushers = make(map[string]pusherinterface.PusherInterface)
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2021-11-21 16:16:10 +05:00
|
|
|
// nolint:exhaustivestruct
|
|
|
|
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
|
|
|
|
output.FormatLevel = func(lvlRaw interface{}) string {
|
|
|
|
var v string
|
|
|
|
|
|
|
|
if lvl, ok := lvlRaw.(string); ok {
|
|
|
|
lvl = strings.ToUpper(lvl)
|
|
|
|
switch lvl {
|
|
|
|
case "DEBUG":
|
|
|
|
v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl)
|
|
|
|
case "ERROR":
|
|
|
|
v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl)
|
|
|
|
case "FATAL":
|
|
|
|
v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl)
|
|
|
|
case "INFO":
|
|
|
|
v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl)
|
|
|
|
case "PANIC":
|
|
|
|
v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl)
|
|
|
|
case "WARN":
|
|
|
|
v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl)
|
|
|
|
default:
|
|
|
|
v = lvl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("| %s |", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Log = zerolog.New(output).With().Timestamp().Logger()
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2021-11-21 16:16:10 +05:00
|
|
|
flaggerLogger := &FlaggerLogger{log: c.Log}
|
|
|
|
c.Flagger = flagger.New("opensaps", flagger.LoggerInterface(flaggerLogger))
|
2017-10-31 22:50:14 +05:00
|
|
|
c.Flagger.Initialize()
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registers configuration interface.
|
|
|
|
func (c *Context) RegisterConfigurationInterface(ci configurationinterface.ConfigurationInterface) {
|
2017-10-31 22:50:14 +05:00
|
|
|
c.Config = ci
|
|
|
|
c.Config.Initialize()
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
2017-08-28 08:04:38 +05:00
|
|
|
// Registers parser interface.
|
|
|
|
func (c *Context) RegisterParserInterface(name string, iface parserinterface.ParserInterface) {
|
2017-10-31 22:50:14 +05:00
|
|
|
c.Parsers[name] = iface
|
|
|
|
c.Parsers[name].Initialize()
|
2017-08-28 08:04:38 +05:00
|
|
|
}
|
|
|
|
|
2017-08-28 01:13:45 +05:00
|
|
|
// Registers Pusher interface.
|
|
|
|
func (c *Context) RegisterPusherInterface(name string, iface pusherinterface.PusherInterface) {
|
2017-10-31 22:50:14 +05:00
|
|
|
c.Pushers[name] = iface
|
|
|
|
c.Pushers[name].Initialize()
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registers Slack API HTTP server control structure.
|
|
|
|
// Russians will have pretty good luff on variable name.
|
|
|
|
func (c *Context) RegisterSlackAPIServerInterface(sasi slackapiserverinterface.SlackAPIServerInterface) {
|
2017-10-31 22:50:14 +05:00
|
|
|
c.SlackAPIServer = sasi
|
|
|
|
c.SlackAPIServer.Initialize()
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
2019-03-05 19:31:13 +05:00
|
|
|
func (c *Context) SendToParser(name string, message slackmessage.SlackMessage) map[string]interface{} {
|
2017-10-31 22:50:14 +05:00
|
|
|
parser, found := c.Parsers[strings.ToLower(name)]
|
|
|
|
if !found {
|
2021-11-21 16:16:10 +05:00
|
|
|
c.Log.Error().Msgf("Parser '%s' not found, will use default one!", name)
|
|
|
|
|
2017-10-31 22:50:14 +05:00
|
|
|
return c.Parsers["default"].ParseMessage(message)
|
|
|
|
}
|
2017-08-28 08:04:38 +05:00
|
|
|
|
2017-10-31 22:50:14 +05:00
|
|
|
return parser.ParseMessage(message)
|
2017-08-28 08:04:38 +05:00
|
|
|
}
|
|
|
|
|
2017-08-28 01:13:45 +05:00
|
|
|
func (c *Context) SendToPusher(protocol string, connection string, data slackmessage.SlackMessage) {
|
2017-10-31 22:50:14 +05:00
|
|
|
pusher, ok := c.Pushers[protocol]
|
|
|
|
if !ok {
|
2021-11-21 16:16:10 +05:00
|
|
|
c.Log.Error().Msgf("Pusher not found (or initialized) for protocol '%s'!", protocol)
|
2017-10-31 22:50:14 +05:00
|
|
|
}
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2017-10-31 22:50:14 +05:00
|
|
|
pusher.Push(connection, data)
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown everything.
|
|
|
|
func (c *Context) Shutdown() {
|
2017-10-31 22:50:14 +05:00
|
|
|
c.SlackAPIServer.Shutdown()
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2017-10-31 22:50:14 +05:00
|
|
|
for _, pusher := range c.Pushers {
|
|
|
|
pusher.Shutdown()
|
|
|
|
}
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|