2021-02-17 21:29:20 +02:00
|
|
|
// Server for Focalboard
|
2020-10-08 18:21:27 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-01 21:36:36 +02:00
|
|
|
"C"
|
2020-10-08 18:21:27 +02:00
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-03-01 21:36:36 +02:00
|
|
|
"os/signal"
|
2020-10-22 15:22:36 +02:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2020-10-16 19:12:53 +02:00
|
|
|
|
2021-01-27 00:13:46 +02:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
|
|
|
"github.com/mattermost/focalboard/server/server"
|
|
|
|
"github.com/mattermost/focalboard/server/services/config"
|
2020-10-08 18:21:27 +02:00
|
|
|
)
|
2021-05-29 08:23:10 +02:00
|
|
|
import (
|
2021-08-25 22:08:01 +02:00
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2021-05-29 08:23:10 +02:00
|
|
|
)
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2021-03-01 21:36:36 +02:00
|
|
|
// Active server used with shared code (dll)
|
|
|
|
var pServer *server.Server
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
const (
|
|
|
|
timeBetweenPidMonitoringChecks = 2 * time.Second
|
|
|
|
)
|
|
|
|
|
2020-10-08 18:21:27 +02:00
|
|
|
func isProcessRunning(pid int) bool {
|
|
|
|
process, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err = process.Signal(syscall.Signal(0))
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-18 02:07:35 +02:00
|
|
|
return err == nil
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:36:36 +02:00
|
|
|
// monitorPid is used to keep the server lifetime in sync with another (client app) process
|
2021-05-29 08:23:10 +02:00
|
|
|
func monitorPid(pid int, logger *mlog.Logger) {
|
|
|
|
logger.Info("Monitoring PID", mlog.Int("pid", pid))
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-08 18:21:27 +02:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if !isProcessRunning(pid) {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Info("Monitored process not found, exiting.")
|
2020-10-08 18:21:27 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
|
|
|
time.Sleep(timeBetweenPidMonitoringChecks)
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
func logInfo(logger *mlog.Logger) {
|
|
|
|
logger.Info("FocalBoard Server",
|
|
|
|
mlog.String("version", model.CurrentVersion),
|
|
|
|
mlog.String("edition", model.Edition),
|
|
|
|
mlog.String("build_number", model.BuildNumber),
|
|
|
|
mlog.String("build_date", model.BuildDate),
|
|
|
|
mlog.String("build_hash", model.BuildHash),
|
|
|
|
)
|
2021-03-01 21:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-10-26 14:19:20 +02:00
|
|
|
// Command line args
|
|
|
|
pMonitorPid := flag.Int("monitorpid", -1, "a process ID")
|
|
|
|
pPort := flag.Int("port", 0, "the port number")
|
|
|
|
pSingleUser := flag.Bool("single-user", false, "single user mode")
|
|
|
|
pDBType := flag.String("dbtype", "", "Database type")
|
|
|
|
pDBConfig := flag.String("dbconfig", "", "Database config")
|
|
|
|
pConfigFilePath := flag.String(
|
|
|
|
"config",
|
|
|
|
"",
|
|
|
|
"Location of the JSON config file",
|
|
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
config, err := config.ReadConfigFile(*pConfigFilePath)
|
2020-10-09 10:27:20 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to read the config file: ", err)
|
|
|
|
return
|
|
|
|
}
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2021-08-25 22:08:01 +02:00
|
|
|
logger, _ := mlog.NewLogger()
|
2021-06-18 16:01:36 +02:00
|
|
|
cfgJSON := config.LoggingCfgJSON
|
|
|
|
if config.LoggingCfgFile == "" && cfgJSON == "" {
|
2021-06-11 14:24:51 +02:00
|
|
|
// if no logging defined, use default config (console output)
|
|
|
|
cfgJSON = defaultLoggingConfig()
|
|
|
|
}
|
2021-09-15 15:01:05 +02:00
|
|
|
err = logger.Configure(config.LoggingCfgFile, cfgJSON, nil)
|
2021-05-29 08:23:10 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error in config file for logger: ", err)
|
|
|
|
return
|
|
|
|
}
|
2021-06-21 11:21:42 +02:00
|
|
|
defer func() { _ = logger.Shutdown() }()
|
2021-05-29 08:23:10 +02:00
|
|
|
|
|
|
|
if logger.HasTargets() {
|
2021-08-25 22:08:01 +02:00
|
|
|
restore := logger.RedirectStdLog(mlog.LvlInfo, mlog.String("src", "stdlog"))
|
2021-05-29 08:23:10 +02:00
|
|
|
defer restore()
|
|
|
|
}
|
|
|
|
|
|
|
|
logInfo(logger)
|
|
|
|
|
2021-02-11 20:13:17 +02:00
|
|
|
singleUser := false
|
|
|
|
if pSingleUser != nil {
|
|
|
|
singleUser = *pSingleUser
|
|
|
|
}
|
|
|
|
|
2021-02-09 22:27:34 +02:00
|
|
|
singleUserToken := ""
|
2021-02-11 20:13:17 +02:00
|
|
|
if singleUser {
|
|
|
|
singleUserToken = os.Getenv("FOCALBOARD_SINGLE_USER_TOKEN")
|
|
|
|
if len(singleUserToken) < 1 {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Fatal("The FOCALBOARD_SINGLE_USER_TOKEN environment variable must be set for single user mode ")
|
2021-02-11 20:13:17 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Info("Single user mode")
|
2020-12-04 12:28:35 +02:00
|
|
|
}
|
2020-10-08 18:21:27 +02:00
|
|
|
|
|
|
|
if pMonitorPid != nil && *pMonitorPid > 0 {
|
2021-05-29 08:23:10 +02:00
|
|
|
monitorPid(*pMonitorPid, logger)
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:36:36 +02:00
|
|
|
// Override config from commandline
|
|
|
|
|
|
|
|
if pDBType != nil && len(*pDBType) > 0 {
|
|
|
|
config.DBType = *pDBType
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Info("DBType from commandline", mlog.String("DBType", *pDBType))
|
2021-03-01 21:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if pDBConfig != nil && len(*pDBConfig) > 0 {
|
|
|
|
config.DBConfigString = *pDBConfig
|
|
|
|
// Don't echo, as the confix string may contain passwords
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Info("DBConfigString overriden from commandline")
|
2021-03-01 21:36:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 18:21:27 +02:00
|
|
|
if pPort != nil && *pPort > 0 && *pPort != config.Port {
|
|
|
|
// Override port
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Info("Port from commandline", mlog.Int("port", *pPort))
|
2020-10-08 18:21:27 +02:00
|
|
|
config.Port = *pPort
|
|
|
|
}
|
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
db, err := server.NewStore(config, logger)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("server.NewStore ERROR", mlog.Err(err))
|
|
|
|
}
|
|
|
|
|
2021-09-13 21:36:36 +02:00
|
|
|
params := server.Params{
|
|
|
|
Cfg: config,
|
|
|
|
SingleUserToken: singleUserToken,
|
|
|
|
DBStore: db,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := server.New(params)
|
2020-10-09 10:08:38 +02:00
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Fatal("server.New ERROR", mlog.Err(err))
|
2020-10-09 10:08:38 +02:00
|
|
|
}
|
2020-10-08 18:21:27 +02:00
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
if err := server.Start(); err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Fatal("server.Start ERROR", mlog.Err(err))
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
2021-03-01 21:36:36 +02:00
|
|
|
|
|
|
|
// Setting up signal capturing
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stop, os.Interrupt)
|
|
|
|
|
|
|
|
// Waiting for SIGINT (pkill -2)
|
|
|
|
<-stop
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
_ = server.Shutdown()
|
2021-03-01 21:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartServer starts the server
|
|
|
|
//export StartServer
|
2021-10-26 14:19:20 +02:00
|
|
|
func StartServer(webPath *C.char, filesPath *C.char, port int, singleUserToken, dbConfigString, configFilePath *C.char) {
|
2021-03-01 21:36:36 +02:00
|
|
|
startServer(
|
|
|
|
C.GoString(webPath),
|
2021-04-22 03:25:26 +02:00
|
|
|
C.GoString(filesPath),
|
2021-03-01 21:36:36 +02:00
|
|
|
port,
|
|
|
|
C.GoString(singleUserToken),
|
|
|
|
C.GoString(dbConfigString),
|
2021-10-26 14:19:20 +02:00
|
|
|
C.GoString(configFilePath),
|
2021-03-01 21:36:36 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopServer stops the server
|
|
|
|
//export StopServer
|
|
|
|
func StopServer() {
|
|
|
|
stopServer()
|
|
|
|
}
|
|
|
|
|
2021-10-26 14:19:20 +02:00
|
|
|
func startServer(webPath string, filesPath string, port int, singleUserToken, dbConfigString, configFilePath string) {
|
2021-03-01 21:36:36 +02:00
|
|
|
if pServer != nil {
|
|
|
|
stopServer()
|
|
|
|
pServer = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// config.json file
|
2021-10-26 14:19:20 +02:00
|
|
|
config, err := config.ReadConfigFile(configFilePath)
|
2021-03-01 21:36:36 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Unable to read the config file: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 22:08:01 +02:00
|
|
|
logger, _ := mlog.NewLogger()
|
2021-09-15 15:01:05 +02:00
|
|
|
err = logger.Configure(config.LoggingCfgFile, config.LoggingCfgJSON, nil)
|
2021-05-29 08:23:10 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error in config file for logger: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logInfo(logger)
|
|
|
|
|
2021-04-22 03:25:26 +02:00
|
|
|
if len(filesPath) > 0 {
|
|
|
|
config.FilesPath = filesPath
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:36:36 +02:00
|
|
|
if len(webPath) > 0 {
|
|
|
|
config.WebPath = webPath
|
|
|
|
}
|
|
|
|
|
|
|
|
if port > 0 {
|
|
|
|
config.Port = port
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbConfigString) > 0 {
|
|
|
|
config.DBConfigString = dbConfigString
|
|
|
|
}
|
|
|
|
|
2021-06-25 16:49:06 +02:00
|
|
|
db, err := server.NewStore(config, logger)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatal("server.NewStore ERROR", mlog.Err(err))
|
|
|
|
}
|
|
|
|
|
2021-09-13 21:36:36 +02:00
|
|
|
params := server.Params{
|
|
|
|
Cfg: config,
|
|
|
|
SingleUserToken: singleUserToken,
|
|
|
|
DBStore: db,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
pServer, err = server.New(params)
|
2021-03-01 21:36:36 +02:00
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Fatal("server.New ERROR", mlog.Err(err))
|
2021-03-01 21:36:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := pServer.Start(); err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
logger.Fatal("server.Start ERROR", mlog.Err(err))
|
2021-03-01 21:36:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopServer() {
|
|
|
|
if pServer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := pServer.Shutdown()
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
pServer.Logger().Error("server.Shutdown ERROR", mlog.Err(err))
|
2021-03-01 21:36:36 +02:00
|
|
|
}
|
2021-06-21 11:21:42 +02:00
|
|
|
_ = pServer.Logger().Shutdown()
|
2021-05-29 08:23:10 +02:00
|
|
|
pServer = nil
|
2020-10-08 18:21:27 +02:00
|
|
|
}
|
2021-06-11 14:24:51 +02:00
|
|
|
|
|
|
|
func defaultLoggingConfig() string {
|
|
|
|
return `
|
|
|
|
{
|
|
|
|
"def": {
|
|
|
|
"type": "console",
|
|
|
|
"options": {
|
|
|
|
"out": "stdout"
|
|
|
|
},
|
|
|
|
"format": "plain",
|
|
|
|
"format_options": {
|
|
|
|
"delim": " ",
|
|
|
|
"min_level_len": 5,
|
|
|
|
"min_msg_len": 40,
|
2021-07-27 18:57:29 +02:00
|
|
|
"enable_color": true,
|
|
|
|
"enable_caller": true
|
2021-06-11 14:24:51 +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}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
}
|