Browse Source

Code refactoring

Vova Tkach 6 years ago
parent
commit
c3e78e5c61
6 changed files with 40 additions and 40 deletions
  1. 5 5
      backend.go
  2. 19 19
      engine/wrapper/static.go
  3. 2 2
      engine/wrapper/wrapper.go
  4. 6 6
      frontend.go
  5. 1 1
      hosts/localhost/config/mysql.example.json
  6. 7 7
      main.go

+ 5 - 5
backend.go

@@ -2,16 +2,16 @@ package main
 
 
 import (
 import (
 	"golang-fave/engine/wrapper"
 	"golang-fave/engine/wrapper"
-	Templates "golang-fave/engine/wrapper/resources/templates"
+	templates "golang-fave/engine/wrapper/resources/templates"
 	utils "golang-fave/engine/wrapper/utils"
 	utils "golang-fave/engine/wrapper/utils"
 )
 )
 
 
-func handleBackEnd(e *wrapper.Wrapper) bool {
+func handleBackEnd(wrapper *wrapper.Wrapper) bool {
 	// MySQL config page
 	// MySQL config page
-	if !utils.IsMySqlConfigExists(e.DirVhostHome) {
-		return e.TmplBackEnd(Templates.CpMySQL, nil)
+	if !utils.IsMySqlConfigExists(wrapper.DirVhostHome) {
+		return wrapper.TmplBackEnd(templates.CpMySQL, nil)
 	}
 	}
 
 
 	// Login page
 	// Login page
-	return e.TmplBackEnd(Templates.CpLogin, nil)
+	return wrapper.TmplBackEnd(templates.CpLogin, nil)
 }
 }

+ 19 - 19
engine/wrapper/static.go

@@ -6,11 +6,11 @@ import (
 	"net/http"
 	"net/http"
 	"os"
 	"os"
 
 
-	Images "golang-fave/engine/wrapper/resources/images"
-	Others "golang-fave/engine/wrapper/resources/others"
-	Scripts "golang-fave/engine/wrapper/resources/scripts"
-	Styles "golang-fave/engine/wrapper/resources/styles"
-	Templates "golang-fave/engine/wrapper/resources/templates"
+	images "golang-fave/engine/wrapper/resources/images"
+	others "golang-fave/engine/wrapper/resources/others"
+	scripts "golang-fave/engine/wrapper/resources/scripts"
+	styles "golang-fave/engine/wrapper/resources/styles"
+	templates "golang-fave/engine/wrapper/resources/templates"
 )
 )
 
 
 type tmplDataErrorMsg struct {
 type tmplDataErrorMsg struct {
@@ -21,57 +21,57 @@ func (e *Wrapper) staticResource() bool {
 	if e.R.URL.Path == "/assets/sys/styles.css" {
 	if e.R.URL.Path == "/assets/sys/styles.css" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "text/css")
 		(*e.W).Header().Set("Content-Type", "text/css")
-		(*e.W).Write(Styles.File_assets_sys_styles_css)
+		(*e.W).Write(styles.File_assets_sys_styles_css)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/cp/styles.css" {
 	} else if e.R.URL.Path == "/assets/cp/styles.css" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "text/css")
 		(*e.W).Header().Set("Content-Type", "text/css")
-		(*e.W).Write(Styles.File_assets_cp_styles_css)
+		(*e.W).Write(styles.File_assets_cp_styles_css)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/sys/bootstrap.css" {
 	} else if e.R.URL.Path == "/assets/sys/bootstrap.css" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "text/css")
 		(*e.W).Header().Set("Content-Type", "text/css")
-		(*e.W).Write(Styles.File_assets_sys_bootstrap_css)
+		(*e.W).Write(styles.File_assets_sys_bootstrap_css)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/sys/jquery.js" {
 	} else if e.R.URL.Path == "/assets/sys/jquery.js" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
 		(*e.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
-		(*e.W).Write(Scripts.File_assets_sys_jquery_js)
+		(*e.W).Write(scripts.File_assets_sys_jquery_js)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/sys/popper.js" {
 	} else if e.R.URL.Path == "/assets/sys/popper.js" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
 		(*e.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
-		(*e.W).Write(Scripts.File_assets_sys_popper_js)
+		(*e.W).Write(scripts.File_assets_sys_popper_js)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/sys/bootstrap.js" {
 	} else if e.R.URL.Path == "/assets/sys/bootstrap.js" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
 		(*e.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
-		(*e.W).Write(Scripts.File_assets_sys_bootstrap_js)
+		(*e.W).Write(scripts.File_assets_sys_bootstrap_js)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/cp/scripts.js" {
 	} else if e.R.URL.Path == "/assets/cp/scripts.js" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
 		(*e.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
-		(*e.W).Write(Scripts.File_assets_cp_scripts_js)
+		(*e.W).Write(scripts.File_assets_cp_scripts_js)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/sys/logo.svg" {
 	} else if e.R.URL.Path == "/assets/sys/logo.svg" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "image/svg+xml")
 		(*e.W).Header().Set("Content-Type", "image/svg+xml")
-		(*e.W).Write(Others.File_assets_sys_logo_svg)
+		(*e.W).Write(others.File_assets_sys_logo_svg)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/sys/bg.png" {
 	} else if e.R.URL.Path == "/assets/sys/bg.png" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "image/png")
 		(*e.W).Header().Set("Content-Type", "image/png")
-		(*e.W).Write(Images.File_assets_sys_bg_png)
+		(*e.W).Write(images.File_assets_sys_bg_png)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/sys/logo.png" {
 	} else if e.R.URL.Path == "/assets/sys/logo.png" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "image/png")
 		(*e.W).Header().Set("Content-Type", "image/png")
-		(*e.W).Write(Images.File_assets_sys_logo_png)
+		(*e.W).Write(images.File_assets_sys_logo_png)
 		return true
 		return true
 	} else if e.R.URL.Path == "/assets/sys/fave.ico" {
 	} else if e.R.URL.Path == "/assets/sys/fave.ico" {
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Cache-Control", "public, max-age=31536000")
 		(*e.W).Header().Set("Content-Type", "image/x-icon")
 		(*e.W).Header().Set("Content-Type", "image/x-icon")
-		(*e.W).Write(Others.File_assets_sys_fave_ico)
+		(*e.W).Write(others.File_assets_sys_fave_ico)
 		return true
 		return true
 	}
 	}
 	return false
 	return false
@@ -121,7 +121,7 @@ func (e *Wrapper) printPageDefault() {
 	}
 	}
 
 
 	// Default page
 	// Default page
-	tmpl, err := template.New("template").Parse(string(Templates.PageDefault))
+	tmpl, err := template.New("template").Parse(string(templates.PageDefault))
 	if err != nil {
 	if err != nil {
 		e.printTmplPageError(err)
 		e.printTmplPageError(err)
 		return
 		return
@@ -144,7 +144,7 @@ func (e *Wrapper) printPage404() {
 	}
 	}
 
 
 	// Default error page
 	// Default error page
-	tmpl, err := template.New("template").Parse(string(Templates.PageError404))
+	tmpl, err := template.New("template").Parse(string(templates.PageError404))
 	if err != nil {
 	if err != nil {
 		e.printTmplPageError(err)
 		e.printTmplPageError(err)
 		return
 		return
@@ -158,7 +158,7 @@ func (e *Wrapper) printPage404() {
 }
 }
 
 
 func (e *Wrapper) printTmplPageError(perr error) {
 func (e *Wrapper) printTmplPageError(perr error) {
-	tmpl, err := template.New("template").Parse(string(Templates.PageTmplError))
+	tmpl, err := template.New("template").Parse(string(templates.PageTmplError))
 	if err != nil {
 	if err != nil {
 		(*e.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 		(*e.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
 		(*e.W).WriteHeader(http.StatusInternalServerError)
 		(*e.W).WriteHeader(http.StatusInternalServerError)

+ 2 - 2
engine/wrapper/wrapper.go

@@ -9,7 +9,7 @@ import (
 	"strings"
 	"strings"
 
 
 	"golang-fave/engine/sessions"
 	"golang-fave/engine/sessions"
-	Templates "golang-fave/engine/wrapper/resources/templates"
+	templates "golang-fave/engine/wrapper/resources/templates"
 )
 )
 
 
 const C_AssetsVersion = "3"
 const C_AssetsVersion = "3"
@@ -60,7 +60,7 @@ func (e *Wrapper) tmplGetSystemData() tmplDataSystem {
 		PathJsPopper:     e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/popper.js" + version,
 		PathJsPopper:     e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/popper.js" + version,
 		PathJsBootstrap:  e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/bootstrap.js" + version,
 		PathJsBootstrap:  e.R.URL.Scheme + "://" + e.R.Host + "/assets/sys/bootstrap.js" + version,
 		PathJsCpScripts:  e.R.URL.Scheme + "://" + e.R.Host + "/assets/cp/scripts.js" + version,
 		PathJsCpScripts:  e.R.URL.Scheme + "://" + e.R.Host + "/assets/cp/scripts.js" + version,
-		BlockModalSysMsg: template.HTML(Templates.BlockModalSysMsg),
+		BlockModalSysMsg: template.HTML(templates.BlockModalSysMsg),
 	}
 	}
 }
 }
 
 

+ 6 - 6
frontend.go

@@ -20,17 +20,17 @@ type TmplData struct {
 	MenuItems       []MenuItem
 	MenuItems       []MenuItem
 }
 }
 
 
-func handleFrontEnd(e *wrapper.Wrapper) bool {
+func handleFrontEnd(wrapper *wrapper.Wrapper) bool {
 	// Redirect to CP, if MySQL config file is not exists
 	// Redirect to CP, if MySQL config file is not exists
-	if !utils.IsMySqlConfigExists(e.DirVhostHome) {
-		(*e.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-		http.Redirect(*e.W, e.R, e.R.URL.Scheme+"://"+e.R.Host+"/cp/", 302)
+	if !utils.IsMySqlConfigExists(wrapper.DirVhostHome) {
+		(*wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+		http.Redirect(*wrapper.W, wrapper.R, wrapper.R.URL.Scheme+"://"+wrapper.R.Host+"/cp/", 302)
 		return true
 		return true
 	}
 	}
 
 
 	// Else logic here
 	// Else logic here
-	if e.R.URL.Path == "/" {
-		return e.TmplFrontEnd("index", TmplData{
+	if wrapper.R.URL.Path == "/" {
+		return wrapper.TmplFrontEnd("index", TmplData{
 			MetaTitle:       "Meta Title",
 			MetaTitle:       "Meta Title",
 			MetaKeywords:    "Meta Keywords",
 			MetaKeywords:    "Meta Keywords",
 			MetaDescription: "Meta Description",
 			MetaDescription: "Meta Description",

+ 1 - 1
hosts/localhost/config/mysql.example.json

@@ -1 +1 @@
-{"Host":"localhost","Name":"","User":"","Password":""}
+{"Host":"localhost","Port":"3306","Name":"","User":"","Password":""}

+ 7 - 7
main.go

@@ -117,20 +117,20 @@ func handler(w http.ResponseWriter, r *http.Request) {
 
 
 	// Create and start engine
 	// Create and start engine
 	wrapper.New(&w, r, host, port, FParamWwwDir, FVhostHomeDir, C_Debug).
 	wrapper.New(&w, r, host, port, FParamWwwDir, FVhostHomeDir, C_Debug).
-		Run(func(e *wrapper.Wrapper) bool {
+		Run(func(wrapper *wrapper.Wrapper) bool {
 			// Actions
 			// Actions
-			action := actions.New(e)
+			action := actions.New(wrapper)
 			if action.Call() {
 			if action.Call() {
-				e.Log("200")
-				e.Session.Save()
+				wrapper.Log("200")
+				wrapper.Session.Save()
 				return true
 				return true
 			}
 			}
 
 
 			// Pages
 			// Pages
-			if !(e.R.URL.Path == "/cp" || strings.HasPrefix(e.R.URL.Path, "/cp/")) {
-				return handleFrontEnd(e)
+			if !(wrapper.R.URL.Path == "/cp" || strings.HasPrefix(wrapper.R.URL.Path, "/cp/")) {
+				return handleFrontEnd(wrapper)
 			} else {
 			} else {
-				return handleBackEnd(e)
+				return handleBackEnd(wrapper)
 			}
 			}
 		})
 		})
 }
 }