wrapper.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. )
  12. const C_AssetsVersion = "2"
  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. }
  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. Action *actions.Action
  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. }
  56. }
  57. func New(w *http.ResponseWriter, r *http.Request, vhost string, port string, wwwdir string, vhosthome string, debug bool) *Wrapper {
  58. return &Wrapper{
  59. VHost: vhost,
  60. Port: port,
  61. DirWww: wwwdir,
  62. DirVhostHome: vhosthome,
  63. W: w,
  64. R: r,
  65. Debug: debug,
  66. }
  67. }
  68. func (e *Wrapper) Run(hRun handleRun) {
  69. // Populate some values
  70. e.RemoteIp = e.R.RemoteAddr
  71. // Create loggers
  72. e.LoggerAcc = log.New(os.Stdout, e.VHost+", ", log.LstdFlags)
  73. e.LoggerErr = log.New(os.Stdout, e.VHost+", ", log.LstdFlags)
  74. // Attach file for access log
  75. if !e.Debug {
  76. acclogfile, acclogfileerr := os.OpenFile(e.DirVhostHome+"/logs/access.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  77. if acclogfileerr == nil {
  78. defer acclogfile.Close()
  79. e.LoggerAcc.SetOutput(acclogfile)
  80. }
  81. }
  82. // Attach file for access log
  83. if !e.Debug {
  84. errlogfile, errlogfileerr := os.OpenFile(e.DirVhostHome+"/logs/error.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  85. if errlogfileerr == nil {
  86. defer errlogfile.Close()
  87. e.LoggerErr.SetOutput(errlogfile)
  88. }
  89. }
  90. // Fix remote IP
  91. if strings.ContainsRune(e.R.RemoteAddr, ':') {
  92. e.RemoteIp, _, _ = net.SplitHostPort(e.R.RemoteAddr)
  93. }
  94. // Redirect to main domain
  95. if e.redirectToMainDomain() {
  96. e.Log("301")
  97. return
  98. }
  99. // Static resource
  100. if e.staticResource() {
  101. e.Log("200")
  102. return
  103. }
  104. // Static file
  105. if e.staticFile() {
  106. e.Log("200")
  107. return
  108. }
  109. // Friendly search engine url
  110. if e.redirectSeoFix() {
  111. e.Log("301")
  112. return
  113. }
  114. // Create and load session
  115. e.Session = sessions.New(e.W, e.R, e.VHost, e.DirVhostHome, e.RemoteIp)
  116. e.Session.Load()
  117. // Set session vars
  118. if !e.Session.IsSetInt("UserId") {
  119. e.Session.SetInt("UserId", 0)
  120. }
  121. if !e.Session.IsSetBool("IsLogged") {
  122. e.Session.SetBool("IsLogged", false)
  123. }
  124. // Create action
  125. e.Action = actions.New(e.W, e.R, e.VHost, e.DirVhostHome, e.RemoteIp)
  126. // Call action
  127. if e.Action.Call() {
  128. e.Session.Save()
  129. return
  130. }
  131. // Logic
  132. if hRun != nil {
  133. if hRun(e) {
  134. e.Session.Save()
  135. return
  136. }
  137. }
  138. // Show default page
  139. if e.R.URL.Path == "/" {
  140. e.Log("200")
  141. e.printPageDefault()
  142. } else {
  143. e.LogError("404")
  144. e.printPage404()
  145. }
  146. }
  147. func (e *Wrapper) Log(value string) {
  148. e.LoggerAcc.Println("[ACC] [" + 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) LogError(value string) {
  153. e.LoggerErr.Println("[ERR] [" + 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) TmplFrontEnd(tname string, data interface{}) bool {
  158. tmpl, err := template.ParseFiles(
  159. e.DirVhostHome+"/template"+"/"+tname+".html",
  160. e.DirVhostHome+"/template"+"/header.html",
  161. e.DirVhostHome+"/template"+"/sidebar.html",
  162. e.DirVhostHome+"/template"+"/footer.html",
  163. )
  164. if err != nil {
  165. e.printTmplPageError(err)
  166. return true
  167. }
  168. tmpl.Execute(*e.W, tmplDataAll{
  169. System: e.tmplGetSystemData(),
  170. Data: data,
  171. })
  172. return true
  173. }
  174. func (e *Wrapper) TmplBackEnd(tcont []byte, data interface{}) bool {
  175. tmpl, err := template.New("template").Parse(string(tcont))
  176. if err != nil {
  177. e.printTmplPageError(err)
  178. return true
  179. }
  180. tmpl.Execute(*e.W, tmplDataAll{
  181. System: e.tmplGetSystemData(),
  182. Data: data,
  183. })
  184. return true
  185. }