helpers.go 7.6 KB

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