static.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package wrapper
  2. import (
  3. "html/template"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. images "golang-fave/engine/wrapper/resources/images"
  8. others "golang-fave/engine/wrapper/resources/others"
  9. scripts "golang-fave/engine/wrapper/resources/scripts"
  10. styles "golang-fave/engine/wrapper/resources/styles"
  11. templates "golang-fave/engine/wrapper/resources/templates"
  12. )
  13. type tmplDataErrorMsg struct {
  14. ErrorMessage string
  15. }
  16. func (this *Wrapper) staticResource() bool {
  17. if this.R.URL.Path == "/assets/sys/styles.css" {
  18. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  19. (*this.W).Header().Set("Content-Type", "text/css")
  20. (*this.W).Write(styles.File_assets_sys_styles_css)
  21. return true
  22. } else if this.R.URL.Path == "/assets/cp/styles.css" {
  23. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  24. (*this.W).Header().Set("Content-Type", "text/css")
  25. (*this.W).Write(styles.File_assets_cp_styles_css)
  26. return true
  27. } else if this.R.URL.Path == "/assets/sys/bootstrap.css" {
  28. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  29. (*this.W).Header().Set("Content-Type", "text/css")
  30. (*this.W).Write(styles.File_assets_sys_bootstrap_css)
  31. return true
  32. } else if this.R.URL.Path == "/assets/sys/jquery.js" {
  33. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  34. (*this.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
  35. (*this.W).Write(scripts.File_assets_sys_jquery_js)
  36. return true
  37. } else if this.R.URL.Path == "/assets/sys/popper.js" {
  38. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  39. (*this.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
  40. (*this.W).Write(scripts.File_assets_sys_popper_js)
  41. return true
  42. } else if this.R.URL.Path == "/assets/sys/bootstrap.js" {
  43. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  44. (*this.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
  45. (*this.W).Write(scripts.File_assets_sys_bootstrap_js)
  46. return true
  47. } else if this.R.URL.Path == "/assets/cp/scripts.js" {
  48. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  49. (*this.W).Header().Set("Content-Type", "application/javascript; charset=utf-8")
  50. (*this.W).Write(scripts.File_assets_cp_scripts_js)
  51. return true
  52. } else if this.R.URL.Path == "/assets/sys/logo.svg" {
  53. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  54. (*this.W).Header().Set("Content-Type", "image/svg+xml")
  55. (*this.W).Write(others.File_assets_sys_logo_svg)
  56. return true
  57. } else if this.R.URL.Path == "/assets/sys/bg.png" {
  58. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  59. (*this.W).Header().Set("Content-Type", "image/png")
  60. (*this.W).Write(images.File_assets_sys_bg_png)
  61. return true
  62. } else if this.R.URL.Path == "/assets/sys/logo.png" {
  63. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  64. (*this.W).Header().Set("Content-Type", "image/png")
  65. (*this.W).Write(images.File_assets_sys_logo_png)
  66. return true
  67. } else if this.R.URL.Path == "/assets/sys/fave.ico" {
  68. (*this.W).Header().Set("Cache-Control", "public, max-age=31536000")
  69. (*this.W).Header().Set("Content-Type", "image/x-icon")
  70. (*this.W).Write(others.File_assets_sys_fave_ico)
  71. return true
  72. }
  73. return false
  74. }
  75. func (this *Wrapper) staticFile() bool {
  76. file := this.R.URL.Path
  77. if file != "/" {
  78. f, err := os.Open(this.DirVHostHome + "/htdocs" + file)
  79. if err == nil {
  80. defer f.Close()
  81. st, err := os.Stat(this.DirVHostHome + "/htdocs" + file)
  82. if err != nil {
  83. return false
  84. }
  85. if st.Mode().IsDir() {
  86. if file[len(file)-1] == '/' {
  87. fi, err := os.Open(this.DirVHostHome + "/htdocs" + file + "/index.html")
  88. if err == nil {
  89. defer fi.Close()
  90. sti, err := os.Stat(this.DirVHostHome + "/htdocs" + file + "/index.html")
  91. if err != nil {
  92. return false
  93. }
  94. if sti.Mode().IsDir() {
  95. return false
  96. }
  97. http.ServeFile(*this.W, this.R, this.DirVHostHome+"/htdocs"+file+"/index.html")
  98. return true
  99. }
  100. }
  101. return false
  102. }
  103. http.ServeFile(*this.W, this.R, this.DirVHostHome+"/htdocs"+file)
  104. return true
  105. }
  106. } else {
  107. f, err := os.Open(this.DirVHostHome + "/htdocs/index.html")
  108. if err == nil {
  109. defer f.Close()
  110. st, err := os.Stat(this.DirVHostHome + "/htdocs/index.html")
  111. if err != nil {
  112. return false
  113. }
  114. if st.Mode().IsDir() {
  115. return false
  116. }
  117. http.ServeFile(*this.W, this.R, this.DirVHostHome+"/htdocs/index.html")
  118. return true
  119. }
  120. }
  121. return false
  122. }
  123. func (this *Wrapper) printPageDefault() {
  124. // Custom page
  125. f, err := os.Open(this.DirVHostHome + "/htdocs" + "/index.html")
  126. if err == nil {
  127. defer f.Close()
  128. http.ServeFile(*this.W, this.R, this.DirVHostHome+"/htdocs"+"/index.html")
  129. return
  130. }
  131. // Default page
  132. tmpl, err := template.New("template").Parse(string(templates.PageDefault))
  133. if err != nil {
  134. this.printTmplPageError(err)
  135. return
  136. }
  137. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  138. (*this.W).Header().Set("Content-Type", "text/html")
  139. tmpl.Execute(*this.W, TmplDataAll{
  140. System: this.TmplGetSystemData(),
  141. })
  142. }
  143. func (this *Wrapper) printPage404() {
  144. // Custom 404 error page
  145. f, err := ioutil.ReadFile(this.DirVHostHome + "/htdocs" + "/404.html")
  146. if err == nil {
  147. (*this.W).WriteHeader(http.StatusNotFound)
  148. (*this.W).Header().Set("Content-Type", "text/html")
  149. (*this.W).Write(f)
  150. return
  151. }
  152. // Default error page
  153. tmpl, err := template.New("template").Parse(string(templates.PageError404))
  154. if err != nil {
  155. this.printTmplPageError(err)
  156. return
  157. }
  158. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  159. (*this.W).WriteHeader(http.StatusNotFound)
  160. (*this.W).Header().Set("Content-Type", "text/html")
  161. tmpl.Execute(*this.W, TmplDataAll{
  162. System: this.TmplGetSystemData(),
  163. })
  164. }
  165. func (this *Wrapper) printTmplPageError(perr error) {
  166. tmpl, err := template.New("template").Parse(string(templates.PageTmplError))
  167. if err != nil {
  168. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  169. (*this.W).WriteHeader(http.StatusInternalServerError)
  170. (*this.W).Header().Set("Content-Type", "text/html")
  171. (*this.W).Write([]byte("<h1>Critical engine error!</h1>"))
  172. (*this.W).Write([]byte("<h2>" + perr.Error() + "</h2>"))
  173. return
  174. }
  175. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  176. (*this.W).WriteHeader(http.StatusInternalServerError)
  177. (*this.W).Header().Set("Content-Type", "text/html")
  178. tmpl.Execute(*this.W, TmplDataAll{
  179. System: this.TmplGetSystemData(),
  180. Data: tmplDataErrorMsg{
  181. ErrorMessage: perr.Error(),
  182. },
  183. })
  184. }
  185. func (this *Wrapper) PrintEnginePageError(perr error) {
  186. tmpl, err := template.New("template").Parse(string(templates.PageEngError))
  187. if err != nil {
  188. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  189. (*this.W).WriteHeader(http.StatusInternalServerError)
  190. (*this.W).Header().Set("Content-Type", "text/html")
  191. (*this.W).Write([]byte("<h1>Critical engine error!</h1>"))
  192. (*this.W).Write([]byte("<h2>" + perr.Error() + "</h2>"))
  193. return
  194. }
  195. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  196. (*this.W).WriteHeader(http.StatusInternalServerError)
  197. (*this.W).Header().Set("Content-Type", "text/html")
  198. tmpl.Execute(*this.W, TmplDataAll{
  199. System: this.TmplGetSystemData(),
  200. Data: tmplDataErrorMsg{
  201. ErrorMessage: perr.Error(),
  202. },
  203. })
  204. }