mirror of
https://github.com/documize/community.git
synced 2025-07-19 13:19:43 +02:00
Specify runtime parameters in TOML format config file
In addition to specifying parameters in the command line and environment variables, you can now omit all parameters and provide a config file. Example: ./documize myconfig.conf Note: there is no switch setting, just provide the filename as an argument. Fixes #243
This commit is contained in:
parent
d151555597
commit
8f1bc8ce1f
26 changed files with 3791 additions and 127 deletions
122
core/env/parser.go
vendored
Normal file
122
core/env/parser.go
vendored
Normal file
|
@ -0,0 +1,122 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
// Package env provides runtime, server level setup and configuration
|
||||
package env
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// ParseFlags loads runtime parameters like port numbers and DB connections.
|
||||
// We first check for -config switch that would point us towards a .CONF file.
|
||||
// If not found, we then read parameters from command line and environment vars.
|
||||
func ParseFlags() (f Flags, ok bool) {
|
||||
// Check and process config file
|
||||
f, ok = configFile()
|
||||
|
||||
// If not OK then get parameters from command line and environment variables.
|
||||
if !ok {
|
||||
f, ok = commandLineEnv()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// configFile read parameters from TOML format config file.
|
||||
func configFile() (f Flags, ok bool) {
|
||||
ok = false
|
||||
|
||||
// See if we have -config parameter -- return false to signal no config file specified.
|
||||
var configFile string
|
||||
// flag.StringVar(&configFile, "config", "", "specify path to TOML format config file")
|
||||
// flag.Parse()
|
||||
|
||||
if len(os.Args) != 2 {
|
||||
return
|
||||
}
|
||||
|
||||
configFile = os.Args[1]
|
||||
if len(configFile) == 0 || !configFileExists(configFile) {
|
||||
return
|
||||
}
|
||||
|
||||
// So now we have file and we parse the TOML format.
|
||||
var ct ConfigToml
|
||||
if _, err := toml.DecodeFile(configFile, &ct); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// TOML format cofig file is good so we map to flags.
|
||||
f.DBType = strings.ToLower(ct.Database.Type)
|
||||
f.DBConn = ct.Database.Connection
|
||||
f.Salt = ct.Database.Salt
|
||||
f.HTTPPort = strconv.Itoa(ct.HTTP.Port)
|
||||
f.ForceHTTPPort2SSL = strconv.Itoa(ct.HTTP.ForceSSLPort)
|
||||
f.SSLCertFile = ct.HTTP.Cert
|
||||
f.SSLKeyFile = ct.HTTP.Key
|
||||
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
|
||||
// commandLineEnv loads command line and OS environment variables required by the program to function.
|
||||
func commandLineEnv() (f Flags, ok bool) {
|
||||
ok = true
|
||||
var dbConn, dbType, jwtKey, siteMode, port, certFile, keyFile, forcePort2SSL, location string
|
||||
|
||||
// register(&configFile, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated")
|
||||
register(&jwtKey, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated")
|
||||
register(&certFile, "cert", false, "the cert.pem file used for https")
|
||||
register(&keyFile, "key", false, "the key.pem file used for https")
|
||||
register(&port, "port", false, "http/https port number")
|
||||
register(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS")
|
||||
register(&siteMode, "offline", false, "set to '1' for OFFLINE mode")
|
||||
register(&dbType, "dbtype", true, "specify the database provider: mysql|percona|mariadb|postgresql|sqlserver")
|
||||
register(&dbConn, "db", true, `'database specific connection string for example "user:password@tcp(localhost:3306)/dbname"`)
|
||||
register(&location, "location", false, `reserved`)
|
||||
|
||||
if !parse("db") {
|
||||
ok = false
|
||||
}
|
||||
|
||||
f.DBType = strings.ToLower(dbType)
|
||||
f.DBConn = dbConn
|
||||
f.ForceHTTPPort2SSL = forcePort2SSL
|
||||
f.HTTPPort = port
|
||||
f.Salt = jwtKey
|
||||
f.SiteMode = siteMode
|
||||
f.SSLCertFile = certFile
|
||||
f.SSLKeyFile = keyFile
|
||||
|
||||
// reserved
|
||||
if len(location) == 0 {
|
||||
location = "selfhost"
|
||||
}
|
||||
f.Location = strings.ToLower(location)
|
||||
|
||||
return f, ok
|
||||
}
|
||||
|
||||
func configFileExists(fn string) bool {
|
||||
info, err := os.Stat(fn)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
|
||||
return !info.IsDir()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue