wrapper.go 4.4 KB

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