helpers.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package helpers
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "errors"
  7. "log"
  8. "net/http"
  9. "os"
  10. "regexp"
  11. "runtime"
  12. "strings"
  13. "time"
  14. "github.com/vladimirok5959/golang-server-sessions/session"
  15. )
  16. // func ClientIP(r *http.Request) string
  17. // func ClientIPs(r *http.Request) []string
  18. // func HandleAppStatus() http.Handler
  19. // func HandleFile(data, contentType string) http.Handler
  20. // func HandleImageJpeg(data string) http.Handler
  21. // func HandleImagePng(data string) http.Handler
  22. // func HandleTextCss(data string) http.Handler
  23. // func HandleTextJavaScript(data string) http.Handler
  24. // func HandleTextPlain(data string) http.Handler
  25. // func HandleTextXml(data string) http.Handler
  26. // func Md5Hash(str []byte) string
  27. // func MinifyHtmlCode(str string) string
  28. // func MinifyHtmlJsCode(str string) string
  29. // func RespondAsBadRequest(w http.ResponseWriter, r *http.Request, err error)
  30. // func RespondAsInternalServerError(w http.ResponseWriter, r *http.Request)
  31. // func RespondAsMethodNotAllowed(w http.ResponseWriter, r *http.Request)
  32. // func SessionStart(w http.ResponseWriter, r *http.Request) (*session.Session, error)
  33. // func SetLanguageCookie(w http.ResponseWriter, r *http.Request) error
  34. var mHtml = regexp.MustCompile(`>[\n\t\r]+<`)
  35. var mHtmlLeft = regexp.MustCompile(`>[\n\t\r]+`)
  36. var mHtmlRight = regexp.MustCompile(`[\n\t\r]+<`)
  37. var mScript = regexp.MustCompile(`<script>([^<]*)</script>`)
  38. var mScriptCommentsInline = regexp.MustCompile(`//.*\n`)
  39. var mScriptCommentsMultiline = regexp.MustCompile(`/\*[^*]*\*/`)
  40. var mScriptLine = regexp.MustCompile(`[\n\t\r]+`)
  41. var mScriptEqual = regexp.MustCompile(`[\n\t\r\s]+=[\n\t\r\s]+`)
  42. var mScriptDots = regexp.MustCompile(`:[\n\t\r\s]+"`)
  43. var mScriptFuncs = regexp.MustCompile(`\)[\n\t\r\s]+{`)
  44. func ClientIP(r *http.Request) string {
  45. ips := ClientIPs(r)
  46. if len(ips) >= 1 {
  47. return ips[0]
  48. }
  49. return ""
  50. }
  51. func ClientIPs(r *http.Request) []string {
  52. ra := r.RemoteAddr
  53. if xff := strings.Trim(r.Header.Get("X-Forwarded-For"), " "); xff != "" {
  54. ra = strings.Join([]string{xff, ra}, ",")
  55. }
  56. res := []string{}
  57. ips := strings.Split(ra, ",")
  58. for _, ip := range ips {
  59. res = append(res, strings.Trim(ip, " "))
  60. }
  61. return res
  62. }
  63. func HandleAppStatus() http.Handler {
  64. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  65. if r.Method != http.MethodGet {
  66. RespondAsMethodNotAllowed(w, r)
  67. return
  68. }
  69. var m runtime.MemStats
  70. runtime.ReadMemStats(&m)
  71. type respMemory struct {
  72. Alloc uint64 `json:"alloc"`
  73. NumGC uint32 `json:"num_gc"`
  74. Sys uint64 `json:"sys"`
  75. TotalAlloc uint64 `json:"total_alloc"`
  76. }
  77. type respRoot struct {
  78. Memory respMemory `json:"memory"`
  79. Routines int `json:"routines"`
  80. }
  81. resp := respRoot{
  82. Memory: respMemory{
  83. Alloc: m.Alloc,
  84. NumGC: m.NumGC,
  85. Sys: m.Sys,
  86. TotalAlloc: m.TotalAlloc,
  87. },
  88. Routines: runtime.NumGoroutine(),
  89. }
  90. j, err := json.Marshal(resp)
  91. if err != nil {
  92. RespondAsBadRequest(w, r, err)
  93. return
  94. }
  95. w.Header().Set("Content-Type", "application/json")
  96. if _, err := w.Write(j); err != nil {
  97. log.Printf("%s\n", err.Error())
  98. }
  99. })
  100. }
  101. func HandleFile(data, contentType string) http.Handler {
  102. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  103. if r.Method != http.MethodGet {
  104. RespondAsMethodNotAllowed(w, r)
  105. return
  106. }
  107. w.Header().Set("Content-Type", contentType)
  108. if _, err := w.Write([]byte(data)); err != nil {
  109. log.Printf("%s\n", err.Error())
  110. }
  111. })
  112. }
  113. func HandleImageJpeg(data string) http.Handler {
  114. return HandleFile(data, "image/jpeg")
  115. }
  116. func HandleImagePng(data string) http.Handler {
  117. return HandleFile(data, "image/png")
  118. }
  119. func HandleTextCss(data string) http.Handler {
  120. return HandleFile(data, "text/css")
  121. }
  122. func HandleTextJavaScript(data string) http.Handler {
  123. return HandleFile(data, "text/javascript")
  124. }
  125. func HandleTextPlain(data string) http.Handler {
  126. return HandleFile(data, "text/plain")
  127. }
  128. func HandleTextXml(data string) http.Handler {
  129. return HandleFile(data, "text/xml")
  130. }
  131. func Md5Hash(str []byte) string {
  132. h := md5.New()
  133. if _, err := h.Write(str); err != nil {
  134. return ""
  135. }
  136. return hex.EncodeToString(h.Sum(nil))
  137. }
  138. func MinifyHtmlCode(str string) string {
  139. str = MinifyHtmlJsCode(str)
  140. str = mHtml.ReplaceAllString(str, "><")
  141. str = mHtmlLeft.ReplaceAllString(str, ">")
  142. str = mHtmlRight.ReplaceAllString(str, "<")
  143. return str
  144. }
  145. func MinifyHtmlJsCode(str string) string {
  146. return mScript.ReplaceAllStringFunc(str, func(str string) string {
  147. str = strings.TrimPrefix(str, "<script>")
  148. str = strings.TrimSuffix(str, "</script>")
  149. str = mScriptCommentsInline.ReplaceAllString(str, "")
  150. str = mScriptCommentsMultiline.ReplaceAllString(str, "")
  151. str = mScriptLine.ReplaceAllString(str, "")
  152. str = mScriptEqual.ReplaceAllString(str, "=")
  153. str = mScriptDots.ReplaceAllString(str, ":\"")
  154. str = mScriptFuncs.ReplaceAllString(str, "){")
  155. return `<script>` + str + `</script>`
  156. })
  157. }
  158. func RespondAsBadRequest(w http.ResponseWriter, r *http.Request, err error) {
  159. if err != nil {
  160. log.Printf("%s\n", err.Error())
  161. w.Header().Set("Content-Type", "application/json")
  162. w.WriteHeader(http.StatusBadRequest)
  163. type Resp struct {
  164. Error string `json:"error"`
  165. }
  166. resp := Resp{
  167. Error: err.Error(),
  168. }
  169. j, err := json.Marshal(resp)
  170. if err != nil {
  171. log.Printf("%s\n", err.Error())
  172. return
  173. }
  174. if _, err := w.Write(j); err != nil {
  175. log.Printf("%s\n", err.Error())
  176. }
  177. } else {
  178. w.WriteHeader(http.StatusBadRequest)
  179. }
  180. }
  181. func RespondAsInternalServerError(w http.ResponseWriter, r *http.Request) {
  182. w.WriteHeader(http.StatusInternalServerError)
  183. }
  184. func RespondAsMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
  185. w.WriteHeader(http.StatusMethodNotAllowed)
  186. }
  187. // Example:
  188. //
  189. // sess, err := helpers.SessionStart(w, r)
  190. //
  191. // if err != nil && !errors.Is(err, os.ErrNotExist) {
  192. //
  193. // helpers.RespondAsBadRequest(w, r, err)
  194. // return
  195. //
  196. // }
  197. //
  198. // defer sess.Close()
  199. func SessionStart(w http.ResponseWriter, r *http.Request) (*session.Session, error) {
  200. sess, err := session.New(w, r, "/tmp")
  201. if err != nil && !errors.Is(err, os.ErrNotExist) {
  202. log.Printf("%s\n", err.Error())
  203. }
  204. return sess, err
  205. }
  206. // Example:
  207. //
  208. // if err = r.ParseForm(); err != nil {
  209. // helpers.RespondAsBadRequest(w, r, err)
  210. // return
  211. // }
  212. //
  213. // if err = helpers.SetLanguageCookie(w, r); err != nil {
  214. // helpers.RespondAsBadRequest(w, r, err)
  215. // return
  216. // }
  217. func SetLanguageCookie(w http.ResponseWriter, r *http.Request) error {
  218. var clang string
  219. if c, err := r.Cookie("lang"); err == nil {
  220. clang = c.Value
  221. }
  222. lang := r.FormValue("lang")
  223. if lang != "" && lang != clang {
  224. http.SetCookie(w, &http.Cookie{
  225. Expires: time.Now().Add(365 * 24 * time.Hour),
  226. HttpOnly: true,
  227. Name: "lang",
  228. Path: "/",
  229. Value: lang,
  230. })
  231. }
  232. return nil
  233. }
  234. // -----------------------------------------------------------------------------
  235. // For funcs which write some data to http.ResponseWriter
  236. //
  237. // Example: w = NewFakeResponseWriter()
  238. //
  239. // w.Body, w.Headers, w.StatusCode
  240. type FakeResponseWriter struct {
  241. Body []byte
  242. Headers http.Header
  243. StatusCode int
  244. }
  245. func (w *FakeResponseWriter) Header() http.Header {
  246. return w.Headers
  247. }
  248. func (w *FakeResponseWriter) Write(b []byte) (int, error) {
  249. w.Body = append(w.Body, b...)
  250. return len(b), nil
  251. }
  252. func (w *FakeResponseWriter) WriteHeader(statusCode int) {
  253. w.StatusCode = statusCode
  254. }
  255. // Create fake http.ResponseWriter for using in tests
  256. func NewFakeResponseWriter() *FakeResponseWriter {
  257. return &FakeResponseWriter{
  258. Body: []byte{},
  259. Headers: http.Header{},
  260. StatusCode: http.StatusOK,
  261. }
  262. }