1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-23 15:19: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

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