2021-05-24 19:06:11 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-06-21 12:08:33 +02:00
|
|
|
"path"
|
2021-05-24 19:06:11 +02:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/mattermost/focalboard/server/server"
|
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
2021-06-04 08:43:32 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/mlog"
|
2021-06-25 16:49:06 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
|
|
|
"github.com/mattermost/focalboard/server/services/store/mattermostauthlayer"
|
|
|
|
"github.com/mattermost/focalboard/server/services/store/sqlstore"
|
2021-06-04 08:43:32 +02:00
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
pluginapi "github.com/mattermost/mattermost-plugin-api"
|
2021-07-06 22:44:11 +02:00
|
|
|
|
2021-08-24 12:13:58 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/model"
|
|
|
|
"github.com/mattermost/mattermost-server/v6/plugin"
|
2021-05-24 19:06:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Plugin implements the interface expected by the Mattermost server to communicate between the server and plugin processes.
|
|
|
|
type Plugin struct {
|
|
|
|
plugin.MattermostPlugin
|
|
|
|
|
|
|
|
// configurationLock synchronizes access to the configuration.
|
|
|
|
configurationLock sync.RWMutex
|
|
|
|
|
|
|
|
// configuration is the active plugin configuration. Consult getConfiguration and
|
|
|
|
// setConfiguration for usage.
|
|
|
|
configuration *configuration
|
|
|
|
|
|
|
|
server *server.Server
|
|
|
|
wsHub *WSHub
|
|
|
|
}
|
|
|
|
|
|
|
|
type WSHub struct {
|
|
|
|
API plugin.API
|
|
|
|
handleWSMessage func(data []byte)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *WSHub) SendWSMessage(data []byte) {
|
2021-07-09 12:59:44 +02:00
|
|
|
err := h.API.PublishPluginClusterEvent(model.PluginClusterEvent{
|
2021-05-24 19:06:11 +02:00
|
|
|
Id: "websocket_event",
|
|
|
|
Data: data,
|
|
|
|
}, model.PluginClusterEventSendOptions{
|
|
|
|
SendType: model.PluginClusterEventSendTypeReliable,
|
|
|
|
})
|
2021-07-09 12:59:44 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.API.LogError("Error sending websocket message", map[string]interface{}{"err": err})
|
|
|
|
}
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *WSHub) SetReceiveWSMessage(handler func(data []byte)) {
|
|
|
|
h.handleWSMessage = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) OnActivate() error {
|
|
|
|
mmconfig := p.API.GetUnsanitizedConfig()
|
|
|
|
filesS3Config := config.AmazonS3Config{}
|
|
|
|
if mmconfig.FileSettings.AmazonS3AccessKeyId != nil {
|
2021-07-09 03:09:02 +02:00
|
|
|
filesS3Config.AccessKeyID = *mmconfig.FileSettings.AmazonS3AccessKeyId
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3SecretAccessKey != nil {
|
|
|
|
filesS3Config.SecretAccessKey = *mmconfig.FileSettings.AmazonS3SecretAccessKey
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3Bucket != nil {
|
|
|
|
filesS3Config.Bucket = *mmconfig.FileSettings.AmazonS3Bucket
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3PathPrefix != nil {
|
|
|
|
filesS3Config.PathPrefix = *mmconfig.FileSettings.AmazonS3PathPrefix
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3Region != nil {
|
|
|
|
filesS3Config.Region = *mmconfig.FileSettings.AmazonS3Region
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3Endpoint != nil {
|
|
|
|
filesS3Config.Endpoint = *mmconfig.FileSettings.AmazonS3Endpoint
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3SSL != nil {
|
|
|
|
filesS3Config.SSL = *mmconfig.FileSettings.AmazonS3SSL
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3SignV2 != nil {
|
|
|
|
filesS3Config.SignV2 = *mmconfig.FileSettings.AmazonS3SignV2
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3SSE != nil {
|
|
|
|
filesS3Config.SSE = *mmconfig.FileSettings.AmazonS3SSE
|
|
|
|
}
|
|
|
|
if mmconfig.FileSettings.AmazonS3Trace != nil {
|
|
|
|
filesS3Config.Trace = *mmconfig.FileSettings.AmazonS3Trace
|
|
|
|
}
|
|
|
|
|
2021-06-04 08:43:32 +02:00
|
|
|
logger := mlog.NewLogger()
|
2021-07-02 18:07:30 +02:00
|
|
|
cfgJSON := defaultLoggingConfig()
|
|
|
|
err := logger.Configure("", cfgJSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-04 08:43:32 +02:00
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
client := pluginapi.NewClient(p.API, p.Driver)
|
|
|
|
sqlDB, err := client.Store.GetMasterDB()
|
|
|
|
if err != nil {
|
2021-07-09 12:59:44 +02:00
|
|
|
return fmt.Errorf("error initializing the DB: %w", err)
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-07 06:43:06 +02:00
|
|
|
baseURL := ""
|
|
|
|
if mmconfig.ServiceSettings.SiteURL != nil {
|
|
|
|
baseURL = *mmconfig.ServiceSettings.SiteURL
|
|
|
|
}
|
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
cfg := &config.Configuration{
|
2021-07-07 06:43:06 +02:00
|
|
|
ServerRoot: baseURL + "/plugins/focalboard",
|
2021-05-24 19:06:11 +02:00
|
|
|
Port: -1,
|
|
|
|
DBType: *mmconfig.SqlSettings.DriverName,
|
|
|
|
DBConfigString: *mmconfig.SqlSettings.DataSource,
|
|
|
|
DBTablePrefix: "focalboard_",
|
|
|
|
UseSSL: false,
|
|
|
|
SecureCookie: true,
|
2021-06-21 12:08:33 +02:00
|
|
|
WebPath: path.Join(*mmconfig.PluginSettings.Directory, "focalboard", "pack"),
|
2021-05-24 19:06:11 +02:00
|
|
|
FilesDriver: *mmconfig.FileSettings.DriverName,
|
|
|
|
FilesPath: *mmconfig.FileSettings.Directory,
|
|
|
|
FilesS3Config: filesS3Config,
|
|
|
|
Telemetry: true,
|
|
|
|
WebhookUpdate: []string{},
|
|
|
|
SessionExpireTime: 2592000,
|
|
|
|
SessionRefreshTime: 18000,
|
|
|
|
LocalOnly: false,
|
|
|
|
EnableLocalMode: false,
|
|
|
|
LocalModeSocketLocation: "",
|
|
|
|
AuthMode: "mattermost",
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
|
|
|
var db store.Store
|
|
|
|
db, err = sqlstore.New(cfg.DBType, cfg.DBConfigString, cfg.DBTablePrefix, logger, sqlDB)
|
|
|
|
if err != nil {
|
2021-07-09 12:59:44 +02:00
|
|
|
return fmt.Errorf("error initializing the DB: %w", err)
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
|
|
|
if cfg.AuthMode == server.MattermostAuthMod {
|
2021-07-06 22:44:11 +02:00
|
|
|
layeredStore, err2 := mattermostauthlayer.New(cfg.DBType, sqlDB, db, logger)
|
2021-06-25 16:49:06 +02:00
|
|
|
if err2 != nil {
|
2021-07-09 12:59:44 +02:00
|
|
|
return fmt.Errorf("error initializing the DB: %w", err2)
|
2021-06-25 16:49:06 +02:00
|
|
|
}
|
|
|
|
db = layeredStore
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:03:56 +02:00
|
|
|
serverID := client.System.GetDiagnosticID()
|
|
|
|
|
|
|
|
server, err := server.New(cfg, "", db, logger, serverID)
|
2021-05-24 19:06:11 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("ERROR INITIALIZING THE SERVER", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.wsHub = &WSHub{API: p.API}
|
|
|
|
server.SetWSHub(p.wsHub)
|
|
|
|
p.server = server
|
|
|
|
return server.Start()
|
|
|
|
}
|
|
|
|
|
2021-07-09 12:59:44 +02:00
|
|
|
func (p *Plugin) OnPluginClusterEvent(_ *plugin.Context, ev model.PluginClusterEvent) {
|
2021-05-24 19:06:11 +02:00
|
|
|
if ev.Id == "websocket_event" {
|
|
|
|
p.wsHub.handleWSMessage(ev.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) OnDeactivate() error {
|
|
|
|
return p.server.Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP demonstrates a plugin that handles HTTP requests by greeting the world.
|
2021-07-09 12:59:44 +02:00
|
|
|
func (p *Plugin) ServeHTTP(_ *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
2021-05-24 19:06:11 +02:00
|
|
|
router := p.server.GetRootRouter()
|
|
|
|
router.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2021-07-02 18:07:30 +02:00
|
|
|
func defaultLoggingConfig() string {
|
|
|
|
return `
|
|
|
|
{
|
|
|
|
"def": {
|
|
|
|
"type": "console",
|
|
|
|
"options": {
|
|
|
|
"out": "stdout"
|
|
|
|
},
|
|
|
|
"format": "plain",
|
|
|
|
"format_options": {
|
|
|
|
"delim": " ",
|
2021-07-27 18:57:29 +02:00
|
|
|
"min_level_len": 0,
|
|
|
|
"min_msg_len": 0,
|
|
|
|
"enable_color": false,
|
|
|
|
"enable_caller": true
|
2021-07-02 18:07:30 +02:00
|
|
|
},
|
|
|
|
"levels": [
|
|
|
|
{"id": 5, "name": "debug"},
|
|
|
|
{"id": 4, "name": "info", "color": 36},
|
|
|
|
{"id": 3, "name": "warn"},
|
|
|
|
{"id": 2, "name": "error", "color": 31},
|
|
|
|
{"id": 1, "name": "fatal", "stacktrace": true},
|
|
|
|
{"id": 0, "name": "panic", "stacktrace": true}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
}
|