module_api.go 2.0 KB

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