wrapper.go 5.6 KB

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