helpers.go 7.7 KB

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