uplink/app/server.go

66 lines
1.5 KiB
Go
Raw Normal View History

2018-04-10 14:34:30 +00:00
package app
import (
2018-04-10 15:08:40 +00:00
"log"
2018-04-11 15:55:15 +00:00
"git.icedream.tech/icedream/uplink/app/authentication"
2018-04-10 14:34:30 +00:00
"git.icedream.tech/icedream/uplink/app/channels"
2018-04-10 15:08:40 +00:00
"git.icedream.tech/icedream/uplink/app/servers/http"
"git.icedream.tech/icedream/uplink/plugins"
2018-04-10 14:34:30 +00:00
)
type registeredPlugin struct {
Instance plugins.PluginInstance
Descriptor plugins.PluginDescriptor
}
2018-04-10 15:08:40 +00:00
type App struct {
Server *httpserver.Server
2018-04-11 15:55:15 +00:00
Authenticator authentication.Authenticator
2018-04-10 15:08:40 +00:00
ChannelManager *channels.ChannelManager
plugins []registeredPlugin
2018-04-10 15:08:40 +00:00
}
func New() *App {
return &App{
Server: httpserver.NewServer(),
2018-04-11 15:55:15 +00:00
Authenticator: new(authentication.DummyAuthenticator),
2018-04-10 15:08:40 +00:00
ChannelManager: channels.NewChannelManager(),
plugins: []registeredPlugin{},
2018-04-10 15:08:40 +00:00
}
}
func (app *App) UsePlugin(plugin *plugins.Plugin) {
2018-04-11 15:55:15 +00:00
pluginInstance := plugin.Run()
if p, ok := pluginInstance.(plugins.ServerPlugin); ok {
p.SetServer(app.Server)
}
if p, ok := pluginInstance.(plugins.ChannelPlugin); ok {
p.SetChannelManager(app.ChannelManager)
}
if p, ok := pluginInstance.(plugins.AuthenticatorPlugin); ok {
p.SetAuthenticator(app.Authenticator)
}
log.Println("Plugin registered:", plugin.Descriptor.Name)
2018-04-11 15:55:15 +00:00
app.plugins = append(app.plugins, registeredPlugin{
Instance: pluginInstance,
Descriptor: plugin.Descriptor,
})
2018-04-10 15:08:40 +00:00
}
func (app *App) Init() {
for _, plugin := range app.plugins {
plugin.Instance.Init()
log.Println("Plugin initialized:", plugin.Descriptor.Name)
2018-04-10 15:08:40 +00:00
}
}
func (app *App) Run() error {
return app.Server.Run()
2018-04-10 14:34:30 +00:00
}