helpers.go 7.7 KB

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