helpers.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package helpers
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "os"
  8. "regexp"
  9. "runtime"
  10. "strconv"
  11. "time"
  12. "github.com/vladimirok5959/golang-server-sessions/session"
  13. )
  14. var mHtml = regexp.MustCompile(`>[\n\t\r\s]+<`)
  15. var mHtmlLeft = regexp.MustCompile(`>[\n\t\r\s]+`)
  16. var mHtmlRight = regexp.MustCompile(`[\n\t\r\s]+<`)
  17. func MinifyHtmlCode(str string) string {
  18. str = mHtml.ReplaceAllString(str, "><")
  19. str = mHtmlLeft.ReplaceAllString(str, ">")
  20. str = mHtmlRight.ReplaceAllString(str, "<")
  21. return str
  22. }
  23. // Example:
  24. //
  25. // sess := helpers.SessionStart(w, r)
  26. //
  27. // defer sess.Close()
  28. func SessionStart(w http.ResponseWriter, r *http.Request) (*session.Session, error) {
  29. sess, err := session.New(w, r, "/tmp")
  30. if err != nil && !errors.Is(err, os.ErrNotExist) {
  31. log.Printf("%s\n", err.Error())
  32. }
  33. return sess, err
  34. }
  35. func RespondAsBadRequest(w http.ResponseWriter, r *http.Request, err error) {
  36. if err != nil {
  37. log.Printf("%s\n", err.Error())
  38. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  39. w.Header().Set("Content-Type", "application/json")
  40. w.WriteHeader(http.StatusBadRequest)
  41. if _, e := w.Write([]byte(`{"error":"` + strconv.Quote(err.Error()) + `"}`)); e != nil {
  42. log.Printf("%s\n", e.Error())
  43. }
  44. } else {
  45. w.WriteHeader(http.StatusBadRequest)
  46. }
  47. }
  48. func RespondAsMethodNotAllowed(w http.ResponseWriter, r *http.Request) {
  49. w.WriteHeader(http.StatusMethodNotAllowed)
  50. }
  51. func SetLanguageCookie(w http.ResponseWriter, r *http.Request) error {
  52. var clang string
  53. if c, err := r.Cookie("lang"); err == nil {
  54. clang = c.Value
  55. }
  56. // if err := r.ParseForm(); err != nil {
  57. // RespondAsBadRequest(w, r, err)
  58. // return err
  59. // }
  60. lang := r.Form.Get("lang")
  61. if lang != "" && lang != clang {
  62. http.SetCookie(w, &http.Cookie{
  63. Name: "lang",
  64. Value: lang,
  65. Expires: time.Now().Add(365 * 24 * time.Hour),
  66. HttpOnly: true,
  67. })
  68. }
  69. return nil
  70. }
  71. func HandlerApplicationStatus() http.Handler {
  72. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  73. if r.Method != http.MethodGet {
  74. RespondAsMethodNotAllowed(w, r)
  75. return
  76. }
  77. var m runtime.MemStats
  78. runtime.ReadMemStats(&m)
  79. memory := fmt.Sprintf(
  80. `{"alloc":"%v","total_alloc":"%v","sys":"%v","num_gc":"%v"}`,
  81. m.Alloc, m.TotalAlloc, m.Sys, m.NumGC,
  82. )
  83. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  84. w.Header().Set("Content-Type", "application/json")
  85. if _, err := w.Write([]byte(fmt.Sprintf(`{"routines":%d,"memory":%s}`, runtime.NumGoroutine(), memory))); err != nil {
  86. log.Printf("%s\n", err.Error())
  87. }
  88. })
  89. }