mirror of
https://github.com/documize/community.git
synced 2025-08-08 15:05:28 +02:00
first-cut use of config table
This commit is contained in:
parent
0616ba42d6
commit
26bcd84958
18 changed files with 145 additions and 65 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/documize/community/documize/api/request"
|
||||||
"github.com/documize/community/wordsmith/environment"
|
"github.com/documize/community/wordsmith/environment"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,8 +14,8 @@ var endPoint = "https://api.documize.com"
|
||||||
var token string
|
var token string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
environment.GetString(&endPoint, "endpoint", false, "Documize end-point", nil)
|
environment.GetString(&endPoint, "endpoint", false, "Documize end-point", request.FlagFromDB)
|
||||||
environment.GetString(&token, "token", false, "Documize token", nil)
|
environment.GetString(&token, "token", false, "Documize token", request.FlagFromDB)
|
||||||
}
|
}
|
||||||
|
|
||||||
var transport = &http.Transport{
|
var transport = &http.Transport{
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
package convert
|
package convert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/documize/community/documize/api/convert/excerpt"
|
"github.com/documize/community/documize/api/convert/excerpt"
|
||||||
"github.com/documize/community/documize/api/convert/html"
|
"github.com/documize/community/documize/api/convert/html"
|
||||||
"github.com/documize/community/documize/api/plugins"
|
"github.com/documize/community/documize/api/plugins"
|
||||||
"github.com/documize/community/wordsmith/api"
|
"github.com/documize/community/wordsmith/api"
|
||||||
"github.com/documize/community/wordsmith/utility"
|
"github.com/documize/community/wordsmith/utility"
|
||||||
"errors"
|
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/documize/community/documize/api/plugins"
|
"github.com/documize/community/documize/api/plugins"
|
||||||
|
"github.com/documize/community/documize/api/request"
|
||||||
"github.com/documize/community/documize/database"
|
"github.com/documize/community/documize/database"
|
||||||
"github.com/documize/community/documize/web"
|
"github.com/documize/community/documize/web"
|
||||||
"github.com/documize/community/wordsmith/environment"
|
"github.com/documize/community/wordsmith/environment"
|
||||||
|
@ -25,10 +26,10 @@ const (
|
||||||
var port, certFile, keyFile, forcePort2SSL string
|
var port, certFile, keyFile, forcePort2SSL string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
environment.GetString(&certFile, "cert", false, "the cert.pem file used for https", nil)
|
environment.GetString(&certFile, "cert", false, "the cert.pem file used for https", request.FlagFromDB)
|
||||||
environment.GetString(&keyFile, "key", false, "the key.pem file used for https", nil)
|
environment.GetString(&keyFile, "key", false, "the key.pem file used for https", request.FlagFromDB)
|
||||||
environment.GetString(&port, "port", false, "http/https port number", nil)
|
environment.GetString(&port, "port", false, "http/https port number", request.FlagFromDB)
|
||||||
environment.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", nil)
|
environment.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", request.FlagFromDB)
|
||||||
}
|
}
|
||||||
|
|
||||||
var testHost string // used during automated testing
|
var testHost string // used during automated testing
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
|
|
||||||
|
"github.com/documize/community/documize/api/request"
|
||||||
"github.com/documize/community/documize/web"
|
"github.com/documize/community/documize/web"
|
||||||
"github.com/documize/community/wordsmith/environment"
|
"github.com/documize/community/wordsmith/environment"
|
||||||
"github.com/documize/community/wordsmith/log"
|
"github.com/documize/community/wordsmith/log"
|
||||||
|
@ -258,11 +259,11 @@ var creds struct{ SMTPuserid, SMTPpassword, SMTPhost, SMTPport, SMTPsender strin
|
||||||
func init() {
|
func init() {
|
||||||
creds.SMTPport = "587" // the default value for outgoing SMTP traffic
|
creds.SMTPport = "587" // the default value for outgoing SMTP traffic
|
||||||
creds.SMTPsender = "Documize <hello@documize.com>" // TODO review as SAAS specific
|
creds.SMTPsender = "Documize <hello@documize.com>" // TODO review as SAAS specific
|
||||||
environment.GetString(&creds.SMTPuserid, "smtpuserid", false, "SMTP username for outgoing email", nil)
|
environment.GetString(&creds.SMTPuserid, "smtpuserid", false, "SMTP username for outgoing email", request.FlagFromDB)
|
||||||
environment.GetString(&creds.SMTPpassword, "smtppassword", false, "SMTP password for outgoing email", nil)
|
environment.GetString(&creds.SMTPpassword, "smtppassword", false, "SMTP password for outgoing email", request.FlagFromDB)
|
||||||
environment.GetString(&creds.SMTPhost, "smtphost", false, "SMTP host for outgoing email", nil)
|
environment.GetString(&creds.SMTPhost, "smtphost", false, "SMTP host for outgoing email", request.FlagFromDB)
|
||||||
environment.GetString(&creds.SMTPport, "smtpport", false, "SMTP port for outgoing email", nil)
|
environment.GetString(&creds.SMTPport, "smtpport", false, "SMTP port for outgoing email", request.FlagFromDB)
|
||||||
environment.GetString(&creds.SMTPsender, "smtpsender", false, "SMTP sender's e-mail for outgoing email", nil)
|
environment.GetString(&creds.SMTPsender, "smtpsender", false, "SMTP sender's e-mail for outgoing email", request.FlagFromDB)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to return SMTP credentials
|
// Helper to return SMTP credentials
|
||||||
|
|
|
@ -31,10 +31,10 @@ package mail
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/documize/community/wordsmith/log"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/documize/community/wordsmith/log"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
"mime"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/documize/community/documize/api/convert/documizeapi"
|
"github.com/documize/community/documize/api/convert/documizeapi"
|
||||||
"github.com/documize/community/documize/api/convert/html"
|
"github.com/documize/community/documize/api/convert/html"
|
||||||
"github.com/documize/community/documize/api/convert/md"
|
"github.com/documize/community/documize/api/convert/md"
|
||||||
|
"github.com/documize/community/documize/api/request"
|
||||||
"github.com/documize/community/wordsmith/api"
|
"github.com/documize/community/wordsmith/api"
|
||||||
"github.com/documize/community/wordsmith/environment"
|
"github.com/documize/community/wordsmith/environment"
|
||||||
"github.com/documize/community/wordsmith/log"
|
"github.com/documize/community/wordsmith/log"
|
||||||
|
@ -22,9 +23,9 @@ var insecure = "false"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
environment.GetString(&PluginFile, "plugin", false,
|
environment.GetString(&PluginFile, "plugin", false,
|
||||||
"the JSON file describing plugins, default 'plugin.json'", nil)
|
"the JSON file describing plugins, default 'plugin.json' set to 'PLUGIN' to configure from database settings", request.FlagFromDB)
|
||||||
environment.GetString(&insecure, "insecure", false,
|
environment.GetString(&insecure, "insecure", false,
|
||||||
"if 'true' allow https endpoints with invalid certificates (only for testing)", nil)
|
"if 'true' allow https endpoints with invalid certificates (only for testing)", request.FlagFromDB)
|
||||||
}
|
}
|
||||||
|
|
||||||
type infoLog struct{}
|
type infoLog struct{}
|
||||||
|
@ -98,13 +99,17 @@ func LibSetup() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
json, err := ioutil.ReadFile(PluginFile)
|
var json = make([]byte, 0)
|
||||||
|
if PluginFile == "PLUGIN" {
|
||||||
|
json = []byte(request.ConfigString(PluginFile, ""))
|
||||||
|
} else {
|
||||||
|
json, err = ioutil.ReadFile(PluginFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("Plugin file '" + PluginFile + "' not found, using no plugins")
|
log.Info("Plugin file '" + PluginFile + "' not found, using no plugins")
|
||||||
json = []byte(" [ ] \n")
|
json = []byte(" [ ] \n")
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
err = Lib.Configure(json)
|
err = Lib.Configure(json)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
54
documize/api/request/config.go
Normal file
54
documize/api/request/config.go
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
package request
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/documize/community/wordsmith/environment"
|
||||||
|
"github.com/documize/community/wordsmith/log"
|
||||||
|
"github.com/documize/community/wordsmith/utility"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FlagFromDB overrides the value in *target if it is set in the database configuration JSON.
|
||||||
|
// Function signiture must map that in environment
|
||||||
|
func FlagFromDB(target *string, name string) bool {
|
||||||
|
value := ConfigString(environment.Prefix, name)
|
||||||
|
//fmt.Println("DEBUG FlagFromDB " + value)
|
||||||
|
if value != `""` && value != "" {
|
||||||
|
*target = strings.TrimPrefix(strings.TrimSuffix(value, `"`), `"`)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigString fetches a configuration JSON element from the config table.
|
||||||
|
func ConfigString(area, path string) (ret string) {
|
||||||
|
if path != "" {
|
||||||
|
path = "." + path
|
||||||
|
}
|
||||||
|
sql := `SELECT JSON_EXTRACT(details,'$` + path + `') AS item FROM config WHERE area = '` + area + `';`
|
||||||
|
|
||||||
|
stmt, err := Db.Preparex(sql)
|
||||||
|
defer utility.Close(stmt)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Error(fmt.Sprintf("Unable to prepare select for ConfigString: %s", sql), err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var item = make([]uint8, 0)
|
||||||
|
|
||||||
|
err = stmt.Get(&item)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
//log.Error(fmt.Sprintf("Unable to execute select for ConfigString: %s", sql), err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(item) > 1 {
|
||||||
|
ret = string(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
//fmt.Println("DEBUG ConfigString " + sql + " => " + ret)
|
||||||
|
return ret
|
||||||
|
}
|
|
@ -38,7 +38,7 @@ func init() {
|
||||||
|
|
||||||
environment.GetString(&connectionString, "db", true,
|
environment.GetString(&connectionString, "db", true,
|
||||||
`"username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`,
|
`"username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`,
|
||||||
func() {
|
func(*string, string) bool {
|
||||||
Db, err = sqlx.Open("mysql", stdConn(connectionString))
|
Db, err = sqlx.Open("mysql", stdConn(connectionString))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -65,6 +65,7 @@ func init() {
|
||||||
log.Info("database.Check(Db) !OK, going into setup mode")
|
log.Info("database.Check(Db) !OK, going into setup mode")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false // value not changed
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
7
documize/database/scripts/migrate/migrate-00002.sql
Normal file
7
documize/database/scripts/migrate/migrate-00002.sql
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
DROP TABLE IF EXISTS `config`;
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS `config` (
|
||||||
|
`area` CHAR(16) NOT NULL,
|
||||||
|
`details` JSON,
|
||||||
|
UNIQUE INDEX `idx_config_area` (`area` ASC) ) ;
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
environment.Parse()
|
environment.Parse("db") // process the db value first
|
||||||
ready := make(chan struct{}, 1) // channel is used for testing
|
ready := make(chan struct{}, 1) // channel is used for testing
|
||||||
|
|
||||||
endpoint.Serve(ready)
|
endpoint.Serve(ready)
|
||||||
|
|
|
@ -33,7 +33,7 @@ var SiteInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
environment.GetString(&SiteMode, "offline", false, "set to '1' for OFFLINE mode", nil)
|
environment.GetString(&SiteMode, "offline", false, "set to '1' for OFFLINE mode", nil) // no sense overriding this setting from the DB
|
||||||
SiteInfo.DBhash = util.GenerateRandomPassword() // do this only once
|
SiteInfo.DBhash = util.GenerateRandomPassword() // do this only once
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,14 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// CallbackT is the type signature of the callback function of GetString().
|
||||||
|
type CallbackT func(*string, string) bool
|
||||||
|
|
||||||
type varT struct {
|
type varT struct {
|
||||||
target *string
|
target *string
|
||||||
name, setter, value string
|
name, setter, value string
|
||||||
required bool
|
required bool
|
||||||
callback func()
|
callback CallbackT
|
||||||
}
|
}
|
||||||
|
|
||||||
type varsT struct {
|
type varsT struct {
|
||||||
|
@ -44,7 +47,7 @@ const Prefix = "DOCUMIZE"
|
||||||
const goInit = "(default)"
|
const goInit = "(default)"
|
||||||
|
|
||||||
// GetString sets-up the flag for later use, it must be called before ParseOK(), usually in an init().
|
// GetString sets-up the flag for later use, it must be called before ParseOK(), usually in an init().
|
||||||
func GetString(target *string, name string, required bool, usage string, callback func()) {
|
func GetString(target *string, name string, required bool, usage string, callback CallbackT) {
|
||||||
name = strings.ToLower(strings.TrimSpace(name))
|
name = strings.ToLower(strings.TrimSpace(name))
|
||||||
setter := Prefix + strings.ToUpper(name)
|
setter := Prefix + strings.ToUpper(name)
|
||||||
value := os.Getenv(setter)
|
value := os.Getenv(setter)
|
||||||
|
@ -59,22 +62,29 @@ func GetString(target *string, name string, required bool, usage string, callbac
|
||||||
// Parse calls flag.Parse() then checks that the required environment variables are all set.
|
// Parse calls flag.Parse() then checks that the required environment variables are all set.
|
||||||
// It should be the first thing called by any main() that uses this library.
|
// It should be the first thing called by any main() that uses this library.
|
||||||
// If all the required variables are not present, it prints an error and calls os.Exit(2) like flag.Parse().
|
// If all the required variables are not present, it prints an error and calls os.Exit(2) like flag.Parse().
|
||||||
func Parse() {
|
func Parse(doFirst string) {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
sort.Sort(&vars)
|
sort.Sort(&vars)
|
||||||
|
for pass := 1; pass <= 2; pass++ {
|
||||||
for vi, v := range vars.vv {
|
for vi, v := range vars.vv {
|
||||||
|
if (pass == 1 && v.name == doFirst) || (pass == 2 && v.name != doFirst) {
|
||||||
typ := "Optional"
|
typ := "Optional"
|
||||||
if v.value != *(v.target) || (v.value != "" && *(v.target) == "") {
|
if v.value != *(v.target) || (v.value != "" && *(v.target) == "") {
|
||||||
vars.vv[vi].setter = "-" + v.name // v is a local copy, not the underlying data
|
vars.vv[vi].setter = "-" + v.name // v is a local copy, not the underlying data
|
||||||
}
|
}
|
||||||
|
if v.callback != nil {
|
||||||
|
if v.callback(v.target, v.name) {
|
||||||
|
vars.vv[vi].setter = "setting:" + v.name // v is a local copy, not the underlying data
|
||||||
|
}
|
||||||
|
}
|
||||||
if v.required {
|
if v.required {
|
||||||
if *(v.target) == "" {
|
if *(v.target) == "" {
|
||||||
fmt.Fprintln(os.Stderr)
|
fmt.Fprintln(os.Stderr)
|
||||||
fmt.Fprintln(os.Stderr, "In order to run", os.Args[0], "the following must be provided:")
|
fmt.Fprintln(os.Stderr, "In order to run", os.Args[0], "the following must be provided:")
|
||||||
for _, vv := range vars.vv {
|
for _, vv := range vars.vv {
|
||||||
if vv.required {
|
if vv.required {
|
||||||
fmt.Fprintf(os.Stderr, "* setting from environment variable '%s' or flag '-%s', current value: '%s' set by '%s'\n",
|
fmt.Fprintf(os.Stderr, "* setting from environment variable '%s' or flag '-%s' or an application setting '%s', current value: '%s' set by '%s'\n",
|
||||||
Prefix+strings.ToUpper(vv.name), vv.name, *(vv.target), vv.setter)
|
Prefix+strings.ToUpper(vv.name), vv.name, vv.name, *(vv.target), vv.setter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Fprintln(os.Stderr)
|
fmt.Fprintln(os.Stderr)
|
||||||
|
@ -84,12 +94,11 @@ func Parse() {
|
||||||
}
|
}
|
||||||
typ = "Required"
|
typ = "Required"
|
||||||
}
|
}
|
||||||
if *(v.target) != "" && v.setter != goInit {
|
if *(v.target) != "" && vars.vv[vi].setter != goInit {
|
||||||
fmt.Fprintf(os.Stdout, "%s setting from '%s' is: '%s'\n",
|
fmt.Fprintf(os.Stdout, "%s setting from '%s' is: '%s'\n",
|
||||||
typ, v.setter, *(v.target))
|
typ, vars.vv[vi].setter, *(v.target))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if v.callback != nil {
|
|
||||||
v.callback()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,9 @@ func init() {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
env.GetString(&environment, "log", false,
|
env.GetString(&environment, "log", false,
|
||||||
"system being logged e.g. 'PRODUCTION'",
|
"system being logged e.g. 'PRODUCTION'",
|
||||||
func() {
|
func(*string, string) bool {
|
||||||
log.Infoln(environment + " environment logging enabled")
|
log.Infoln(environment + " environment logging enabled")
|
||||||
|
return false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue