Browse Source

Backend by module

Vova Tkach 6 years ago
parent
commit
2a959c0721

+ 1 - 25
engine.go

@@ -4,7 +4,6 @@ import (
 	"database/sql"
 	"net/http"
 	"strings"
-	//"log"
 
 	"golang-fave/engine/backend"
 	"golang-fave/engine/frontend"
@@ -51,30 +50,7 @@ func handlerPage(wrapper *wrapper.Wrapper) bool {
 		return true
 	}
 
-	// Parse url
-	/*
-		url_buff := wrapper.R.URL.Path
-		if len(url_buff) >= 1 && url_buff[:1] == "/" {
-			url_buff = url_buff[1:]
-		}
-		if len(url_buff) >= 1 && url_buff[len(url_buff)-1:] == "/" {
-			url_buff = url_buff[:len(url_buff)-1]
-		}
-
-		log.Printf("###############")
-		log.Printf("(%s)", url_buff)
-	*/
-
-	/*
-		url_args := utils.UrlToArray(wrapper.R.URL.Path)
-		log.Printf("############### (%d)", len(url_args))
-		for key, value := range url_args {
-			log.Printf(">>> (%d) -> (%s)", key, value)
-		}
-	*/
-
-	// log.Printf("###############")
-
+	// Parse url params
 	url_args := utils.UrlToArray(wrapper.R.URL.Path)
 
 	// Run WebSite or CP

+ 28 - 59
engine/backend/backend.go

@@ -6,6 +6,7 @@ import (
 	"html/template"
 
 	"golang-fave/constants"
+	"golang-fave/engine/backend/modules"
 	"golang-fave/engine/wrapper"
 
 	templates "golang-fave/engine/wrapper/resources/templates"
@@ -21,6 +22,7 @@ type Backend struct {
 
 type TmplData struct {
 	Title          string
+	BodyClasses    string
 	UserId         int
 	UserFirstName  string
 	UserLastName   string
@@ -63,80 +65,47 @@ func (this *Backend) Run() bool {
 		return this.wrapper.TmplBackEnd(templates.CpLogin, nil)
 	}
 
-	// wrapper.R.URL.Path
-
 	// Display cp page
-	/*
-		(*this.wrapper.W).Write([]byte(`Admin panel here...`))
-		return true
-	*/
-	// return this.wrapper.TmplBackEnd(templates.CpBase, nil)
-
-	/*
-		tmpl, err := template.New("template").Parse(string(templates.CpBase))
-		if err == nil {
-			(*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
-			tmpl.Execute(*this.wrapper.W, wrapper.TmplDataAll{
-				System: this.wrapper.TmplGetSystemData(),
-				Data: TmplData{
-					Title: "Fave " + constants.ServerVersion,
-					UserEmail: this.user.A_email,
-					SidebarLeft: "Sidebar left",
-					Content: "Content",
-					SidebarRight: "Sidebar right",
-				},
-			})
-			return true
-		}
-	*/
-
-	// Get parsed template as string
-	// https://coderwall.com/p/ns60fq/simply-output-go-html-template-execution-to-strings
+	body_class := "cp"
 
-	// http://localhost:8080/admin/
+	// Get module content here
+	page_sb_left := ""
+	page_content := ""
+	page_sb_right := ""
 
-	/*
-		sidebar_left := string(`<ul class="nav flex-column">
-			<li class="nav-item active">
-				<a class="nav-link" href="#">Pages</a>
-				<ul class="nav flex-column">
-					<li class="nav-item active">
-						<a class="nav-link" href="#">List of pages</a>
-					</li>
-					<li class="nav-item">
-						<a class="nav-link" href="#">Add new page</a>
-					</li>
-				</ul>
-			</li>
-			<li class="nav-item">
-				<a class="nav-link" href="#">Link 2</a>
-			</li>
-			<li class="nav-item">
-				<a class="nav-link" href="#">Link 3</a>
-			</li>
-			<li class="nav-item">
-				<a class="nav-link" href="#">Link 4</a>
-			</li>
-		</ul>`)
-	*/
+	mdl := modules.New(this.wrapper, this.db, this.user, this.urls)
+	if mdl.Run() {
+		page_content = mdl.GetContent()
+		page_sb_right = mdl.GetSidebarRight()
+	}
+	page_sb_left = mdl.GetSidebarLeft()
 
-	sidebar_left := ""
-	content := "Content"
-	sidebar_right := "Sidebar right"
+	// If right sidebar and content need to show
+	if page_sb_left != "" {
+		body_class = body_class + " cp-sidebar-left"
+	}
+	if page_content == "" {
+		body_class = body_class + " cp-404"
+		page_content = "Panel 404"
+	}
+	if page_sb_right != "" {
+		body_class = body_class + " cp-sidebar-right"
+	}
 
 	page := this.wrapper.TmplParseToString(templates.CpBase, wrapper.TmplDataAll{
 		System: this.wrapper.TmplGetSystemData(),
 		Data: TmplData{
 			Title:          "Fave " + constants.ServerVersion,
+			BodyClasses:    body_class,
 			UserId:         this.user.A_id,
 			UserFirstName:  this.user.A_first_name,
 			UserLastName:   this.user.A_last_name,
 			UserEmail:      this.user.A_email,
 			UserPassword:   "",
 			UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(this.user.A_email) + "?s=80&r=g",
-			SidebarLeft:    template.HTML(sidebar_left),
-			Content:        template.HTML(content),
-			SidebarRight:   template.HTML(sidebar_right),
+			SidebarLeft:    template.HTML(page_sb_left),
+			Content:        template.HTML(page_content),
+			SidebarRight:   template.HTML(page_sb_right),
 		},
 	})
 	(*this.wrapper.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")

+ 32 - 0
engine/backend/modules/module_index.go

@@ -0,0 +1,32 @@
+package modules
+
+import (
+	utils "golang-fave/engine/wrapper/utils"
+)
+
+func (this *Module) Module_index() {
+	// Do something here...
+}
+
+func (this *Module) Module_index_alias() string {
+	return "index"
+}
+
+func (this *Module) Module_index_name() string {
+	return "Pages"
+}
+
+func (this *Module) Module_index_submenu() []utils.ModuleSubMenu {
+	result := make([]utils.ModuleSubMenu, 0)
+	result = append(result, utils.ModuleSubMenu{Alias: "default", Name: "List of pages"})
+	result = append(result, utils.ModuleSubMenu{Alias: "add", Name: "Add new page"})
+	return result
+}
+
+func (this *Module) Module_index_content() string {
+	return "Index content"
+}
+
+func (this *Module) Module_index_sidebar() string {
+	return "Index right sidebar"
+}

+ 32 - 0
engine/backend/modules/module_users.go

@@ -0,0 +1,32 @@
+package modules
+
+import (
+	utils "golang-fave/engine/wrapper/utils"
+)
+
+func (this *Module) Module_users() {
+	// Do something here...
+}
+
+func (this *Module) Module_users_alias() string {
+	return "users"
+}
+
+func (this *Module) Module_users_name() string {
+	return "Users"
+}
+
+func (this *Module) Module_users_submenu() []utils.ModuleSubMenu {
+	result := make([]utils.ModuleSubMenu, 0)
+	result = append(result, utils.ModuleSubMenu{Alias: "default", Name: "List of users"})
+	result = append(result, utils.ModuleSubMenu{Alias: "add", Name: "Add new user"})
+	return result
+}
+
+func (this *Module) Module_users_content() string {
+	return "Users content"
+}
+
+func (this *Module) Module_users_sidebar() string {
+	return "Users right sidebar"
+}

+ 131 - 0
engine/backend/modules/modules.go

@@ -0,0 +1,131 @@
+package modules
+
+import (
+	"database/sql"
+	_ "github.com/go-sql-driver/mysql"
+
+	"reflect"
+	"strconv"
+	"strings"
+
+	"golang-fave/engine/wrapper"
+
+	utils "golang-fave/engine/wrapper/utils"
+)
+
+type Module struct {
+	wrapper *wrapper.Wrapper
+	db      *sql.DB
+	user    *utils.MySql_user
+	urls    *[]string
+	mmod    string
+	smod    string
+	imod    int
+}
+
+func (this *Module) module_get_name(name string) string {
+	mname := "Module_" + name + "_name"
+	if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
+		result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
+		return result[0].String()
+	}
+	return ""
+}
+
+func (this *Module) module_get_submenu(name string) string {
+	mname := "Module_" + name + "_submenu"
+	if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
+		result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
+		result_array := result[0].Interface().([]utils.ModuleSubMenu)
+		result_html := ""
+		for _, value := range result_array {
+			class := ""
+			if name == this.mmod && value.Alias == this.smod {
+				class = " active"
+			}
+			result_html += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + name + `/` + value.Alias + `/">` + value.Name + `</a></li>`
+		}
+		if result_html != "" {
+			result_html = `<ul class="nav flex-column">` + result_html + `</ul>`
+		}
+		return result_html
+	}
+	return ""
+}
+
+func New(wrapper *wrapper.Wrapper, db *sql.DB, user *utils.MySql_user, url_args *[]string) *Module {
+	mmod := "index"
+	smod := "default"
+	imod := 0
+	if len(*url_args) >= 2 {
+		mmod = (*url_args)[1]
+	}
+	if len(*url_args) >= 3 {
+		smod = (*url_args)[2]
+	}
+	if len(*url_args) >= 4 {
+		if val, err := strconv.Atoi((*url_args)[3]); err == nil {
+			imod = val
+		}
+	}
+	return &Module{wrapper, db, user, url_args, mmod, smod, imod}
+}
+
+func (this *Module) Run() bool {
+	mname := "Module_" + this.mmod
+	if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
+		reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
+		return true
+	}
+	return false
+}
+
+func (this *Module) GetSidebarLeft() string {
+	sidebar := `<ul class="nav flex-column">`
+
+	// Make module list
+	aType := reflect.TypeOf(this)
+	for i := 0; i < aType.NumMethod(); i++ {
+		aMethod := aType.Method(i)
+		if strings.HasPrefix(aMethod.Name, "Module_") && strings.HasSuffix(aMethod.Name, "_alias") {
+			// Extract module alias
+			alias := aMethod.Name[7:][:5]
+
+			// Item active class
+			class := ""
+			if alias == this.mmod {
+				class = " active"
+			}
+
+			// Active item sub menu
+			submenu := ""
+			if alias == this.mmod {
+				submenu = this.module_get_submenu(alias)
+			}
+
+			// Add module to list
+			sidebar += `<li class="nav-item` + class + `"><a class="nav-link" href="/cp/` + alias + `/">` + this.module_get_name(alias) + `</a>` + submenu + `</li>`
+		}
+	}
+
+	sidebar += `</ul>`
+	return sidebar
+}
+
+func (this *Module) GetContent() string {
+	mname := "Module_" + this.mmod + "_content"
+	if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
+		result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
+		return result[0].String()
+	}
+	return ""
+}
+
+func (this *Module) GetSidebarRight() string {
+	mname := "Module_" + this.mmod + "_sidebar"
+	if _, ok := reflect.TypeOf(this).MethodByName(mname); ok {
+		result := reflect.ValueOf(this).MethodByName(mname).Call([]reflect.Value{})
+		return result[0].String()
+	}
+	return ""
+}

File diff suppressed because it is too large
+ 0 - 0
engine/wrapper/resources/templates/cp.base.go


+ 1 - 1
engine/wrapper/resources/templates/cp.base.html

@@ -10,7 +10,7 @@
 		<link rel="stylesheet" href="{{$.System.PathCssCpStyles}}">
 		<link rel="shortcut icon" href="{{$.System.PathIcoFav}}" type="image/x-icon" />
 	</head>
-	<body class="cp cp-sidebar-left cp-sidebar-right">
+	<body class="{{$.Data.BodyClasses}}">
 		<div class="modal fade" id="sys-modal-user-settings" tabindex="-1" role="dialog" aria-labelledby="sysModalUserSettingsLabel" aria-hidden="true">
 			<div class="modal-dialog modal-dialog-centered" role="document">
 				<div class="modal-content">

+ 6 - 0
engine/wrapper/utils/struct_msubmenu.go

@@ -0,0 +1,6 @@
+package utils
+
+type ModuleSubMenu struct {
+	Alias string
+	Name  string
+}

Some files were not shown because too many files changed in this diff