wrapper.go 5.2 KB

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