module_api.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package modules
  2. import (
  3. // "html"
  4. "net/http"
  5. "golang-fave/assets"
  6. // "golang-fave/consts"
  7. // "golang-fave/engine/builder"
  8. "golang-fave/engine/fetdata"
  9. "golang-fave/engine/wrapper"
  10. "golang-fave/utils"
  11. )
  12. func (this *Modules) RegisterModule_Api() *Module {
  13. return this.newModule(MInfo{
  14. WantDB: true,
  15. Mount: "api",
  16. Name: "Api",
  17. Order: 803,
  18. System: true,
  19. Icon: assets.SysSvgIconPage,
  20. Sub: &[]MISub{},
  21. }, func(wrap *wrapper.Wrapper) {
  22. if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "api" && wrap.UrlArgs[1] == "products" {
  23. // Fix url
  24. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  25. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  26. return
  27. }
  28. // XML
  29. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  30. wrap.W.Header().Set("Content-Type", "text/xml; charset=utf-8")
  31. wrap.W.WriteHeader(http.StatusOK)
  32. wrap.W.Write([]byte("XML"))
  33. } else if len(wrap.UrlArgs) == 1 {
  34. // Fix url
  35. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  36. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  37. return
  38. }
  39. // Some info
  40. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  41. wrap.W.WriteHeader(http.StatusOK)
  42. wrap.W.Write([]byte("Fave engine API mount point!"))
  43. } else {
  44. // User error 404 page
  45. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true), http.StatusNotFound)
  46. return
  47. }
  48. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  49. // No any page for back-end
  50. return "", "", ""
  51. })
  52. }