mirror of
https://github.com/documize/community.git
synced 2025-07-25 08:09:43 +02:00
refactored flag/env loading
This commit is contained in:
parent
dc49dbbeff
commit
68130122e7
23 changed files with 1128 additions and 909 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue