1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 21:29:42 +02:00
documize/core/env/flags.go

164 lines
5 KiB
Go
Raw Normal View History

2016-07-07 18:54:16 -07:00
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
2016-07-07 18:54:16 -07:00
// 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>.
2016-07-07 18:54:16 -07:00
//
// https://documize.com
2017-07-19 18:47:01 +01:00
// Package env provides runtime, server level setup and configuration
2017-07-18 21:55:17 +01:00
package env
2016-07-07 18:54:16 -07:00
import (
"flag"
"fmt"
"os"
"sort"
"strings"
"sync"
2016-07-07 18:54:16 -07:00
)
2017-07-19 18:47:01 +01:00
// Flags provides access to environment and command line switches for this program.
type Flags struct {
DBConn string // database connection string
Salt string // the salt string used to encode JWT tokens
DBType string // database type
2017-07-19 18:47:01 +01:00
SSLCertFile string // (optional) name of SSL certificate PEM file
SSLKeyFile string // (optional) name of SSL key PEM file
HTTPPort string // (optional) HTTP or HTTPS port
ForceHTTPPort2SSL string // (optional) HTTP that should be redirected to HTTPS
SiteMode string // (optional) if 1 then serve offline web page
Location string // reserved
2017-07-19 18:47:01 +01:00
}
// SSLEnabled returns true if both cert and key were provided at runtime.
func (f *Flags) SSLEnabled() bool {
return f.SSLCertFile != "" && f.SSLKeyFile != ""
}
2016-07-07 18:54:16 -07:00
2017-07-19 18:47:01 +01:00
type flagItem struct {
2016-07-07 18:54:16 -07:00
target *string
name, setter, value string
required bool
}
2017-07-19 18:47:01 +01:00
type progFlags struct {
items []flagItem
2016-07-07 18:54:16 -07:00
}
// Len is part of sort.Interface.
2017-07-19 18:47:01 +01:00
func (v *progFlags) Len() int {
return len(v.items)
2016-07-07 18:54:16 -07:00
}
// Swap is part of sort.Interface.
2017-07-19 18:47:01 +01:00
func (v *progFlags) Swap(i, j int) {
v.items[i], v.items[j] = v.items[j], v.items[i]
2016-07-07 18:54:16 -07:00
}
// Less is part of sort.Interface.
2017-07-19 18:47:01 +01:00
func (v *progFlags) Less(i, j int) bool {
return v.items[i].name < v.items[j].name
2016-07-07 18:54:16 -07:00
}
2017-07-19 18:47:01 +01:00
// prefix provides the prefix for all environment variables
const prefix = "DOCUMIZE"
2016-07-07 18:54:16 -07:00
const goInit = "(default)"
2017-07-19 18:47:01 +01:00
var flagList progFlags
var loadMutex sync.Mutex
// ParseFlags loads command line and OS environment variables required by the program to function.
func ParseFlags() (f Flags, ok bool) {
ok = true
var dbConn, dbType, jwtKey, siteMode, port, certFile, keyFile, forcePort2SSL, location string
2017-07-19 18:47:01 +01:00
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")
2019-03-26 08:51:02 -04:00
register(&dbType, "dbtype", true, "specify the database provider: mysql|percona|mariadb|postgresql|sqlserver")
2018-09-12 20:03:34 +01:00
register(&dbConn, "db", true, `'database specific connection string for example "user:password@tcp(localhost:3306)/dbname"`)
register(&location, "location", false, `reserved`)
2017-07-19 18:47:01 +01:00
if !parse("db") {
ok = false
}
2017-07-19 18:47:01 +01:00
2019-03-26 08:51:02 -04:00
f.DBType = strings.ToLower(dbType)
2017-07-19 18:47:01 +01:00
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
2017-07-19 18:47:01 +01:00
}
// register prepares flag for subsequent parsing
func register(target *string, name string, required bool, usage string) {
loadMutex.Lock()
defer loadMutex.Unlock()
2016-07-07 18:54:16 -07:00
name = strings.ToLower(strings.TrimSpace(name))
2017-07-19 18:47:01 +01:00
setter := prefix + strings.ToUpper(name)
2016-07-07 18:54:16 -07:00
value := os.Getenv(setter)
if value == "" {
value = *target // use the Go initialized value
setter = goInit
}
2017-07-19 18:47:01 +01:00
2016-07-07 18:54:16 -07:00
flag.StringVar(target, name, value, usage)
2017-07-19 18:47:01 +01:00
flagList.items = append(flagList.items, flagItem{target: target, name: name, required: required, value: value, setter: setter})
2016-07-07 18:54:16 -07:00
}
2017-07-19 18:47:01 +01:00
// parse loads flags from OS environment and command line switches
func parse(doFirst string) (ok bool) {
2017-07-19 18:47:01 +01:00
loadMutex.Lock()
defer loadMutex.Unlock()
2016-07-07 18:54:16 -07:00
flag.Parse()
2017-07-19 18:47:01 +01:00
sort.Sort(&flagList)
2016-07-07 18:54:16 -07:00
for pass := 1; pass <= 2; pass++ {
2017-07-19 18:47:01 +01:00
for vi, v := range flagList.items {
2016-07-07 18:54:16 -07:00
if (pass == 1 && v.name == doFirst) || (pass == 2 && v.name != doFirst) {
if v.value != *(v.target) || (v.value != "" && *(v.target) == "") {
2017-07-19 18:47:01 +01:00
flagList.items[vi].setter = "-" + v.name // v is a local copy, not the underlying data
2016-07-07 18:54:16 -07:00
}
if v.required {
if *(v.target) == "" {
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "In order to run", os.Args[0], "the following must be provided:")
2017-07-19 18:47:01 +01:00
for _, vv := range flagList.items {
2016-07-07 18:54:16 -07:00
if vv.required {
fmt.Fprintf(os.Stderr, "* setting from environment variable '%s' or flag '-%s' or an application setting '%s', current value: '%s' set by '%s'\n",
2017-07-19 18:47:01 +01:00
prefix+strings.ToUpper(vv.name), vv.name, vv.name, *(vv.target), vv.setter)
2016-07-07 18:54:16 -07:00
}
}
fmt.Fprintln(os.Stderr)
flag.Usage()
return false
2016-07-07 18:54:16 -07:00
}
}
}
}
}
return true
2016-07-07 18:54:16 -07:00
}