module_api.go 2.0 KB

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