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

@ -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