wrapper.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. if !this.Session.IsSetBool("IsLogged") {
  120. this.Session.SetBool("IsLogged", false)
  121. }
  122. // Logic
  123. if hRun != nil {
  124. if hRun(this) {
  125. this.Log("200")
  126. this.Session.Save()
  127. return
  128. }
  129. }
  130. // Show default page
  131. if this.R.URL.Path == "/" {
  132. this.Log("200")
  133. this.printPageDefault()
  134. } else {
  135. this.LogError("404")
  136. this.printPage404()
  137. }
  138. }
  139. func (this *Wrapper) Log(value string) {
  140. this.LoggerAcc.Println("[ACC] [" + this.R.Method + "] [" + value + "] [" + this.RemoteIp +
  141. "] [" + this.R.URL.Scheme + "://" + this.R.Host + this.R.URL.RequestURI() +
  142. "] [" + this.R.Header.Get("User-Agent") + "]")
  143. }
  144. func (this *Wrapper) LogError(value string) {
  145. this.LoggerErr.Println("[ERR] [" + this.R.Method + "] [" + value + "] [" + this.RemoteIp +
  146. "] [" + this.R.URL.Scheme + "://" + this.R.Host + this.R.URL.RequestURI() +
  147. "] [" + this.R.Header.Get("User-Agent") + "]")
  148. }
  149. func (this *Wrapper) TmplFrontEnd(tname string, data interface{}) bool {
  150. tmpl, err := template.ParseFiles(
  151. this.DirVHostHome+"/template"+"/"+tname+".html",
  152. this.DirVHostHome+"/template"+"/header.html",
  153. this.DirVHostHome+"/template"+"/sidebar.html",
  154. this.DirVHostHome+"/template"+"/footer.html",
  155. )
  156. if err != nil {
  157. this.printTmplPageError(err)
  158. return true
  159. }
  160. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  161. tmpl.Execute(*this.W, tmplDataAll{
  162. System: this.tmplGetSystemData(),
  163. Data: data,
  164. })
  165. return true
  166. }
  167. func (this *Wrapper) TmplBackEnd(tcont []byte, data interface{}) bool {
  168. tmpl, err := template.New("template").Parse(string(tcont))
  169. if err != nil {
  170. this.printTmplPageError(err)
  171. return true
  172. }
  173. (*this.W).Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  174. tmpl.Execute(*this.W, tmplDataAll{
  175. System: this.tmplGetSystemData(),
  176. Data: data,
  177. })
  178. return true
  179. }
  180. func (this *Wrapper) EngineErrMsgOnError(err error) bool {
  181. if err != nil {
  182. this.printEnginePageError(err)
  183. return true
  184. }
  185. return false
  186. }