wrapper.go 5.6 KB

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