From 5848ea1c07e66e484e17b7b252f8e4442a1e5079 Mon Sep 17 00:00:00 2001 From: Archit Mathur Date: Tue, 26 Oct 2021 17:49:20 +0530 Subject: [PATCH] [GH-1256] feat: config file flag (#1418) * feat: adds support for --config * feat: adds support for configFilePath in startServer * chore: removes ugly spacing --- README.md | 2 +- server/main/main.go | 31 ++++++++++++++++++------------- server/services/config/config.go | 11 +++++++---- win-wpf/Focalboard/App.xaml.cs | 5 +++-- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a7fcf3b26..b2a735555 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ make To start the server, run `./bin/focalboard-server` -Server settings are in config.json. +Server settings are in config.json (or the path specified with --config). Open a browser to [http://localhost:8000](http://localhost:8000) to start. diff --git a/server/main/main.go b/server/main/main.go index 3a4fa4a5c..085e89f18 100644 --- a/server/main/main.go +++ b/server/main/main.go @@ -87,8 +87,20 @@ func logInfo(logger *mlog.Logger) { } func main() { - // config.json file - config, err := config.ReadConfigFile() + // 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) if err != nil { log.Fatal("Unable to read the config file: ", err) return @@ -114,14 +126,6 @@ func main() { logInfo(logger) - // Command line args - pMonitorPid := flag.Int("monitorpid", -1, "a process ID") - pPort := flag.Int("port", config.Port, "the port number") - pSingleUser := flag.Bool("single-user", false, "single user mode") - pDBType := flag.String("dbtype", "", "Database type") - pDBConfig := flag.String("dbconfig", "", "Database config") - flag.Parse() - singleUser := false if pSingleUser != nil { singleUser = *pSingleUser @@ -193,13 +197,14 @@ func main() { // StartServer starts the server //export StartServer -func StartServer(webPath *C.char, filesPath *C.char, port int, singleUserToken, dbConfigString *C.char) { +func StartServer(webPath *C.char, filesPath *C.char, port int, singleUserToken, dbConfigString, configFilePath *C.char) { startServer( C.GoString(webPath), C.GoString(filesPath), port, C.GoString(singleUserToken), C.GoString(dbConfigString), + C.GoString(configFilePath), ) } @@ -209,14 +214,14 @@ func StopServer() { stopServer() } -func startServer(webPath string, filesPath string, port int, singleUserToken, dbConfigString string) { +func startServer(webPath string, filesPath string, port int, singleUserToken, dbConfigString, configFilePath string) { if pServer != nil { stopServer() pServer = nil } // config.json file - config, err := config.ReadConfigFile() + config, err := config.ReadConfigFile(configFilePath) if err != nil { log.Fatal("Unable to read the config file: ", err) return diff --git a/server/services/config/config.go b/server/services/config/config.go index a0ee0cf7a..f8ea2226a 100644 --- a/server/services/config/config.go +++ b/server/services/config/config.go @@ -59,10 +59,13 @@ type Configuration struct { } // ReadConfigFile read the configuration from the filesystem. -func ReadConfigFile() (*Configuration, error) { - viper.SetConfigName("config") // name of config file (without extension) - viper.SetConfigType("json") // REQUIRED if the config file does not have the extension in the name - viper.AddConfigPath(".") // optionally look for config in the working directory +func ReadConfigFile(configFilePath string) (*Configuration, error) { + if configFilePath == "" { + viper.SetConfigFile("./config.json") + } else { + viper.SetConfigFile(configFilePath) + } + viper.SetEnvPrefix("focalboard") viper.AutomaticEnv() // read config values from env like FOCALBOARD_SERVERROOT=... viper.SetDefault("ServerRoot", DefaultServerRoot) diff --git a/win-wpf/Focalboard/App.xaml.cs b/win-wpf/Focalboard/App.xaml.cs index d710ef871..7908bc748 100644 --- a/win-wpf/Focalboard/App.xaml.cs +++ b/win-wpf/Focalboard/App.xaml.cs @@ -116,7 +116,8 @@ namespace Focalboard { byte[] filesPathBytes = Encoding.UTF8.GetBytes(filesPath); byte[] sessionTokenBytes = Encoding.UTF8.GetBytes(sessionToken); byte[] dbPathBytes = Encoding.UTF8.GetBytes(dbPath); - GoFunctions.StartServer(webFolderBytes, filesPathBytes, port, sessionTokenBytes, dbPathBytes); + byte[] configFilePathBytes = Encoding.UTF8.GetBytes(""); + GoFunctions.StartServer(webFolderBytes, filesPathBytes, port, sessionTokenBytes, dbPathBytes, configFilePathBytes); Debug.WriteLine("Server started"); } @@ -148,7 +149,7 @@ namespace Focalboard { static class GoFunctions { [DllImport(@"focalboard-server.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] - public static extern void StartServer(byte[] webPath, byte[] filesPath, int port, byte[] singleUserToken, byte[] dbConfigString); + public static extern void StartServer(byte[] webPath, byte[] filesPath, int port, byte[] singleUserToken, byte[] dbConfigString, byte[] configFilePath); [DllImport(@"focalboard-server.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)] public static extern void StopServer();