wrapper.go 4.8 KB

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