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

refactored flag/env loading

This commit is contained in:
Harvey Kandola 2017-07-19 18:47:01 +01:00
parent dc49dbbeff
commit 68130122e7
23 changed files with 1128 additions and 909 deletions

View file

@ -37,10 +37,10 @@ for arch in amd64 ; do
for os in darwin linux windows ; do
if [ "$os" == "windows" ] ; then
echo "Compiling documize-community-$os-$arch.exe"
env GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o bin/documize-community-$os-$arch.exe ./cmd/community
env GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o bin/documize-community-$os-$arch.exe ./edition/community.go
else
echo "Compiling documize-community-$os-$arch"
env GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o bin/documize-community-$os-$arch ./cmd/community
env GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o bin/documize-community-$os-$arch ./edition/community.go
fi
done
done

View file

@ -1,31 +0,0 @@
// 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
// This package provides Documize as a single executable.
package main
import (
"github.com/documize/community/core/api/endpoint"
"github.com/documize/community/core/env"
"github.com/documize/community/core/section"
_ "github.com/documize/community/embed" // the compressed front-end code and static data
_ "github.com/go-sql-driver/mysql" // the mysql driver is required behind the scenes
)
func main() {
env.Parse("db") // process the db value first
ready := make(chan struct{}, 1) // channel is used for testing
section.Register()
endpoint.Serve(ready)
}

View file

@ -19,6 +19,7 @@ import (
"net/http"
"strings"
"github.com/documize/community/core/api"
"github.com/documize/community/core/api/endpoint/models"
"github.com/documize/community/core/api/entity"
"github.com/documize/community/core/api/request"
@ -348,7 +349,7 @@ func preAuthorizeStaticAssets(r *http.Request) bool {
strings.ToLower(r.URL.Path) == "/robots.txt" ||
strings.ToLower(r.URL.Path) == "/version" ||
strings.HasPrefix(strings.ToLower(r.URL.Path), "/api/public/") ||
((web.SiteMode == web.SiteModeSetup) && (strings.ToLower(r.URL.Path) == "/api/setup")) {
((api.Runtime.Flags.SiteMode == web.SiteModeSetup) && (strings.ToLower(r.URL.Path) == "/api/setup")) {
return true
}

View file

@ -15,6 +15,7 @@ import (
"fmt"
"net/http"
"github.com/documize/community/core/api"
"github.com/documize/community/core/api/store"
"github.com/documize/community/core/log"
)
@ -43,7 +44,7 @@ func writeTransactionError(w http.ResponseWriter, method string, err error) {
// IsInvalidLicense returns true if license is invalid
func IsInvalidLicense() bool {
return Product.License.Valid == false
return api.Runtime.Product.License.Valid == false
}
/*

View file

@ -12,46 +12,44 @@
package endpoint
import (
"crypto/rand"
"fmt"
"net/http"
"strings"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/documize/community/core/api"
"github.com/documize/community/core/api/request"
"github.com/documize/community/core/env"
"github.com/documize/community/core/log"
)
var jwtKey string
// var jwtKey string
func init() {
env.GetString(&jwtKey, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated",
func(t *string, n string) bool {
if jwtKey == "" {
b := make([]byte, 17)
_, err := rand.Read(b)
if err != nil {
jwtKey = err.Error()
log.Error("problem using crypto/rand", err)
return false
}
for k, v := range b {
if (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= '0' && v <= '0') {
b[k] = v
} else {
s := fmt.Sprintf("%x", v)
b[k] = s[0]
}
}
jwtKey = string(b)
log.Info("Please set DOCUMIZESALT or use -salt with this value: " + jwtKey)
}
return true
})
}
// func init() {
// env.GetString(&jwtKey, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated",
// func(t *string, n string) bool {
// if jwtKey == "" {
// b := make([]byte, 17)
// _, err := rand.Read(b)
// if err != nil {
// jwtKey = err.Error()
// log.Error("problem using crypto/rand", err)
// return false
// }
// for k, v := range b {
// if (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= '0' && v <= '0') {
// b[k] = v
// } else {
// s := fmt.Sprintf("%x", v)
// b[k] = s[0]
// }
// }
// jwtKey = string(b)
// log.Info("Please set DOCUMIZESALT or use -salt with this value: " + jwtKey)
// }
// return true
// })
// }
// Generates JSON Web Token (http://jwt.io)
func generateJWT(user, org, domain string) string {
@ -64,7 +62,7 @@ func generateJWT(user, org, domain string) string {
"domain": domain,
})
tokenString, _ := token.SignedString([]byte(jwtKey))
tokenString, _ := token.SignedString([]byte(api.Runtime.Flags.Salt))
return tokenString
}
@ -103,7 +101,7 @@ func decodeJWT(tokenString string) (c request.Context, claims jwt.Claims, err er
c.Guest = false
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte(jwtKey), nil
return []byte(api.Runtime.Flags.Salt), nil
})
if err != nil {

View file

@ -18,6 +18,7 @@ import (
"net/http"
"text/template"
"github.com/documize/community/core/api"
"github.com/documize/community/core/api/entity"
"github.com/documize/community/core/api/request"
"github.com/documize/community/core/log"
@ -45,9 +46,9 @@ func GetMeta(w http.ResponseWriter, r *http.Request) {
data.AllowAnonymousAccess = org.AllowAnonymousAccess
data.AuthProvider = org.AuthProvider
data.AuthConfig = org.AuthConfig
data.Version = Product.Version
data.Edition = Product.License.Edition
data.Valid = Product.License.Valid
data.Version = api.Runtime.Product.Version
data.Edition = api.Runtime.Product.License.Edition
data.Valid = api.Runtime.Product.License.Valid
data.ConversionEndpoint = org.ConversionEndpoint
// Strip secrets

View file

@ -18,40 +18,37 @@ import (
"strings"
"github.com/codegangsta/negroni"
"github.com/documize/community/core"
"github.com/documize/community/core/api"
"github.com/documize/community/core/api/plugins"
"github.com/documize/community/core/database"
"github.com/documize/community/core/env"
"github.com/documize/community/core/log"
"github.com/documize/community/core/web"
"github.com/gorilla/mux"
)
var port, certFile, keyFile, forcePort2SSL string
// var port, certFile, keyFile, forcePort2SSL string
// Product details app edition and version
var Product core.ProdInfo
// var Product env.ProdInfo
func init() {
Product.Major = "1"
Product.Minor = "49"
Product.Patch = "2"
Product.Version = fmt.Sprintf("%s.%s.%s", Product.Major, Product.Minor, Product.Patch)
Product.Edition = "Community"
Product.Title = fmt.Sprintf("%s Edition", Product.Edition)
Product.License = core.License{}
Product.License.Seats = 1
Product.License.Valid = true
Product.License.Trial = false
Product.License.Edition = "Community"
// func init() {
// // Product.Major = "1"
// // Product.Minor = "50"
// // Product.Patch = "0"
// // Product.Version = fmt.Sprintf("%s.%s.%s", Product.Major, Product.Minor, Product.Patch)
// // Product.Edition = "Community"
// // Product.Title = fmt.Sprintf("%s Edition", Product.Edition)
// // Product.License = env.License{}
// // Product.License.Seats = 1
// // Product.License.Valid = true
// // Product.License.Trial = false
// // Product.License.Edition = "Community"
env.GetString(&certFile, "cert", false, "the cert.pem file used for https", nil)
env.GetString(&keyFile, "key", false, "the key.pem file used for https", nil)
env.GetString(&port, "port", false, "http/https port number", nil)
env.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", nil)
log.Info("Server.Init complete")
}
// // env.GetString(&certFile, "cert", false, "the cert.pem file used for https", nil)
// // env.GetString(&keyFile, "key", false, "the key.pem file used for https", nil)
// // env.GetString(&port, "port", false, "http/https port number", nil)
// // env.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", nil)
// }
var testHost string // used during automated testing
@ -64,9 +61,9 @@ func Serve(ready chan struct{}) {
os.Exit(1)
}
log.Info(fmt.Sprintf("Starting %s version %s", Product.Title, Product.Version))
log.Info(fmt.Sprintf("Starting %s version %s", api.Runtime.Product.Title, api.Runtime.Product.Version))
switch web.SiteMode {
switch api.Runtime.Flags.SiteMode {
case web.SiteModeOffline:
log.Info("Serving OFFLINE web app")
case web.SiteModeSetup:
@ -105,43 +102,34 @@ func Serve(ready chan struct{}) {
n.UseHandler(router)
ready <- struct{}{}
if certFile == "" && keyFile == "" {
if port == "" {
port = "80"
}
log.Info("Starting non-SSL server on " + port)
n.Run(testHost + ":" + port)
if !api.Runtime.Flags.SSLEnabled() {
log.Info("Starting non-SSL server on " + api.Runtime.Flags.HTTPPort)
n.Run(testHost + ":" + api.Runtime.Flags.HTTPPort)
} else {
if port == "" {
port = "443"
}
if forcePort2SSL != "" {
log.Info("Starting non-SSL server on " + forcePort2SSL + " and redirecting to SSL server on " + port)
if api.Runtime.Flags.ForceHTTPPort2SSL != "" {
log.Info("Starting non-SSL server on " + api.Runtime.Flags.ForceHTTPPort2SSL + " and redirecting to SSL server on " + api.Runtime.Flags.HTTPPort)
go func() {
err := http.ListenAndServe(":"+forcePort2SSL, http.HandlerFunc(
err := http.ListenAndServe(":"+api.Runtime.Flags.ForceHTTPPort2SSL, http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Connection", "close")
var host = strings.Replace(req.Host, forcePort2SSL, port, 1) + req.RequestURI
var host = strings.Replace(req.Host, api.Runtime.Flags.ForceHTTPPort2SSL, api.Runtime.Flags.HTTPPort, 1) + req.RequestURI
http.Redirect(w, req, "https://"+host, http.StatusMovedPermanently)
}))
if err != nil {
log.Error("ListenAndServe on "+forcePort2SSL, err)
log.Error("ListenAndServe on "+api.Runtime.Flags.ForceHTTPPort2SSL, err)
}
}()
}
log.Info("Starting SSL server on " + port + " with " + certFile + " " + keyFile)
log.Info("Starting SSL server on " + api.Runtime.Flags.HTTPPort + " with " + api.Runtime.Flags.SSLCertFile + " " + api.Runtime.Flags.SSLKeyFile)
// TODO: https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/
server := &http.Server{Addr: ":" + port, Handler: n /*, TLSConfig: myTLSConfig*/}
server := &http.Server{Addr: ":" + api.Runtime.Flags.HTTPPort, Handler: n /*, TLSConfig: myTLSConfig*/}
server.SetKeepAlivesEnabled(true)
if err := server.ListenAndServeTLS(certFile, keyFile); err != nil {
log.Error("ListenAndServeTLS on "+port, err)
if err := server.ListenAndServeTLS(api.Runtime.Flags.SSLCertFile, api.Runtime.Flags.SSLKeyFile); err != nil {
log.Error("ListenAndServeTLS on "+api.Runtime.Flags.HTTPPort, err)
}
}
}
@ -153,7 +141,7 @@ func cors(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Set("Access-Control-Expose-Headers", "x-documize-version, x-documize-status")
if r.Method == "OPTIONS" {
w.Header().Add("X-Documize-Version", Product.Version)
w.Header().Add("X-Documize-Version", api.Runtime.Product.Version)
w.Header().Add("Cache-Control", "no-cache")
if _, err := w.Write([]byte("")); err != nil {
@ -166,7 +154,7 @@ func cors(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
}
func metrics(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Add("X-Documize-Version", Product.Version)
w.Header().Add("X-Documize-Version", api.Runtime.Product.Version)
w.Header().Add("Cache-Control", "no-cache")
// Prevent page from being displayed in an iframe
w.Header().Add("X-Frame-Options", "DENY")
@ -180,7 +168,7 @@ func metrics(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
}
func version(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte(Product.Version)); err != nil {
if _, err := w.Write([]byte(api.Runtime.Product.Version)); err != nil {
log.Error("versionHandler", err)
}
}

View file

@ -23,7 +23,6 @@ import (
"github.com/documize/community/core/api/convert/md"
"github.com/documize/community/core/api/request"
api "github.com/documize/community/core/convapi"
"github.com/documize/community/core/env"
"github.com/documize/community/core/log"
"github.com/documize/glick"
)
@ -32,12 +31,12 @@ import (
var PluginFile = "DB" // this points to the database
var insecure = "false"
func init() {
env.GetString(&PluginFile, "plugin", false,
"the JSON file describing plugins, default 'DB' uses the database config table 'FILEPLUGINS' entry", nil)
env.GetString(&insecure, "insecure", false,
"if 'true' allow https endpoints with invalid certificates (only for testing)", nil)
}
// func init() {
// env.GetString(&PluginFile, "plugin", false,
// "the JSON file describing plugins, default 'DB' uses the database config table 'FILEPLUGINS' entry", nil)
// env.GetString(&insecure, "insecure", false,
// "if 'true' allow https endpoints with invalid certificates (only for testing)", nil)
// }
type infoLog struct{}

8
core/api/repair.go Normal file
View file

@ -0,0 +1,8 @@
package api
import (
"github.com/documize/community/core/env"
)
// Runtime is code repair in progress
var Runtime env.Runtime

View file

@ -13,17 +13,16 @@ package request
import (
"fmt"
"os"
"strings"
"time"
// "os"
// "strings"
// "time"
"github.com/jmoiron/sqlx"
"github.com/documize/community/core/database"
"github.com/documize/community/core/env"
// "github.com/documize/community/core/database"
// "github.com/documize/community/core/env"
"github.com/documize/community/core/log"
"github.com/documize/community/core/streamutil"
"github.com/documize/community/core/web"
// "github.com/documize/community/core/web"
)
var connectionString string
@ -46,84 +45,84 @@ func (dr *databaseRequest) MakeTx() (err error) {
return err
}
func init() {
var err error
// func init() {
// var err error
env.GetString(&connectionString, "db", true,
`'username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`,
func(*string, string) bool {
// env.GetString(&connectionString, "db", true,
// `'username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`,
// func(*string, string) bool {
Db, err = sqlx.Open("mysql", stdConn(connectionString))
// Db, err = sqlx.Open("mysql", stdConn(connectionString))
if err != nil {
log.Error("Unable to setup database", err)
}
// if err != nil {
// log.Error("Unable to setup database", err)
// }
Db.SetMaxIdleConns(30)
Db.SetMaxOpenConns(100)
Db.SetConnMaxLifetime(time.Second * 14400)
// Db.SetMaxIdleConns(30)
// Db.SetMaxOpenConns(100)
// Db.SetConnMaxLifetime(time.Second * 14400)
err = Db.Ping()
// err = Db.Ping()
if err != nil {
log.Error("Unable to connect to database, connection string should be of the form: '"+
"username:password@tcp(host:3306)/database'", err)
os.Exit(2)
}
// if err != nil {
// log.Error("Unable to connect to database, connection string should be of the form: '"+
// "username:password@tcp(host:3306)/database'", err)
// os.Exit(2)
// }
// go into setup mode if required
if web.SiteMode != web.SiteModeOffline {
if database.Check(Db, connectionString) {
if err := database.Migrate(true /* the config table exists */); err != nil {
log.Error("Unable to run database migration: ", err)
os.Exit(2)
}
} else {
log.Info("database.Check(Db) !OK, going into setup mode")
}
}
// // go into setup mode if required
// if web.SiteMode != web.SiteModeOffline {
// if database.Check(Db, connectionString) {
// if err := database.Migrate(true /* the config table exists */); err != nil {
// log.Error("Unable to run database migration: ", err)
// os.Exit(2)
// }
// } else {
// log.Info("database.Check(Db) !OK, going into setup mode")
// }
// }
return false // value not changed
})
}
// return false // value not changed
// })
// }
var stdParams = map[string]string{
"charset": "utf8",
"parseTime": "True",
"maxAllowedPacket": "4194304", // 4194304 // 16777216 = 16MB
}
// var stdParams = map[string]string{
// "charset": "utf8",
// "parseTime": "True",
// "maxAllowedPacket": "4194304", // 4194304 // 16777216 = 16MB
// }
func stdConn(cs string) string {
queryBits := strings.Split(cs, "?")
ret := queryBits[0] + "?"
retFirst := true
if len(queryBits) == 2 {
paramBits := strings.Split(queryBits[1], "&")
for _, pb := range paramBits {
found := false
if assignBits := strings.Split(pb, "="); len(assignBits) == 2 {
_, found = stdParams[strings.TrimSpace(assignBits[0])]
}
if !found { // if we can't work out what it is, put it through
if retFirst {
retFirst = false
} else {
ret += "&"
}
ret += pb
}
}
}
for k, v := range stdParams {
if retFirst {
retFirst = false
} else {
ret += "&"
}
ret += k + "=" + v
}
return ret
}
// func stdConn(cs string) string {
// queryBits := strings.Split(cs, "?")
// ret := queryBits[0] + "?"
// retFirst := true
// if len(queryBits) == 2 {
// paramBits := strings.Split(queryBits[1], "&")
// for _, pb := range paramBits {
// found := false
// if assignBits := strings.Split(pb, "="); len(assignBits) == 2 {
// _, found = stdParams[strings.TrimSpace(assignBits[0])]
// }
// if !found { // if we can't work out what it is, put it through
// if retFirst {
// retFirst = false
// } else {
// ret += "&"
// }
// ret += pb
// }
// }
// }
// for k, v := range stdParams {
// if retFirst {
// retFirst = false
// } else {
// ret += "&"
// }
// ret += k + "=" + v
// }
// return ret
// }
type baseManager struct {
}

View file

@ -17,6 +17,7 @@ import (
"strings"
"time"
"github.com/documize/community/core/api"
"github.com/documize/community/core/api/entity"
"github.com/documize/community/core/log"
"github.com/documize/community/core/streamutil"
@ -82,7 +83,7 @@ func (p *Persister) GetOrganizationByDomain(subdomain string) (org entity.Organi
err = nil
subdomain = strings.ToLower(subdomain)
if web.SiteMode == web.SiteModeNormal { // only return an organization when running normally
if api.Runtime.Flags.SiteMode == web.SiteModeNormal { // only return an organization when running normally
var stmt *sqlx.Stmt

View file

@ -17,6 +17,7 @@ import (
"strconv"
"strings"
"github.com/documize/community/core/env"
"github.com/documize/community/core/log"
"github.com/documize/community/core/streamutil"
"github.com/documize/community/core/web"
@ -31,25 +32,25 @@ const sqlVariantMariaDB string = "MariaDB"
var dbCheckOK bool // default false
// dbPtr is a pointer to the central connection to the database, used by all database requests.
var dbPtr **sqlx.DB
var dbPtr *sqlx.DB
// Check that the database is configured correctly and that all the required tables exist.
// It must be the first function called in this package.
func Check(Db *sqlx.DB, connectionString string) bool {
dbPtr = &Db
func Check(runtime env.Runtime) bool {
dbPtr = runtime.Db
log.Info("Database checks: started")
csBits := strings.Split(connectionString, "/")
csBits := strings.Split(runtime.Flags.DBConn, "/")
if len(csBits) > 1 {
web.SiteInfo.DBname = strings.Split(csBits[len(csBits)-1], "?")[0]
}
rows, err := Db.Query("SELECT VERSION() AS version, @@version_comment as comment, @@character_set_database AS charset, @@collation_database AS collation;")
rows, err := runtime.Db.Query("SELECT VERSION() AS version, @@version_comment as comment, @@character_set_database AS charset, @@collation_database AS collation;")
if err != nil {
log.Error("Can't get MySQL configuration", err)
web.SiteInfo.Issue = "Can't get MySQL configuration: " + err.Error()
web.SiteMode = web.SiteModeBadDB
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
defer streamutil.Close(rows)
@ -65,7 +66,7 @@ func Check(Db *sqlx.DB, connectionString string) bool {
if err != nil {
log.Error("no MySQL configuration returned", err)
web.SiteInfo.Issue = "no MySQL configuration return issue: " + err.Error()
web.SiteMode = web.SiteModeBadDB
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
@ -92,7 +93,7 @@ func Check(Db *sqlx.DB, connectionString string) bool {
want := fmt.Sprintf("%d.%d.%d", verInts[0], verInts[1], verInts[2])
log.Error("MySQL version element "+strconv.Itoa(k+1)+" of '"+version+"' not high enough, need at least version "+want, errors.New("bad MySQL version"))
web.SiteInfo.Issue = "MySQL version element " + strconv.Itoa(k+1) + " of '" + version + "' not high enough, need at least version " + want
web.SiteMode = web.SiteModeBadDB
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
}
@ -101,30 +102,30 @@ func Check(Db *sqlx.DB, connectionString string) bool {
if charset != "utf8" {
log.Error("MySQL character set not utf8:", errors.New(charset))
web.SiteInfo.Issue = "MySQL character set not utf8: " + charset
web.SiteMode = web.SiteModeBadDB
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
if !strings.HasPrefix(collation, "utf8") {
log.Error("MySQL collation sequence not utf8...:", errors.New(collation))
web.SiteInfo.Issue = "MySQL collation sequence not utf8...: " + collation
web.SiteMode = web.SiteModeBadDB
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
}
{ // if there are no rows in the database, enter set-up mode
var flds []string
if err := Db.Select(&flds,
if err := runtime.Db.Select(&flds,
`SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '`+web.SiteInfo.DBname+
`' and TABLE_TYPE='BASE TABLE'`); err != nil {
log.Error("Can't get MySQL number of tables", err)
web.SiteInfo.Issue = "Can't get MySQL number of tables: " + err.Error()
web.SiteMode = web.SiteModeBadDB
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
if strings.TrimSpace(flds[0]) == "0" {
log.Info("Entering database set-up mode because the database is empty.....")
web.SiteMode = web.SiteModeSetup
runtime.Flags.SiteMode = web.SiteModeSetup
return false
}
}
@ -137,16 +138,16 @@ func Check(Db *sqlx.DB, connectionString string) bool {
for _, table := range tables {
var dummy []string
if err := Db.Select(&dummy, "SELECT 1 FROM "+table+" LIMIT 1;"); err != nil {
if err := runtime.Db.Select(&dummy, "SELECT 1 FROM "+table+" LIMIT 1;"); err != nil {
log.Error("Entering bad database mode because: SELECT 1 FROM "+table+" LIMIT 1;", err)
web.SiteInfo.Issue = "MySQL database is not empty, but does not contain table: " + table
web.SiteMode = web.SiteModeBadDB
runtime.Flags.SiteMode = web.SiteModeBadDB
return false
}
}
}
web.SiteMode = web.SiteModeNormal // actually no need to do this (as already ""), this for documentation
runtime.Flags.SiteMode = web.SiteModeNormal // actually no need to do this (as already ""), this for documentation
web.SiteInfo.DBname = "" // do not give this info when not in set-up mode
dbCheckOK = true
return true

View file

@ -18,6 +18,7 @@ import (
"strings"
"time"
"github.com/documize/community/core/api"
"github.com/documize/community/core/log"
"github.com/documize/community/core/secrets"
"github.com/documize/community/core/stringutil"
@ -65,7 +66,7 @@ func Create(w http.ResponseWriter, r *http.Request) {
target := "/setup"
status := http.StatusBadRequest
if web.SiteMode == web.SiteModeNormal {
if api.Runtime.Flags.SiteMode == web.SiteModeNormal {
target = "/"
status = http.StatusOK
}
@ -133,7 +134,7 @@ func Create(w http.ResponseWriter, r *http.Request) {
return
}
web.SiteMode = web.SiteModeNormal
api.Runtime.Flags.SiteMode = web.SiteModeNormal
}
// The result of completing the onboarding process.

126
core/env/flags.go vendored
View file

@ -9,8 +9,7 @@
//
// https://documize.com
// Package env allow environment variables to be obtained from either the environment or the command line.
// Environment variables are always uppercase, with the Prefix; flags are always lowercase without.
// Package env provides runtime, server level setup and configuration
package env
import (
@ -22,102 +21,125 @@ import (
"sync"
)
// CallbackT is the type signature of the callback function of GetString().
type CallbackT func(*string, string) bool
// 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
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
}
type varT struct {
// SSLEnabled returns true if both cert and key were provided at runtime.
func (f *Flags) SSLEnabled() bool {
return f.SSLCertFile != "" && f.SSLKeyFile != ""
}
type flagItem struct {
target *string
name, setter, value string
required bool
callback CallbackT
}
type varsT struct {
vv []varT
type progFlags struct {
items []flagItem
}
var vars varsT
var varsMutex sync.Mutex
// Len is part of sort.Interface.
func (v *varsT) Len() int {
return len(v.vv)
func (v *progFlags) Len() int {
return len(v.items)
}
// Swap is part of sort.Interface.
func (v *varsT) Swap(i, j int) {
v.vv[i], v.vv[j] = v.vv[j], v.vv[i]
func (v *progFlags) Swap(i, j int) {
v.items[i], v.items[j] = v.items[j], v.items[i]
}
// Less is part of sort.Interface.
func (v *varsT) Less(i, j int) bool {
return v.vv[i].name < v.vv[j].name
func (v *progFlags) Less(i, j int) bool {
return v.items[i].name < v.items[j].name
}
// Prefix provides the prefix for all Environment variables
const Prefix = "DOCUMIZE"
// prefix provides the prefix for all environment variables
const prefix = "DOCUMIZE"
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 CallbackT) {
varsMutex.Lock()
defer varsMutex.Unlock()
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) {
var dbConn, jwtKey, siteMode, port, certFile, keyFile, forcePort2SSL string
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(&dbConn, "db", true, `'username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`)
parse("db")
f.DBConn = dbConn
f.ForceHTTPPort2SSL = forcePort2SSL
f.HTTPPort = port
f.Salt = jwtKey
f.SiteMode = siteMode
f.SSLCertFile = certFile
f.SSLKeyFile = keyFile
return f
}
// register prepares flag for subsequent parsing
func register(target *string, name string, required bool, usage string) {
loadMutex.Lock()
defer loadMutex.Unlock()
name = strings.ToLower(strings.TrimSpace(name))
setter := Prefix + strings.ToUpper(name)
setter := prefix + strings.ToUpper(name)
value := os.Getenv(setter)
if value == "" {
value = *target // use the Go initialized value
setter = goInit
}
flag.StringVar(target, name, value, usage)
vars.vv = append(vars.vv, varT{target: target, name: name, required: required, callback: callback, value: value, setter: setter})
flagList.items = append(flagList.items, flagItem{target: target, name: name, required: required, value: value, setter: setter})
}
var showSettings = flag.Bool("showsettings", false, "if true, show settings in the log (WARNING: these settings may include passwords)")
// parse loads flags from OS environment and command line switches
func parse(doFirst string) {
loadMutex.Lock()
defer loadMutex.Unlock()
// 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(doFirst string) {
varsMutex.Lock()
defer varsMutex.Unlock()
flag.Parse()
sort.Sort(&vars)
sort.Sort(&flagList)
for pass := 1; pass <= 2; pass++ {
for vi, v := range vars.vv {
for vi, v := range flagList.items {
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
}
flagList.items[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 {
for _, vv := range flagList.items {
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)
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 *showSettings {
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))
}
}
}
}

6
core/env/logger.go vendored
View file

@ -12,11 +12,13 @@
// Package env provides runtime, server level setup and configuration
package env
import "github.com/jmoiron/sqlx"
import (
"github.com/jmoiron/sqlx"
)
// Logger provides the interface for Documize compatible loggers.
type Logger interface {
Info(message string)
Error(message string, err error)
SetDB(db *sqlx.DB)
SetDB(l Logger, db *sqlx.DB) Logger
}

View file

@ -9,7 +9,7 @@
//
// https://documize.com
package core
package env
import (
"fmt"

2
core/env/runtime.go vendored
View file

@ -17,6 +17,8 @@ import "github.com/jmoiron/sqlx"
// Runtime provides access to database, logger and other server-level scoped objects.
// Use Context for per-request values.
type Runtime struct {
Flags Flags
Db *sqlx.DB
Log Logger
Product ProdInfo
}

View file

@ -19,23 +19,14 @@ import (
"runtime"
log "github.com/Sirupsen/logrus"
"github.com/documize/community/core/env"
)
var environment = "Non-production"
func init() {
log.SetFormatter(new(log.TextFormatter))
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
env.GetString(&environment, "log", false,
"system being logged e.g. 'PRODUCTION'",
func(*string, string) bool {
log.Infoln(environment + " environment logging enabled")
return false
})
}
// Debug logs a message for debug purposes.

View file

@ -16,12 +16,12 @@ import (
"html/template"
"net/http"
"github.com/documize/community/core/env"
"github.com/documize/community/core/api"
"github.com/documize/community/core/secrets"
)
// SiteMode defines that the web server should show the system to be in a particular state.
var SiteMode string
// var SiteMode string
const (
// SiteModeNormal serves app
@ -40,7 +40,7 @@ var SiteInfo struct {
}
func init() {
env.GetString(&SiteMode, "offline", false, "set to '1' for OFFLINE mode", nil) // no sense overriding this setting from the DB
// env.GetString(&SiteMode, "offline", false, "set to '1' for OFFLINE mode", nil) // no sense overriding this setting from the DB
SiteInfo.DBhash = secrets.GenerateRandomPassword() // do this only once
}
@ -57,7 +57,7 @@ var Embed EmbedHandler
// EmberHandler provides the webserver for pages developed using the Ember programming environment.
func EmberHandler(w http.ResponseWriter, r *http.Request) {
filename := "index.html"
switch SiteMode {
switch api.Runtime.Flags.SiteMode {
case SiteModeOffline:
filename = "offline.html"
case SiteModeSetup:

178
edition/community.go Normal file
View file

@ -0,0 +1,178 @@
// 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
// This package provides Documize as a single executable.
package main
import (
"crypto/rand"
"fmt"
"strings"
"time"
"github.com/documize/community/core/api"
"github.com/documize/community/core/api/endpoint"
"github.com/documize/community/core/api/request"
"github.com/documize/community/core/database"
"github.com/documize/community/core/env"
"github.com/documize/community/core/section"
"github.com/documize/community/core/web"
"github.com/documize/community/edition/logging"
_ "github.com/documize/community/embed" // the compressed front-end code and static data
_ "github.com/go-sql-driver/mysql" // the mysql driver is required behind the scenes
"github.com/jmoiron/sqlx"
)
func init() {
// runtime stores server/application level information
runtime := env.Runtime{}
// wire up logging implementation
runtime.Log = logging.NewLogger()
// define product edition details
runtime.Product = env.ProdInfo{}
runtime.Product.Major = "1"
runtime.Product.Minor = "50"
runtime.Product.Patch = "0"
runtime.Product.Version = fmt.Sprintf("%s.%s.%s", runtime.Product.Major, runtime.Product.Minor, runtime.Product.Patch)
runtime.Product.Edition = "Community"
runtime.Product.Title = fmt.Sprintf("%s Edition", runtime.Product.Edition)
runtime.Product.License = env.License{}
runtime.Product.License.Seats = 1
runtime.Product.License.Valid = true
runtime.Product.License.Trial = false
runtime.Product.License.Edition = "Community"
runtime.Flags = env.ParseFlags()
flagPrep(&runtime)
// temp code repair
api.Runtime = runtime
request.Db = runtime.Db
}
func main() {
// process the db value first
// env.Parse("db")
section.Register()
ready := make(chan struct{}, 1) // channel is used for testing
endpoint.Serve(ready)
}
func flagPrep(r *env.Runtime) bool {
// Prepare DB
db, err := sqlx.Open("mysql", stdConn(r.Flags.DBConn))
if err != nil {
r.Log.Error("unable to setup database", err)
}
r.Db = db
r.Db.SetMaxIdleConns(30)
r.Db.SetMaxOpenConns(100)
r.Db.SetConnMaxLifetime(time.Second * 14400)
err = r.Db.Ping()
if err != nil {
r.Log.Error("unable to connect to database, connection string should be of the form: '"+
"username:password@tcp(host:3306)/database'", err)
return false
}
// go into setup mode if required
if r.Flags.SiteMode != web.SiteModeOffline {
if database.Check(*r) {
if err := database.Migrate(true /* the config table exists */); err != nil {
r.Log.Error("unable to run database migration", err)
return false
}
} else {
r.Log.Info("going into setup mode to prepare new database")
}
}
// Prepare SALT
if r.Flags.Salt == "" {
b := make([]byte, 17)
_, err := rand.Read(b)
if err != nil {
r.Log.Error("problem using crypto/rand", err)
return false
}
for k, v := range b {
if (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= '0' && v <= '0') {
b[k] = v
} else {
s := fmt.Sprintf("%x", v)
b[k] = s[0]
}
}
r.Flags.Salt = string(b)
r.Log.Info("please set DOCUMIZESALT or use -salt with this value: " + r.Flags.Salt)
}
// Prepare HTTP ports
if r.Flags.SSLCertFile == "" && r.Flags.SSLKeyFile == "" {
if r.Flags.HTTPPort == "" {
r.Flags.HTTPPort = "80"
}
} else {
if r.Flags.HTTPPort == "" {
r.Flags.HTTPPort = "443"
}
}
return true
}
var stdParams = map[string]string{
"charset": "utf8",
"parseTime": "True",
"maxAllowedPacket": "4194304", // 4194304 // 16777216 = 16MB
}
func stdConn(cs string) string {
queryBits := strings.Split(cs, "?")
ret := queryBits[0] + "?"
retFirst := true
if len(queryBits) == 2 {
paramBits := strings.Split(queryBits[1], "&")
for _, pb := range paramBits {
found := false
if assignBits := strings.Split(pb, "="); len(assignBits) == 2 {
_, found = stdParams[strings.TrimSpace(assignBits[0])]
}
if !found { // if we can't work out what it is, put it through
if retFirst {
retFirst = false
} else {
ret += "&"
}
ret += pb
}
}
}
for k, v := range stdParams {
if retFirst {
retFirst = false
} else {
ret += "&"
}
ret += k + "=" + v
}
return ret
}

57
edition/logging/logger.go Normal file
View file

@ -0,0 +1,57 @@
// 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 logging defines application-wide logging implementation.
package logging
import (
"log"
"os"
"github.com/documize/community/core/env"
"github.com/jmoiron/sqlx"
)
// Logger is how we log.
type Logger struct {
db *sqlx.DB
log *log.Logger
}
// Info logs message.
func (l Logger) Info(message string) {
l.log.Println(message)
}
// Error logs error with message.
func (l Logger) Error(message string, err error) {
l.log.Println(message)
l.log.Println(err)
}
// SetDB associates database connection with given logger.
// Logger will also record messages to database given valid database connection.
func (l Logger) SetDB(logger env.Logger, db *sqlx.DB) env.Logger {
l.db = db
return logger
}
// NewLogger returns initialized logging instance.
func NewLogger() env.Logger {
l := log.New(os.Stdout, "", 0)
l.SetOutput(os.Stdout)
// log.SetOutput(os.Stdout)
var logger Logger
logger.log = l
return logger
}

File diff suppressed because one or more lines are too long

View file

@ -1,16 +1,16 @@
{
"community":
{
"version": "1.49.2",
"version": "1.50.0",
"major": 1,
"minor": 49,
"patch": 2
"minor": 50,
"patch": 0
},
"enterprise":
{
"version": "1.51.2",
"version": "1.52.0",
"major": 1,
"minor": 51,
"patch": 2
"minor": 52,
"patch": 0
}
}