Vova Tkach 5 years ago
parent
commit
e7e407bc64
2 changed files with 63 additions and 2 deletions
  1. 57 0
      modules/module_api.go
  2. 6 2
      modules/modules.go

+ 57 - 0
modules/module_api.go

@@ -0,0 +1,57 @@
+package modules
+
+import (
+	// "html"
+	"net/http"
+
+	"golang-fave/assets"
+	// "golang-fave/consts"
+	// "golang-fave/engine/builder"
+	"golang-fave/engine/fetdata"
+	"golang-fave/engine/wrapper"
+	"golang-fave/utils"
+)
+
+func (this *Modules) RegisterModule_Api() *Module {
+	return this.newModule(MInfo{
+		WantDB: true,
+		Mount:  "api",
+		Name:   "Api",
+		Order:  803,
+		System: true,
+		Icon:   assets.SysSvgIconPage,
+		Sub:    &[]MISub{},
+	}, func(wrap *wrapper.Wrapper) {
+		if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "products" {
+			// Fix url
+			if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
+				http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
+				return
+			}
+
+			// XML
+			wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+			wrap.W.Header().Set("Content-Type", "text/xml; charset=utf-8")
+			wrap.W.WriteHeader(http.StatusOK)
+			wrap.W.Write([]byte("XML"))
+		} else if len(wrap.UrlArgs) == 1 {
+			// Fix url
+			if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
+				http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
+				return
+			}
+
+			// Some info
+			wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
+			wrap.W.WriteHeader(http.StatusOK)
+			wrap.W.Write([]byte("Fave engine API mount point!"))
+		} else {
+			// User error 404 page
+			wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
+			return
+		}
+	}, func(wrap *wrapper.Wrapper) (string, string, string) {
+		// No any page for back-end
+		return "", "", ""
+	})
+}

+ 6 - 2
modules/modules.go

@@ -184,7 +184,9 @@ func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper, sys bool) string {
 		if mod.Mount == "index" {
 			href = `/cp/`
 		}
-		html += `<a class="dropdown-item` + class + `" href="` + href + `">` + mod.Name + `</a>`
+		if !(sys && mod.Mount == "api") {
+			html += `<a class="dropdown-item` + class + `" href="` + href + `">` + mod.Name + `</a>`
+		}
 	}
 	return html
 }
@@ -211,7 +213,9 @@ func (this *Modules) getSidebarModules(wrap *wrapper.Wrapper) string {
 		if !mod.System {
 			html_def += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
 		} else {
-			html_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
+			if mod.Mount != "api" {
+				html_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
+			}
 		}
 	}
 	if html_def != "" {