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:
parent
dc49dbbeff
commit
68130122e7
23 changed files with 1128 additions and 909 deletions
4
build.sh
4
build.sh
|
@ -37,10 +37,10 @@ for arch in amd64 ; do
|
||||||
for os in darwin linux windows ; do
|
for os in darwin linux windows ; do
|
||||||
if [ "$os" == "windows" ] ; then
|
if [ "$os" == "windows" ] ; then
|
||||||
echo "Compiling documize-community-$os-$arch.exe"
|
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
|
else
|
||||||
echo "Compiling documize-community-$os-$arch"
|
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
|
fi
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/documize/community/core/api"
|
||||||
"github.com/documize/community/core/api/endpoint/models"
|
"github.com/documize/community/core/api/endpoint/models"
|
||||||
"github.com/documize/community/core/api/entity"
|
"github.com/documize/community/core/api/entity"
|
||||||
"github.com/documize/community/core/api/request"
|
"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) == "/robots.txt" ||
|
||||||
strings.ToLower(r.URL.Path) == "/version" ||
|
strings.ToLower(r.URL.Path) == "/version" ||
|
||||||
strings.HasPrefix(strings.ToLower(r.URL.Path), "/api/public/") ||
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/documize/community/core/api"
|
||||||
"github.com/documize/community/core/api/store"
|
"github.com/documize/community/core/api/store"
|
||||||
"github.com/documize/community/core/log"
|
"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
|
// IsInvalidLicense returns true if license is invalid
|
||||||
func IsInvalidLicense() bool {
|
func IsInvalidLicense() bool {
|
||||||
return Product.License.Valid == false
|
return api.Runtime.Product.License.Valid == false
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -12,46 +12,44 @@
|
||||||
package endpoint
|
package endpoint
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
jwt "github.com/dgrijalva/jwt-go"
|
jwt "github.com/dgrijalva/jwt-go"
|
||||||
|
"github.com/documize/community/core/api"
|
||||||
"github.com/documize/community/core/api/request"
|
"github.com/documize/community/core/api/request"
|
||||||
"github.com/documize/community/core/env"
|
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var jwtKey string
|
// var jwtKey string
|
||||||
|
|
||||||
func init() {
|
// func init() {
|
||||||
env.GetString(&jwtKey, "salt", false, "the salt string used to encode JWT tokens, if not set a random value will be generated",
|
// 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 {
|
// func(t *string, n string) bool {
|
||||||
if jwtKey == "" {
|
// if jwtKey == "" {
|
||||||
b := make([]byte, 17)
|
// b := make([]byte, 17)
|
||||||
_, err := rand.Read(b)
|
// _, err := rand.Read(b)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
jwtKey = err.Error()
|
// jwtKey = err.Error()
|
||||||
log.Error("problem using crypto/rand", err)
|
// log.Error("problem using crypto/rand", err)
|
||||||
return false
|
// return false
|
||||||
}
|
// }
|
||||||
for k, v := range b {
|
// for k, v := range b {
|
||||||
if (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= '0' && v <= '0') {
|
// if (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= '0' && v <= '0') {
|
||||||
b[k] = v
|
// b[k] = v
|
||||||
} else {
|
// } else {
|
||||||
s := fmt.Sprintf("%x", v)
|
// s := fmt.Sprintf("%x", v)
|
||||||
b[k] = s[0]
|
// b[k] = s[0]
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
jwtKey = string(b)
|
// jwtKey = string(b)
|
||||||
log.Info("Please set DOCUMIZESALT or use -salt with this value: " + jwtKey)
|
// log.Info("Please set DOCUMIZESALT or use -salt with this value: " + jwtKey)
|
||||||
}
|
// }
|
||||||
return true
|
// return true
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Generates JSON Web Token (http://jwt.io)
|
// Generates JSON Web Token (http://jwt.io)
|
||||||
func generateJWT(user, org, domain string) string {
|
func generateJWT(user, org, domain string) string {
|
||||||
|
@ -64,7 +62,7 @@ func generateJWT(user, org, domain string) string {
|
||||||
"domain": domain,
|
"domain": domain,
|
||||||
})
|
})
|
||||||
|
|
||||||
tokenString, _ := token.SignedString([]byte(jwtKey))
|
tokenString, _ := token.SignedString([]byte(api.Runtime.Flags.Salt))
|
||||||
|
|
||||||
return tokenString
|
return tokenString
|
||||||
}
|
}
|
||||||
|
@ -103,7 +101,7 @@ func decodeJWT(tokenString string) (c request.Context, claims jwt.Claims, err er
|
||||||
c.Guest = false
|
c.Guest = false
|
||||||
|
|
||||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
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 {
|
if err != nil {
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/documize/community/core/api"
|
||||||
"github.com/documize/community/core/api/entity"
|
"github.com/documize/community/core/api/entity"
|
||||||
"github.com/documize/community/core/api/request"
|
"github.com/documize/community/core/api/request"
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
|
@ -45,9 +46,9 @@ func GetMeta(w http.ResponseWriter, r *http.Request) {
|
||||||
data.AllowAnonymousAccess = org.AllowAnonymousAccess
|
data.AllowAnonymousAccess = org.AllowAnonymousAccess
|
||||||
data.AuthProvider = org.AuthProvider
|
data.AuthProvider = org.AuthProvider
|
||||||
data.AuthConfig = org.AuthConfig
|
data.AuthConfig = org.AuthConfig
|
||||||
data.Version = Product.Version
|
data.Version = api.Runtime.Product.Version
|
||||||
data.Edition = Product.License.Edition
|
data.Edition = api.Runtime.Product.License.Edition
|
||||||
data.Valid = Product.License.Valid
|
data.Valid = api.Runtime.Product.License.Valid
|
||||||
data.ConversionEndpoint = org.ConversionEndpoint
|
data.ConversionEndpoint = org.ConversionEndpoint
|
||||||
|
|
||||||
// Strip secrets
|
// Strip secrets
|
||||||
|
|
|
@ -18,40 +18,37 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"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/api/plugins"
|
||||||
"github.com/documize/community/core/database"
|
"github.com/documize/community/core/database"
|
||||||
"github.com/documize/community/core/env"
|
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
"github.com/documize/community/core/web"
|
"github.com/documize/community/core/web"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
var port, certFile, keyFile, forcePort2SSL string
|
// var port, certFile, keyFile, forcePort2SSL string
|
||||||
|
|
||||||
// Product details app edition and version
|
// Product details app edition and version
|
||||||
var Product core.ProdInfo
|
// var Product env.ProdInfo
|
||||||
|
|
||||||
func init() {
|
// func init() {
|
||||||
Product.Major = "1"
|
// // Product.Major = "1"
|
||||||
Product.Minor = "49"
|
// // Product.Minor = "50"
|
||||||
Product.Patch = "2"
|
// // Product.Patch = "0"
|
||||||
Product.Version = fmt.Sprintf("%s.%s.%s", Product.Major, Product.Minor, Product.Patch)
|
// // Product.Version = fmt.Sprintf("%s.%s.%s", Product.Major, Product.Minor, Product.Patch)
|
||||||
Product.Edition = "Community"
|
// // Product.Edition = "Community"
|
||||||
Product.Title = fmt.Sprintf("%s Edition", Product.Edition)
|
// // Product.Title = fmt.Sprintf("%s Edition", Product.Edition)
|
||||||
Product.License = core.License{}
|
// // Product.License = env.License{}
|
||||||
Product.License.Seats = 1
|
// // Product.License.Seats = 1
|
||||||
Product.License.Valid = true
|
// // Product.License.Valid = true
|
||||||
Product.License.Trial = false
|
// // Product.License.Trial = false
|
||||||
Product.License.Edition = "Community"
|
// // Product.License.Edition = "Community"
|
||||||
|
|
||||||
env.GetString(&certFile, "cert", false, "the cert.pem file used for https", nil)
|
// // 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(&keyFile, "key", false, "the key.pem file used for https", nil)
|
||||||
env.GetString(&port, "port", false, "http/https port number", nil)
|
// // env.GetString(&port, "port", false, "http/https port number", nil)
|
||||||
env.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", nil)
|
// // env.GetString(&forcePort2SSL, "forcesslport", false, "redirect given http port number to TLS", nil)
|
||||||
|
// }
|
||||||
log.Info("Server.Init complete")
|
|
||||||
}
|
|
||||||
|
|
||||||
var testHost string // used during automated testing
|
var testHost string // used during automated testing
|
||||||
|
|
||||||
|
@ -64,9 +61,9 @@ func Serve(ready chan struct{}) {
|
||||||
os.Exit(1)
|
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:
|
case web.SiteModeOffline:
|
||||||
log.Info("Serving OFFLINE web app")
|
log.Info("Serving OFFLINE web app")
|
||||||
case web.SiteModeSetup:
|
case web.SiteModeSetup:
|
||||||
|
@ -105,43 +102,34 @@ func Serve(ready chan struct{}) {
|
||||||
n.UseHandler(router)
|
n.UseHandler(router)
|
||||||
ready <- struct{}{}
|
ready <- struct{}{}
|
||||||
|
|
||||||
if certFile == "" && keyFile == "" {
|
if !api.Runtime.Flags.SSLEnabled() {
|
||||||
if port == "" {
|
log.Info("Starting non-SSL server on " + api.Runtime.Flags.HTTPPort)
|
||||||
port = "80"
|
n.Run(testHost + ":" + api.Runtime.Flags.HTTPPort)
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("Starting non-SSL server on " + port)
|
|
||||||
|
|
||||||
n.Run(testHost + ":" + port)
|
|
||||||
} else {
|
} else {
|
||||||
if port == "" {
|
if api.Runtime.Flags.ForceHTTPPort2SSL != "" {
|
||||||
port = "443"
|
log.Info("Starting non-SSL server on " + api.Runtime.Flags.ForceHTTPPort2SSL + " and redirecting to SSL server on " + api.Runtime.Flags.HTTPPort)
|
||||||
}
|
|
||||||
|
|
||||||
if forcePort2SSL != "" {
|
|
||||||
log.Info("Starting non-SSL server on " + forcePort2SSL + " and redirecting to SSL server on " + port)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := http.ListenAndServe(":"+forcePort2SSL, http.HandlerFunc(
|
err := http.ListenAndServe(":"+api.Runtime.Flags.ForceHTTPPort2SSL, http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, req *http.Request) {
|
func(w http.ResponseWriter, req *http.Request) {
|
||||||
w.Header().Set("Connection", "close")
|
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)
|
http.Redirect(w, req, "https://"+host, http.StatusMovedPermanently)
|
||||||
}))
|
}))
|
||||||
if err != nil {
|
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/
|
// 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)
|
server.SetKeepAlivesEnabled(true)
|
||||||
if err := server.ListenAndServeTLS(certFile, keyFile); err != nil {
|
if err := server.ListenAndServeTLS(api.Runtime.Flags.SSLCertFile, api.Runtime.Flags.SSLKeyFile); err != nil {
|
||||||
log.Error("ListenAndServeTLS on "+port, err)
|
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")
|
w.Header().Set("Access-Control-Expose-Headers", "x-documize-version, x-documize-status")
|
||||||
|
|
||||||
if r.Method == "OPTIONS" {
|
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")
|
w.Header().Add("Cache-Control", "no-cache")
|
||||||
|
|
||||||
if _, err := w.Write([]byte("")); err != nil {
|
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) {
|
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")
|
w.Header().Add("Cache-Control", "no-cache")
|
||||||
// Prevent page from being displayed in an iframe
|
// Prevent page from being displayed in an iframe
|
||||||
w.Header().Add("X-Frame-Options", "DENY")
|
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) {
|
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)
|
log.Error("versionHandler", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,6 @@ import (
|
||||||
"github.com/documize/community/core/api/convert/md"
|
"github.com/documize/community/core/api/convert/md"
|
||||||
"github.com/documize/community/core/api/request"
|
"github.com/documize/community/core/api/request"
|
||||||
api "github.com/documize/community/core/convapi"
|
api "github.com/documize/community/core/convapi"
|
||||||
"github.com/documize/community/core/env"
|
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
"github.com/documize/glick"
|
"github.com/documize/glick"
|
||||||
)
|
)
|
||||||
|
@ -32,12 +31,12 @@ import (
|
||||||
var PluginFile = "DB" // this points to the database
|
var PluginFile = "DB" // this points to the database
|
||||||
var insecure = "false"
|
var insecure = "false"
|
||||||
|
|
||||||
func init() {
|
// func init() {
|
||||||
env.GetString(&PluginFile, "plugin", false,
|
// env.GetString(&PluginFile, "plugin", false,
|
||||||
"the JSON file describing plugins, default 'DB' uses the database config table 'FILEPLUGINS' entry", nil)
|
// "the JSON file describing plugins, default 'DB' uses the database config table 'FILEPLUGINS' entry", nil)
|
||||||
env.GetString(&insecure, "insecure", false,
|
// env.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)", nil)
|
||||||
}
|
// }
|
||||||
|
|
||||||
type infoLog struct{}
|
type infoLog struct{}
|
||||||
|
|
||||||
|
|
8
core/api/repair.go
Normal file
8
core/api/repair.go
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/documize/community/core/env"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Runtime is code repair in progress
|
||||||
|
var Runtime env.Runtime
|
|
@ -13,17 +13,16 @@ package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
// "os"
|
||||||
"strings"
|
// "strings"
|
||||||
"time"
|
// "time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
// "github.com/documize/community/core/database"
|
||||||
"github.com/documize/community/core/database"
|
// "github.com/documize/community/core/env"
|
||||||
"github.com/documize/community/core/env"
|
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
"github.com/documize/community/core/streamutil"
|
"github.com/documize/community/core/streamutil"
|
||||||
"github.com/documize/community/core/web"
|
// "github.com/documize/community/core/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
var connectionString string
|
var connectionString string
|
||||||
|
@ -46,84 +45,84 @@ func (dr *databaseRequest) MakeTx() (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
// func init() {
|
||||||
var err error
|
// var err error
|
||||||
|
|
||||||
env.GetString(&connectionString, "db", true,
|
// env.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(*string, string) bool {
|
// func(*string, string) bool {
|
||||||
|
|
||||||
Db, err = sqlx.Open("mysql", stdConn(connectionString))
|
// Db, err = sqlx.Open("mysql", stdConn(connectionString))
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Error("Unable to setup database", err)
|
// log.Error("Unable to setup database", err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
Db.SetMaxIdleConns(30)
|
// Db.SetMaxIdleConns(30)
|
||||||
Db.SetMaxOpenConns(100)
|
// Db.SetMaxOpenConns(100)
|
||||||
Db.SetConnMaxLifetime(time.Second * 14400)
|
// Db.SetConnMaxLifetime(time.Second * 14400)
|
||||||
|
|
||||||
err = Db.Ping()
|
// err = Db.Ping()
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Error("Unable to connect to database, connection string should be of the form: '"+
|
// log.Error("Unable to connect to database, connection string should be of the form: '"+
|
||||||
"username:password@tcp(host:3306)/database'", err)
|
// "username:password@tcp(host:3306)/database'", err)
|
||||||
os.Exit(2)
|
// os.Exit(2)
|
||||||
}
|
// }
|
||||||
|
|
||||||
// go into setup mode if required
|
// // go into setup mode if required
|
||||||
if web.SiteMode != web.SiteModeOffline {
|
// if web.SiteMode != web.SiteModeOffline {
|
||||||
if database.Check(Db, connectionString) {
|
// if database.Check(Db, connectionString) {
|
||||||
if err := database.Migrate(true /* the config table exists */); err != nil {
|
// if err := database.Migrate(true /* the config table exists */); err != nil {
|
||||||
log.Error("Unable to run database migration: ", err)
|
// log.Error("Unable to run database migration: ", err)
|
||||||
os.Exit(2)
|
// os.Exit(2)
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
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
|
// return false // value not changed
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
var stdParams = map[string]string{
|
// var stdParams = map[string]string{
|
||||||
"charset": "utf8",
|
// "charset": "utf8",
|
||||||
"parseTime": "True",
|
// "parseTime": "True",
|
||||||
"maxAllowedPacket": "4194304", // 4194304 // 16777216 = 16MB
|
// "maxAllowedPacket": "4194304", // 4194304 // 16777216 = 16MB
|
||||||
}
|
// }
|
||||||
|
|
||||||
func stdConn(cs string) string {
|
// func stdConn(cs string) string {
|
||||||
queryBits := strings.Split(cs, "?")
|
// queryBits := strings.Split(cs, "?")
|
||||||
ret := queryBits[0] + "?"
|
// ret := queryBits[0] + "?"
|
||||||
retFirst := true
|
// retFirst := true
|
||||||
if len(queryBits) == 2 {
|
// if len(queryBits) == 2 {
|
||||||
paramBits := strings.Split(queryBits[1], "&")
|
// paramBits := strings.Split(queryBits[1], "&")
|
||||||
for _, pb := range paramBits {
|
// for _, pb := range paramBits {
|
||||||
found := false
|
// found := false
|
||||||
if assignBits := strings.Split(pb, "="); len(assignBits) == 2 {
|
// if assignBits := strings.Split(pb, "="); len(assignBits) == 2 {
|
||||||
_, found = stdParams[strings.TrimSpace(assignBits[0])]
|
// _, found = stdParams[strings.TrimSpace(assignBits[0])]
|
||||||
}
|
// }
|
||||||
if !found { // if we can't work out what it is, put it through
|
// if !found { // if we can't work out what it is, put it through
|
||||||
if retFirst {
|
// if retFirst {
|
||||||
retFirst = false
|
// retFirst = false
|
||||||
} else {
|
// } else {
|
||||||
ret += "&"
|
// ret += "&"
|
||||||
}
|
// }
|
||||||
ret += pb
|
// ret += pb
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
for k, v := range stdParams {
|
// for k, v := range stdParams {
|
||||||
if retFirst {
|
// if retFirst {
|
||||||
retFirst = false
|
// retFirst = false
|
||||||
} else {
|
// } else {
|
||||||
ret += "&"
|
// ret += "&"
|
||||||
}
|
// }
|
||||||
ret += k + "=" + v
|
// ret += k + "=" + v
|
||||||
}
|
// }
|
||||||
return ret
|
// return ret
|
||||||
}
|
// }
|
||||||
|
|
||||||
type baseManager struct {
|
type baseManager struct {
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/documize/community/core/api"
|
||||||
"github.com/documize/community/core/api/entity"
|
"github.com/documize/community/core/api/entity"
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
"github.com/documize/community/core/streamutil"
|
"github.com/documize/community/core/streamutil"
|
||||||
|
@ -82,7 +83,7 @@ func (p *Persister) GetOrganizationByDomain(subdomain string) (org entity.Organi
|
||||||
err = nil
|
err = nil
|
||||||
subdomain = strings.ToLower(subdomain)
|
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
|
var stmt *sqlx.Stmt
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/documize/community/core/env"
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
"github.com/documize/community/core/streamutil"
|
"github.com/documize/community/core/streamutil"
|
||||||
"github.com/documize/community/core/web"
|
"github.com/documize/community/core/web"
|
||||||
|
@ -31,25 +32,25 @@ const sqlVariantMariaDB string = "MariaDB"
|
||||||
var dbCheckOK bool // default false
|
var dbCheckOK bool // default false
|
||||||
|
|
||||||
// dbPtr is a pointer to the central connection to the database, used by all database requests.
|
// 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.
|
// Check that the database is configured correctly and that all the required tables exist.
|
||||||
// It must be the first function called in this package.
|
// It must be the first function called in this package.
|
||||||
func Check(Db *sqlx.DB, connectionString string) bool {
|
func Check(runtime env.Runtime) bool {
|
||||||
dbPtr = &Db
|
dbPtr = runtime.Db
|
||||||
|
|
||||||
log.Info("Database checks: started")
|
log.Info("Database checks: started")
|
||||||
|
|
||||||
csBits := strings.Split(connectionString, "/")
|
csBits := strings.Split(runtime.Flags.DBConn, "/")
|
||||||
if len(csBits) > 1 {
|
if len(csBits) > 1 {
|
||||||
web.SiteInfo.DBname = strings.Split(csBits[len(csBits)-1], "?")[0]
|
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 {
|
if err != nil {
|
||||||
log.Error("Can't get MySQL configuration", err)
|
log.Error("Can't get MySQL configuration", err)
|
||||||
web.SiteInfo.Issue = "Can't get MySQL configuration: " + err.Error()
|
web.SiteInfo.Issue = "Can't get MySQL configuration: " + err.Error()
|
||||||
web.SiteMode = web.SiteModeBadDB
|
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer streamutil.Close(rows)
|
defer streamutil.Close(rows)
|
||||||
|
@ -65,7 +66,7 @@ func Check(Db *sqlx.DB, connectionString string) bool {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("no MySQL configuration returned", err)
|
log.Error("no MySQL configuration returned", err)
|
||||||
web.SiteInfo.Issue = "no MySQL configuration return issue: " + err.Error()
|
web.SiteInfo.Issue = "no MySQL configuration return issue: " + err.Error()
|
||||||
web.SiteMode = web.SiteModeBadDB
|
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||||
return false
|
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])
|
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"))
|
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.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
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,30 +102,30 @@ func Check(Db *sqlx.DB, connectionString string) bool {
|
||||||
if charset != "utf8" {
|
if charset != "utf8" {
|
||||||
log.Error("MySQL character set not utf8:", errors.New(charset))
|
log.Error("MySQL character set not utf8:", errors.New(charset))
|
||||||
web.SiteInfo.Issue = "MySQL character set not utf8: " + charset
|
web.SiteInfo.Issue = "MySQL character set not utf8: " + charset
|
||||||
web.SiteMode = web.SiteModeBadDB
|
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(collation, "utf8") {
|
if !strings.HasPrefix(collation, "utf8") {
|
||||||
log.Error("MySQL collation sequence not utf8...:", errors.New(collation))
|
log.Error("MySQL collation sequence not utf8...:", errors.New(collation))
|
||||||
web.SiteInfo.Issue = "MySQL collation sequence not utf8...: " + collation
|
web.SiteInfo.Issue = "MySQL collation sequence not utf8...: " + collation
|
||||||
web.SiteMode = web.SiteModeBadDB
|
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{ // if there are no rows in the database, enter set-up mode
|
{ // if there are no rows in the database, enter set-up mode
|
||||||
var flds []string
|
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+
|
`SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '`+web.SiteInfo.DBname+
|
||||||
`' and TABLE_TYPE='BASE TABLE'`); err != nil {
|
`' and TABLE_TYPE='BASE TABLE'`); err != nil {
|
||||||
log.Error("Can't get MySQL number of tables", err)
|
log.Error("Can't get MySQL number of tables", err)
|
||||||
web.SiteInfo.Issue = "Can't get MySQL number of tables: " + err.Error()
|
web.SiteInfo.Issue = "Can't get MySQL number of tables: " + err.Error()
|
||||||
web.SiteMode = web.SiteModeBadDB
|
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(flds[0]) == "0" {
|
if strings.TrimSpace(flds[0]) == "0" {
|
||||||
log.Info("Entering database set-up mode because the database is empty.....")
|
log.Info("Entering database set-up mode because the database is empty.....")
|
||||||
web.SiteMode = web.SiteModeSetup
|
runtime.Flags.SiteMode = web.SiteModeSetup
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,17 +138,17 @@ func Check(Db *sqlx.DB, connectionString string) bool {
|
||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
var dummy []string
|
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)
|
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.SiteInfo.Issue = "MySQL database is not empty, but does not contain table: " + table
|
||||||
web.SiteMode = web.SiteModeBadDB
|
runtime.Flags.SiteMode = web.SiteModeBadDB
|
||||||
return false
|
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
|
web.SiteInfo.DBname = "" // do not give this info when not in set-up mode
|
||||||
dbCheckOK = true
|
dbCheckOK = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/documize/community/core/api"
|
||||||
"github.com/documize/community/core/log"
|
"github.com/documize/community/core/log"
|
||||||
"github.com/documize/community/core/secrets"
|
"github.com/documize/community/core/secrets"
|
||||||
"github.com/documize/community/core/stringutil"
|
"github.com/documize/community/core/stringutil"
|
||||||
|
@ -65,7 +66,7 @@ func Create(w http.ResponseWriter, r *http.Request) {
|
||||||
target := "/setup"
|
target := "/setup"
|
||||||
status := http.StatusBadRequest
|
status := http.StatusBadRequest
|
||||||
|
|
||||||
if web.SiteMode == web.SiteModeNormal {
|
if api.Runtime.Flags.SiteMode == web.SiteModeNormal {
|
||||||
target = "/"
|
target = "/"
|
||||||
status = http.StatusOK
|
status = http.StatusOK
|
||||||
}
|
}
|
||||||
|
@ -133,7 +134,7 @@ func Create(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
web.SiteMode = web.SiteModeNormal
|
api.Runtime.Flags.SiteMode = web.SiteModeNormal
|
||||||
}
|
}
|
||||||
|
|
||||||
// The result of completing the onboarding process.
|
// The result of completing the onboarding process.
|
||||||
|
|
126
core/env/flags.go
vendored
126
core/env/flags.go
vendored
|
@ -9,8 +9,7 @@
|
||||||
//
|
//
|
||||||
// https://documize.com
|
// https://documize.com
|
||||||
|
|
||||||
// Package env allow environment variables to be obtained from either the environment or the command line.
|
// Package env provides runtime, server level setup and configuration
|
||||||
// Environment variables are always uppercase, with the Prefix; flags are always lowercase without.
|
|
||||||
package env
|
package env
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -22,102 +21,125 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CallbackT is the type signature of the callback function of GetString().
|
// Flags provides access to environment and command line switches for this program.
|
||||||
type CallbackT func(*string, string) bool
|
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
|
target *string
|
||||||
name, setter, value string
|
name, setter, value string
|
||||||
required bool
|
required bool
|
||||||
callback CallbackT
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type varsT struct {
|
type progFlags struct {
|
||||||
vv []varT
|
items []flagItem
|
||||||
}
|
}
|
||||||
|
|
||||||
var vars varsT
|
|
||||||
var varsMutex sync.Mutex
|
|
||||||
|
|
||||||
// Len is part of sort.Interface.
|
// Len is part of sort.Interface.
|
||||||
func (v *varsT) Len() int {
|
func (v *progFlags) Len() int {
|
||||||
return len(v.vv)
|
return len(v.items)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swap is part of sort.Interface.
|
// Swap is part of sort.Interface.
|
||||||
func (v *varsT) Swap(i, j int) {
|
func (v *progFlags) Swap(i, j int) {
|
||||||
v.vv[i], v.vv[j] = v.vv[j], v.vv[i]
|
v.items[i], v.items[j] = v.items[j], v.items[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Less is part of sort.Interface.
|
// Less is part of sort.Interface.
|
||||||
func (v *varsT) Less(i, j int) bool {
|
func (v *progFlags) Less(i, j int) bool {
|
||||||
return v.vv[i].name < v.vv[j].name
|
return v.items[i].name < v.items[j].name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prefix provides the prefix for all Environment variables
|
// prefix provides the prefix for all environment variables
|
||||||
const Prefix = "DOCUMIZE"
|
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().
|
var flagList progFlags
|
||||||
func GetString(target *string, name string, required bool, usage string, callback CallbackT) {
|
var loadMutex sync.Mutex
|
||||||
varsMutex.Lock()
|
|
||||||
defer varsMutex.Unlock()
|
// 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))
|
name = strings.ToLower(strings.TrimSpace(name))
|
||||||
setter := Prefix + strings.ToUpper(name)
|
setter := prefix + strings.ToUpper(name)
|
||||||
|
|
||||||
value := os.Getenv(setter)
|
value := os.Getenv(setter)
|
||||||
if value == "" {
|
if value == "" {
|
||||||
value = *target // use the Go initialized value
|
value = *target // use the Go initialized value
|
||||||
setter = goInit
|
setter = goInit
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.StringVar(target, name, value, usage)
|
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()
|
flag.Parse()
|
||||||
sort.Sort(&vars)
|
sort.Sort(&flagList)
|
||||||
|
|
||||||
for pass := 1; pass <= 2; pass++ {
|
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) {
|
if (pass == 1 && v.name == doFirst) || (pass == 2 && v.name != doFirst) {
|
||||||
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
|
flagList.items[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 flagList.items {
|
||||||
if vv.required {
|
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",
|
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)
|
fmt.Fprintln(os.Stderr)
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
os.Exit(2)
|
|
||||||
return
|
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
6
core/env/logger.go
vendored
|
@ -12,11 +12,13 @@
|
||||||
// Package env provides runtime, server level setup and configuration
|
// Package env provides runtime, server level setup and configuration
|
||||||
package env
|
package env
|
||||||
|
|
||||||
import "github.com/jmoiron/sqlx"
|
import (
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
)
|
||||||
|
|
||||||
// Logger provides the interface for Documize compatible loggers.
|
// Logger provides the interface for Documize compatible loggers.
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Info(message string)
|
Info(message string)
|
||||||
Error(message string, err error)
|
Error(message string, err error)
|
||||||
SetDB(db *sqlx.DB)
|
SetDB(l Logger, db *sqlx.DB) Logger
|
||||||
}
|
}
|
||||||
|
|
2
core/product.go → core/env/product.go
vendored
2
core/product.go → core/env/product.go
vendored
|
@ -9,7 +9,7 @@
|
||||||
//
|
//
|
||||||
// https://documize.com
|
// https://documize.com
|
||||||
|
|
||||||
package core
|
package env
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
6
core/env/runtime.go
vendored
6
core/env/runtime.go
vendored
|
@ -17,6 +17,8 @@ import "github.com/jmoiron/sqlx"
|
||||||
// Runtime provides access to database, logger and other server-level scoped objects.
|
// Runtime provides access to database, logger and other server-level scoped objects.
|
||||||
// Use Context for per-request values.
|
// Use Context for per-request values.
|
||||||
type Runtime struct {
|
type Runtime struct {
|
||||||
Db *sqlx.DB
|
Flags Flags
|
||||||
Log Logger
|
Db *sqlx.DB
|
||||||
|
Log Logger
|
||||||
|
Product ProdInfo
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,23 +19,14 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/documize/community/core/env"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var environment = "Non-production"
|
var environment = "Non-production"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
log.SetFormatter(new(log.TextFormatter))
|
log.SetFormatter(new(log.TextFormatter))
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
log.SetLevel(log.DebugLevel)
|
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.
|
// Debug logs a message for debug purposes.
|
||||||
|
|
|
@ -16,12 +16,12 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/documize/community/core/env"
|
"github.com/documize/community/core/api"
|
||||||
"github.com/documize/community/core/secrets"
|
"github.com/documize/community/core/secrets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SiteMode defines that the web server should show the system to be in a particular state.
|
// SiteMode defines that the web server should show the system to be in a particular state.
|
||||||
var SiteMode string
|
// var SiteMode string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// SiteModeNormal serves app
|
// SiteModeNormal serves app
|
||||||
|
@ -40,8 +40,8 @@ var SiteInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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
|
SiteInfo.DBhash = secrets.GenerateRandomPassword() // do this only once
|
||||||
}
|
}
|
||||||
|
|
||||||
// EmbedHandler is defined in each embed directory
|
// EmbedHandler is defined in each embed directory
|
||||||
|
@ -57,7 +57,7 @@ var Embed EmbedHandler
|
||||||
// EmberHandler provides the webserver for pages developed using the Ember programming environment.
|
// EmberHandler provides the webserver for pages developed using the Ember programming environment.
|
||||||
func EmberHandler(w http.ResponseWriter, r *http.Request) {
|
func EmberHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
filename := "index.html"
|
filename := "index.html"
|
||||||
switch SiteMode {
|
switch api.Runtime.Flags.SiteMode {
|
||||||
case SiteModeOffline:
|
case SiteModeOffline:
|
||||||
filename = "offline.html"
|
filename = "offline.html"
|
||||||
case SiteModeSetup:
|
case SiteModeSetup:
|
||||||
|
|
178
edition/community.go
Normal file
178
edition/community.go
Normal 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
57
edition/logging/logger.go
Normal 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
12
meta.json
12
meta.json
|
@ -1,16 +1,16 @@
|
||||||
{
|
{
|
||||||
"community":
|
"community":
|
||||||
{
|
{
|
||||||
"version": "1.49.2",
|
"version": "1.50.0",
|
||||||
"major": 1,
|
"major": 1,
|
||||||
"minor": 49,
|
"minor": 50,
|
||||||
"patch": 2
|
"patch": 0
|
||||||
},
|
},
|
||||||
"enterprise":
|
"enterprise":
|
||||||
{
|
{
|
||||||
"version": "1.51.2",
|
"version": "1.52.0",
|
||||||
"major": 1,
|
"major": 1,
|
||||||
"minor": 51,
|
"minor": 52,
|
||||||
"patch": 2
|
"patch": 0
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue