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"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/documize/api/request"
|
||||
"github.com/documize/community/wordsmith/environment"
|
||||
)
|
||||
|
||||
|
@ -13,8 +14,8 @@ var endPoint = "https://api.documize.com"
|
|||
var token string
|
||||
|
||||
func init() {
|
||||
environment.GetString(&endPoint, "endpoint", false, "Documize end-point", nil)
|
||||
environment.GetString(&token, "token", false, "Documize token", nil)
|
||||
environment.GetString(&endPoint, "endpoint", false, "Documize end-point", request.FlagFromDB)
|
||||
environment.GetString(&token, "token", false, "Documize token", request.FlagFromDB)
|
||||
}
|
||||
|
||||
var transport = &http.Transport{
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
package convert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/documize/community/documize/api/convert/excerpt"
|
||||
"github.com/documize/community/documize/api/convert/html"
|
||||
"github.com/documize/community/documize/api/plugins"
|
||||
"github.com/documize/community/wordsmith/api"
|
||||
"github.com/documize/community/wordsmith/utility"
|
||||
"errors"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
|
|
@ -13,7 +13,7 @@ import (
|
|||
|
||||
func TestConvert(t *testing.T) {
|
||||
|
||||
plugins.PluginFile = "" // no file as html is built-in
|
||||
plugins.PluginFile = "" // no file as html is built-in
|
||||
if lerr := plugins.LibSetup(); lerr == nil {
|
||||
//t.Error("did not error on plugin.Libsetup() with no plugin.json file")
|
||||
//return
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"github.com/codegangsta/negroni"
|
||||
"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/web"
|
||||
"github.com/documize/community/wordsmith/environment"
|
||||
|
@ -25,10 +26,10 @@ const (
|
|||
var port, certFile, keyFile, forcePort2SSL string
|
||||
|
||||
func init() {
|
||||
environment.GetString(&certFile, "cert", false, "the cert.pem file used for https", nil)
|
||||
environment.GetString(&keyFile, "key", false, "the key.pem file used for https", nil)
|
||||
environment.GetString(&port, "port", false, "http/https port number", nil)
|
||||
environment.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", 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", request.FlagFromDB)
|
||||
environment.GetString(&port, "port", false, "http/https port number", request.FlagFromDB)
|
||||
environment.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", request.FlagFromDB)
|
||||
}
|
||||
|
||||
var testHost string // used during automated testing
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"html/template"
|
||||
"net/smtp"
|
||||
|
||||
"github.com/documize/community/documize/api/request"
|
||||
"github.com/documize/community/documize/web"
|
||||
"github.com/documize/community/wordsmith/environment"
|
||||
"github.com/documize/community/wordsmith/log"
|
||||
|
@ -258,11 +259,11 @@ var creds struct{ SMTPuserid, SMTPpassword, SMTPhost, SMTPport, SMTPsender strin
|
|||
func init() {
|
||||
creds.SMTPport = "587" // the default value for outgoing SMTP traffic
|
||||
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.SMTPpassword, "smtppassword", false, "SMTP password for outgoing email", nil)
|
||||
environment.GetString(&creds.SMTPhost, "smtphost", false, "SMTP host for outgoing email", nil)
|
||||
environment.GetString(&creds.SMTPport, "smtpport", false, "SMTP port for outgoing email", nil)
|
||||
environment.GetString(&creds.SMTPsender, "smtpsender", false, "SMTP sender's e-mail 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", request.FlagFromDB)
|
||||
environment.GetString(&creds.SMTPhost, "smtphost", false, "SMTP host for outgoing email", request.FlagFromDB)
|
||||
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", request.FlagFromDB)
|
||||
}
|
||||
|
||||
// Helper to return SMTP credentials
|
||||
|
|
|
@ -31,10 +31,10 @@ package mail
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/documize/community/wordsmith/log"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/documize/community/wordsmith/log"
|
||||
"io"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/documize/community/documize/api/convert/documizeapi"
|
||||
"github.com/documize/community/documize/api/convert/html"
|
||||
"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/environment"
|
||||
"github.com/documize/community/wordsmith/log"
|
||||
|
@ -22,9 +23,9 @@ var insecure = "false"
|
|||
|
||||
func init() {
|
||||
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,
|
||||
"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{}
|
||||
|
@ -98,13 +99,17 @@ func LibSetup() error {
|
|||
return err
|
||||
}
|
||||
|
||||
json, err := ioutil.ReadFile(PluginFile)
|
||||
if err != nil {
|
||||
log.Info("Plugin file '" + PluginFile + "' not found, using no plugins")
|
||||
json = []byte(" [ ] \n")
|
||||
err = nil
|
||||
var json = make([]byte, 0)
|
||||
if PluginFile == "PLUGIN" {
|
||||
json = []byte(request.ConfigString(PluginFile, ""))
|
||||
} else {
|
||||
json, err = ioutil.ReadFile(PluginFile)
|
||||
if err != nil {
|
||||
log.Info("Plugin file '" + PluginFile + "' not found, using no plugins")
|
||||
json = []byte(" [ ] \n")
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
|
||||
err = Lib.Configure(json)
|
||||
if err != nil {
|
||||
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,
|
||||
`"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))
|
||||
|
||||
if err != nil {
|
||||
|
@ -65,6 +65,7 @@ func init() {
|
|||
log.Info("database.Check(Db) !OK, going into setup mode")
|
||||
}
|
||||
|
||||
return false // value not changed
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -36,9 +36,9 @@ func (p *Persister) SetupOrganization(company, title, message, domain, email str
|
|||
Title: title, // string `json:"title"`
|
||||
Message: message, // string `json:"message"`
|
||||
//URL: "test.domain", // string `json:"url"`
|
||||
Domain: domain, // string `json:"domain"`
|
||||
Email: email, // string `json:"email"`
|
||||
AllowAnonymousAccess: false, // bool `json:"allowAnonymousAccess"`
|
||||
Domain: domain, // string `json:"domain"`
|
||||
Email: email, // string `json:"email"`
|
||||
AllowAnonymousAccess: false, // bool `json:"allowAnonymousAccess"`
|
||||
//Serial: "123", // string `json:"-"`
|
||||
Active: true, // bool `json:"-"`
|
||||
}
|
||||
|
@ -50,6 +50,6 @@ func (p *Persister) SetupOrganization(company, title, message, domain, email str
|
|||
if err != nil {
|
||||
return org, err
|
||||
}
|
||||
p.Context.Transaction, err = Db.Beginx()
|
||||
p.Context.Transaction, err = Db.Beginx()
|
||||
return org, err
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ func testAddUser(t *testing.T, p *Persister) entity.User {
|
|||
//Password: "testpassword", // string `json:"-"`
|
||||
//Salt: "testsalt", // string `json:"-"`
|
||||
//Reset: "testreset", // string `json:"-"`
|
||||
Accounts: nil, // []Account `json:"accounts"`
|
||||
Accounts: nil, // []Account `json:"accounts"`
|
||||
}
|
||||
user.Salt = generateSalt()
|
||||
requestedPassword := generateRandomPassword()
|
||||
|
|
|
@ -31,7 +31,7 @@ func TestUpload(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestConvert(t *testing.T) {
|
||||
_, _, err :=
|
||||
_, _, err :=
|
||||
lsp.Convert(api.ConversionJobRequest{})
|
||||
if err == nil {
|
||||
t.Error("there should have been a convert error")
|
||||
|
|
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() {
|
||||
environment.Parse()
|
||||
environment.Parse("db") // process the db value first
|
||||
ready := make(chan struct{}, 1) // channel is used for testing
|
||||
|
||||
endpoint.Serve(ready)
|
||||
|
|
|
@ -33,8 +33,8 @@ var SiteInfo struct {
|
|||
}
|
||||
|
||||
func init() {
|
||||
environment.GetString(&SiteMode, "offline", false, "set to '1' for OFFLINE mode", nil)
|
||||
SiteInfo.DBhash = util.GenerateRandomPassword() // do this only once
|
||||
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
|
||||
}
|
||||
|
||||
// EmberHandler provides the webserver for pages developed using the Ember programming environment.
|
||||
|
|
|
@ -10,11 +10,14 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// CallbackT is the type signature of the callback function of GetString().
|
||||
type CallbackT func(*string, string) bool
|
||||
|
||||
type varT struct {
|
||||
target *string
|
||||
name, setter, value string
|
||||
required bool
|
||||
callback func()
|
||||
callback CallbackT
|
||||
}
|
||||
|
||||
type varsT struct {
|
||||
|
@ -41,10 +44,10 @@ func (v *varsT) Less(i, j int) bool {
|
|||
// Prefix provides the prefix for all Environment variables
|
||||
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().
|
||||
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))
|
||||
setter := Prefix + strings.ToUpper(name)
|
||||
value := os.Getenv(setter)
|
||||
|
@ -59,37 +62,43 @@ 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.
|
||||
// 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().
|
||||
func Parse() {
|
||||
func Parse(doFirst string) {
|
||||
flag.Parse()
|
||||
sort.Sort(&vars)
|
||||
for vi, v := range vars.vv {
|
||||
typ := "Optional"
|
||||
if v.value != *(v.target) || (v.value != "" && *(v.target) == "") {
|
||||
vars.vv[vi].setter = "-" + v.name // v is a local copy, not the underlying data
|
||||
}
|
||||
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:")
|
||||
for _, vv := range vars.vv {
|
||||
if vv.required {
|
||||
fmt.Fprintf(os.Stderr, "* setting from environment variable '%s' or flag '-%s', current value: '%s' set by '%s'\n",
|
||||
Prefix+strings.ToUpper(vv.name), vv.name, *(vv.target), vv.setter)
|
||||
for pass := 1; pass <= 2; pass++ {
|
||||
for vi, v := range vars.vv {
|
||||
if (pass == 1 && v.name == doFirst) || (pass == 2 && v.name != doFirst) {
|
||||
typ := "Optional"
|
||||
if v.value != *(v.target) || (v.value != "" && *(v.target) == "") {
|
||||
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
|
||||
}
|
||||
}
|
||||
fmt.Fprintln(os.Stderr)
|
||||
flag.Usage()
|
||||
os.Exit(2)
|
||||
return
|
||||
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:")
|
||||
for _, vv := range vars.vv {
|
||||
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",
|
||||
Prefix+strings.ToUpper(vv.name), vv.name, vv.name, *(vv.target), vv.setter)
|
||||
}
|
||||
}
|
||||
fmt.Fprintln(os.Stderr)
|
||||
flag.Usage()
|
||||
os.Exit(2)
|
||||
return
|
||||
}
|
||||
typ = "Required"
|
||||
}
|
||||
if *(v.target) != "" && vars.vv[vi].setter != goInit {
|
||||
fmt.Fprintf(os.Stdout, "%s setting from '%s' is: '%s'\n",
|
||||
typ, vars.vv[vi].setter, *(v.target))
|
||||
}
|
||||
}
|
||||
typ = "Required"
|
||||
}
|
||||
if *(v.target) != "" && v.setter != goInit {
|
||||
fmt.Fprintf(os.Stdout, "%s setting from '%s' is: '%s'\n",
|
||||
typ, v.setter, *(v.target))
|
||||
}
|
||||
if v.callback != nil {
|
||||
v.callback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,8 +20,9 @@ func init() {
|
|||
log.SetLevel(log.DebugLevel)
|
||||
env.GetString(&environment, "log", false,
|
||||
"system being logged e.g. 'PRODUCTION'",
|
||||
func() {
|
||||
func(*string, string) bool {
|
||||
log.Infoln(environment + " environment logging enabled")
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue