Browse Source

Debug mode at runtime, flags to constants

Vova Tkach 6 years ago
parent
commit
951035a2c2
4 changed files with 32 additions and 24 deletions
  1. 1 1
      Makefile
  2. 7 1
      consts/consts.go
  3. 2 2
      logger/logger.go
  4. 22 20
      main.go

+ 1 - 1
Makefile

@@ -28,7 +28,7 @@ test:
 	go test ./...
 
 run:
-	@./fave -host 0.0.0.0 -port 8080 -dir ./hosts
+	@./fave -host 0.0.0.0 -port 8080 -dir ./hosts -debug true
 
 update:
 	go mod vendor

+ 7 - 1
consts/consts.go

@@ -4,7 +4,6 @@ import (
 	"html/template"
 )
 
-const Debug = true
 const ServerVersion = "1.0.0"
 const AssetsVersion = "8"
 const AssetsPath = "assets"
@@ -25,6 +24,13 @@ const AssetsSysLogoPng = AssetsPath + "/sys/logo.png"
 const AssetsSysLogoSvg = AssetsPath + "/sys/logo.svg"
 const AssetsSysStylesCss = AssetsPath + "/sys/styles.css"
 
+// Make global for other packages
+var ParamHost string
+var ParamPort int
+var ParamWwwDir string
+var ParamDebug bool
+
+// For admin panel
 type BreadCrumb struct {
 	Name string
 	Link string

+ 2 - 2
logger/logger.go

@@ -23,7 +23,7 @@ type Logger struct {
 }
 
 func (this *Logger) console(msg logMsg) {
-	if consts.Debug {
+	if consts.ParamDebug {
 		if !msg.isError {
 			fmt.Fprintln(os.Stdout, "\033[0;32m[ACCESS] "+msg.message+"\033[0m")
 		} else {
@@ -41,7 +41,7 @@ func (this *Logger) console(msg logMsg) {
 
 func (this *Logger) write(msg logMsg) {
 	// Ignore file if debug
-	if consts.Debug {
+	if consts.ParamDebug {
 		this.console(msg)
 		return
 	}

+ 22 - 20
main.go

@@ -20,32 +20,34 @@ import (
 	"github.com/vladimirok5959/golang-server-static/static"
 )
 
-var ParamHost string
-var ParamPort int
-var ParamWwwDir string
-
 func init() {
-	flag.StringVar(&ParamHost, "host", "0.0.0.0", "server host")
-	flag.IntVar(&ParamPort, "port", 8080, "server port")
-	flag.StringVar(&ParamWwwDir, "dir", "", "virtual hosts directory")
+	flag.StringVar(&consts.ParamHost, "host", "0.0.0.0", "server host")
+	flag.IntVar(&consts.ParamPort, "port", 8080, "server port")
+	flag.StringVar(&consts.ParamWwwDir, "dir", "", "virtual hosts directory")
+	flag.BoolVar(&consts.ParamDebug, "debug", false, "debug mode with ignoring log files")
 	flag.Parse()
 }
 
 func main() {
 	// Universal, params by env vars
-	if ParamHost == "0.0.0.0" {
+	if consts.ParamHost == "0.0.0.0" {
 		if os.Getenv("FAVE_HOST") != "" {
-			ParamHost = os.Getenv("FAVE_HOST")
+			consts.ParamHost = os.Getenv("FAVE_HOST")
 		}
 	}
-	if ParamPort == 8080 {
+	if consts.ParamPort == 8080 {
 		if os.Getenv("FAVE_PORT") != "" {
-			ParamPort = utils.StrToInt(os.Getenv("FAVE_PORT"))
+			consts.ParamPort = utils.StrToInt(os.Getenv("FAVE_PORT"))
 		}
 	}
-	if ParamWwwDir == "" {
+	if consts.ParamWwwDir == "" {
 		if os.Getenv("FAVE_DIR") != "" {
-			ParamWwwDir = os.Getenv("FAVE_DIR")
+			consts.ParamWwwDir = os.Getenv("FAVE_DIR")
+		}
+	}
+	if consts.ParamDebug == false {
+		if os.Getenv("FAVE_DEBUG") == "true" {
+			consts.ParamDebug = true
 		}
 	}
 
@@ -54,18 +56,18 @@ func main() {
 	defer lg.Close()
 
 	// Check www dir
-	ParamWwwDir = utils.FixPath(ParamWwwDir)
-	if !utils.IsDirExists(ParamWwwDir) {
+	consts.ParamWwwDir = utils.FixPath(consts.ParamWwwDir)
+	if !utils.IsDirExists(consts.ParamWwwDir) {
 		lg.Log("Virtual hosts directory is not exists", nil, true)
 		lg.Log("Example: ./fave -host 127.0.0.1 -port 80 -dir ./hosts", nil, true)
 		return
 	}
 
 	// Attach www dir to logger
-	lg.SetWwwDir(ParamWwwDir)
+	lg.SetWwwDir(consts.ParamWwwDir)
 
 	// Session cleaner
-	sess_cl_ch, sess_cl_stop := session_clean_start(ParamWwwDir)
+	sess_cl_ch, sess_cl_stop := session_clean_start(consts.ParamWwwDir)
 	defer session_clean_stop(sess_cl_ch, sess_cl_stop)
 
 	// Init mounted resources
@@ -79,7 +81,7 @@ func main() {
 	mods := modules.New()
 
 	// Init and start web server
-	bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", ParamHost, ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
+	bootstrap.Start(lg.Handler, fmt.Sprintf("%s:%d", consts.ParamHost, consts.ParamPort), 30, consts.AssetsPath, func(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
 	}, func(w http.ResponseWriter, r *http.Request) {
 		// Schema
@@ -95,10 +97,10 @@ func main() {
 		// Host and port
 		host, port := utils.ExtractHostPort(r.Host, false)
 		curr_host := host
-		vhost_dir := ParamWwwDir + string(os.PathSeparator) + host
+		vhost_dir := consts.ParamWwwDir + string(os.PathSeparator) + host
 		if !utils.IsDirExists(vhost_dir) {
 			curr_host = "localhost"
-			vhost_dir = ParamWwwDir + string(os.PathSeparator) + "localhost"
+			vhost_dir = consts.ParamWwwDir + string(os.PathSeparator) + "localhost"
 		}
 
 		// Check for minimal dir structure