wrapper.go 5.8 KB

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