wrapper.go 5.5 KB

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