wrapper.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package wrapper
  2. import (
  3. "html/template"
  4. "log"
  5. "net"
  6. "net/http"
  7. "os"
  8. "strings"
  9. "golang-fave/constants"
  10. "golang-fave/engine/sessions"
  11. templates "golang-fave/engine/wrapper/resources/templates"
  12. )
  13. type handleRun func(wrapper *Wrapper) bool
  14. type tmplDataSystem struct {
  15. PathIcoFav string
  16. PathSvgLogo string
  17. PathCssStyles string
  18. PathCssCpStyles string
  19. PathCssBootstrap string
  20. PathJsJquery string
  21. PathJsPopper string
  22. PathJsBootstrap string
  23. PathJsCpScripts string
  24. BlockModalSysMsg template.HTML
  25. }
  26. type tmplDataAll struct {
  27. System tmplDataSystem
  28. Data interface{}
  29. }
  30. type Wrapper struct {
  31. W *http.ResponseWriter
  32. R *http.Request
  33. VHost string
  34. Port string
  35. DirWww string
  36. DirVHostHome string
  37. RemoteIp string
  38. LoggerAcc *log.Logger
  39. LoggerErr *log.Logger
  40. Session *sessions.Session
  41. }
  42. func (this *Wrapper) tmplGetSystemData() tmplDataSystem {
  43. return tmplDataSystem{
  44. PathIcoFav: this.R.URL.Scheme + "://" + this.R.Host + "/assets/sys/fave.ico?v=" + constants.AssetsVersion,
  45. PathSvgLogo: this.R.URL.Scheme + "://" + this.R.Host + "/assets/sys/logo.svg?v=" + constants.AssetsVersion,
  46. PathCssStyles: this.R.URL.Scheme + "://" + this.R.Host + "/assets/sys/styles.css?v=" + constants.AssetsVersion,
  47. PathCssCpStyles: this.R.URL.Scheme + "://" + this.R.Host + "/assets/cp/styles.css?v=" + constants.AssetsVersion,
  48. PathCssBootstrap: this.R.URL.Scheme + "://" + this.R.Host + "/assets/sys/bootstrap.css?v=" + constants.AssetsVersion,
  49. PathJsJquery: this.R.URL.Scheme + "://" + this.R.Host + "/assets/sys/jquery.js?v=" + constants.AssetsVersion,
  50. PathJsPopper: this.R.URL.Scheme + "://" + this.R.Host + "/assets/sys/popper.js?v=" + constants.AssetsVersion,
  51. PathJsBootstrap: this.R.URL.Scheme + "://" + this.R.Host + "/assets/sys/bootstrap.js?v=" + constants.AssetsVersion,
  52. PathJsCpScripts: this.R.URL.Scheme + "://" + this.R.Host + "/assets/cp/scripts.js?v=" + constants.AssetsVersion,
  53. BlockModalSysMsg: template.HTML(templates.BlockModalSysMsg),
  54. }
  55. }
  56. func New(w *http.ResponseWriter, r *http.Request, vhost string, port string, wwwdir string, vhosthome string) *Wrapper {
  57. return &Wrapper{
  58. VHost: vhost,
  59. Port: port,
  60. DirWww: wwwdir,
  61. DirVHostHome: vhosthome,
  62. W: w,
  63. R: r,
  64. }
  65. }
  66. func (this *Wrapper) Run(hRun handleRun) {
  67. // Populate some values
  68. this.RemoteIp = this.R.RemoteAddr
  69. // Create loggers
  70. this.LoggerAcc = log.New(os.Stdout, this.VHost+", ", log.LstdFlags)
  71. this.LoggerErr = log.New(os.Stdout, this.VHost+", ", log.LstdFlags)
  72. // Attach file for access log
  73. if !constants.Debug {
  74. acclogfile, acclogfileerr := os.OpenFile(this.DirVHostHome+"/logs/access.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  75. if acclogfileerr == nil {
  76. defer acclogfile.Close()
  77. this.LoggerAcc.SetOutput(acclogfile)
  78. }
  79. }
  80. // Attach file for access log
  81. if !constants.Debug {
  82. errlogfile, errlogfileerr := os.OpenFile(this.DirVHostHome+"/logs/error.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  83. if errlogfileerr == nil {
  84. defer errlogfile.Close()
  85. this.LoggerErr.SetOutput(errlogfile)
  86. }
  87. }
  88. // Fix remote IP
  89. if strings.ContainsRune(this.R.RemoteAddr, ':') {
  90. this.RemoteIp, _, _ = net.SplitHostPort(this.R.RemoteAddr)
  91. }
  92. // Redirect to main domain
  93. if this.redirectToMainDomain() {
  94. this.Log("301")
  95. return
  96. }
  97. // Static resource
  98. if this.staticResource() {
  99. this.Log("200")
  100. return
  101. }
  102. // Static file
  103. if this.staticFile() {
  104. this.Log("200")
  105. return
  106. }
  107. // Friendly search engine url
  108. if this.redirectSeoFix() {
  109. this.Log("301")
  110. return
  111. }
  112. // Create and load session
  113. this.Session = sessions.New(this.W, this.R, this.VHost, this.DirVHostHome, this.RemoteIp)
  114. this.Session.Load()
  115. // Set session vars
  116. if !this.Session.IsSetInt("UserId") {
  117. this.Session.SetInt("UserId", 0)
  118. }
  119. // Logic
  120. if hRun != nil {
  121. if hRun(this) {
  122. this.Log("200")
  123. this.Session.Save()
  124. return
  125. }
  126. }
  127. // Show default page
  128. if this.R.URL.Path == "/" {
  129. this.Log("200")
  130. this.printPageDefault()
  131. } else {
  132. this.LogError("404")
  133. this.printPage404()
  134. }
  135. }
  136. func (this *Wrapper) Log(value string) {
  137. this.LoggerAcc.Println("[ACC] [" + this.R.Method + "] [" + value + "] [" + this.RemoteIp +
  138. "] [" + this.R.URL.Scheme + "://" + this.R.Host + this.R.URL.RequestURI() +
  139. "] [" + this.R.Header.Get("User-Agent") + "]")
  140. }
  141. func (this *Wrapper) LogError(value string) {
  142. this.LoggerErr.Println("[ERR] [" + this.R.Method + "] [" + value + "] [" + this.RemoteIp +
  143. "] [" + this.R.URL.Scheme + "://" + this.R.Host + this.R.URL.RequestURI() +
  144. "] [" + this.R.Header.Get("User-Agent") + "]")
  145. }
  146. func (this *Wrapper) TmplFrontEnd(tname string, data interface{}) bool {
  147. tmpl, err := template.ParseFiles(
  148. this.DirVHostHome+"/template"+"/"+tname+".html",
  149. this.DirVHostHome+"/template"+"/header.html",
  150. this.DirVHostHome+"/template"+"/sidebar.html",
  151. this.DirVHostHome+"/template"+"/footer.html",
  152. )
  153. if err != nil {
  154. this.printTmplPageError(err)
  155. return true
  156. }
  157. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  158. tmpl.Execute(*this.W, tmplDataAll{
  159. System: this.tmplGetSystemData(),
  160. Data: data,
  161. })
  162. return true
  163. }
  164. func (this *Wrapper) TmplBackEnd(tcont []byte, data interface{}) bool {
  165. tmpl, err := template.New("template").Parse(string(tcont))
  166. if err != nil {
  167. this.printTmplPageError(err)
  168. return true
  169. }
  170. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  171. tmpl.Execute(*this.W, tmplDataAll{
  172. System: this.tmplGetSystemData(),
  173. Data: data,
  174. })
  175. return true
  176. }
  177. func (this *Wrapper) EngineErrMsgOnError(err error) bool {
  178. if err != nil {
  179. this.printEnginePageError(err)
  180. return true
  181. }
  182. return false
  183. }