1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00

Look for implicit documize.conf

@harveykandola

Now looks for implicit documize.conf, then specified .conf, then fallback to flags and envars.
This commit is contained in:
sauls8t 2019-04-28 14:54:48 +01:00
parent 1fefdaec9f
commit 34d1639899
5 changed files with 1173 additions and 1153 deletions

1
core/env/flags.go vendored
View file

@ -23,6 +23,7 @@ type Flags struct {
SSLKeyFile string // (optional) name of SSL key PEM file
SiteMode string // (optional) if 1 then serve offline web page
Location string // reserved
ConfigSource string // tells us if configuration info was obtained from command line or config file
}
// SSLEnabled returns true if both cert and key were provided at runtime.

42
core/env/parser.go vendored
View file

@ -21,10 +21,10 @@ import (
"github.com/BurntSushi/toml"
)
// ParseFlags loads runtime parameters like port numbers and DB connections.
// LoadConfig 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) {
func LoadConfig() (f Flags, ok bool) {
// Check and process config file
f, ok = configFile()
@ -33,24 +33,45 @@ func ParseFlags() (f Flags, ok bool) {
f, ok = commandLineEnv()
}
// reserved
if len(f.Location) == 0 {
f.Location = "selfhost"
}
return
}
// configFile checks for the presence of exactly one command argument
// configFile checks for the presence of zero or one command argument.
// If no arguments are provided then we look for and load documize.conf file.
// If one argument is provided then we load the specified config file.
// If more than one argument is provided then we exit as flags as have passed.
// checks to see if it is a TOML format config file.
func configFile() (f Flags, ok bool) {
ok = false
// We are expecting: ./documize sample.conf
var configFile string
if len(os.Args) != 2 {
// First argument is always program being executed.
// No additional arguments means check for documize.conf file.
if len(os.Args) == 1 {
// No arguments, so we default to default config filename.
configFile = "documize.conf"
} else if len(os.Args) == 2 {
// Config filename passed in, so we use it.
configFile = os.Args[1]
} else {
// Too many arguments means flags passed in so we return.
return
}
configFile = os.Args[1]
// Does file exist?
if len(configFile) == 0 || !configFileExists(configFile) {
return
}
// Tell caller where the config came from.
f.ConfigSource = configFile
// So now we have file and we parse the TOML format.
var ct ConfigToml
if _, err := toml.DecodeFile(configFile, &ct); err != nil {
@ -66,6 +87,7 @@ func configFile() (f Flags, ok bool) {
f.ForceHTTPPort2SSL = strconv.Itoa(ct.HTTP.ForceSSLPort)
f.SSLCertFile = ct.HTTP.Cert
f.SSLKeyFile = ct.HTTP.Key
f.Location = strings.ToLower(ct.Install.Location)
ok = true
return
@ -99,12 +121,8 @@ func commandLineEnv() (f Flags, ok bool) {
f.SiteMode = siteMode
f.SSLCertFile = certFile
f.SSLKeyFile = keyFile
// reserved
if len(location) == 0 {
location = "selfhost"
}
f.Location = strings.ToLower(location)
f.ConfigSource = "flags/environment"
return f, ok
}

View file

@ -28,16 +28,16 @@ import (
)
func main() {
// runtime stores server/application level information
// Runtime stores server/application information.
rt := env.Runtime{}
// wire up logging implementation
// Wire up logging implementation.
rt.Log = logging.NewLogger(false)
// wire up embedded web assets handler
// Wire up embedded web assets handler.
web.Embed = embed.NewEmbedder()
// product details
// Specify the product edition.
rt.Product = domain.Product{}
rt.Product.Major = "2"
rt.Product.Minor = "4"
@ -50,21 +50,24 @@ func main() {
// Setup data store.
s := store.Store{}
// Parse flags/envars.
// Parse configuration information.
flagsOK := false
rt.Flags, flagsOK = env.ParseFlags()
rt.Flags, flagsOK = env.LoadConfig()
if !flagsOK {
os.Exit(0)
}
rt.Log.Info("Configuration source: " + rt.Flags.ConfigSource)
// Start database init.
bootOK := boot.InitRuntime(&rt, &s)
if bootOK {
// runtime.Log = runtime.Log.SetDB(runtime.Db)
}
// Register smart sections
// Register document sections.
section.Register(&rt, &s)
// Start web server.
ready := make(chan struct{}, 1) // channel signals router ready
server.Start(&rt, &s, ready)
}

File diff suppressed because one or more lines are too long

View file

@ -124,8 +124,6 @@ func Start(rt *env.Runtime, s *store.Store, ready chan struct{}) {
rt.Log.Info("Web Server: starting SSL server on " + rt.Flags.HTTPPort + " with " + rt.Flags.SSLCertFile + " " + rt.Flags.SSLKeyFile)
// TODO: https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/
server := &http.Server{Addr: ":" + rt.Flags.HTTPPort, Handler: n /*, TLSConfig: myTLSConfig*/}
server.SetKeepAlivesEnabled(true)