cookie.go 707 B

123456789101112131415161718192021222324252627282930313233343536
  1. package tools
  2. import (
  3. "net/http"
  4. "strings"
  5. )
  6. func SetCookie(name string, value string, w *http.ResponseWriter) {
  7. cookie := http.Cookie{
  8. Name: name,
  9. Value: value,
  10. }
  11. http.SetCookie(*w, &cookie)
  12. }
  13. func GetCookie(r *http.Request, name string) string {
  14. cookies := r.Cookies()
  15. for _, cookie := range cookies {
  16. if cookie.Name == name {
  17. return cookie.Value
  18. }
  19. }
  20. return ""
  21. }
  22. func GetMailServerFromCookie(r *http.Request) *MailServer {
  23. auth := GetCookie(r, "auth")
  24. if !strings.Contains(auth, "|") {
  25. return nil
  26. }
  27. authStrings := strings.Split(auth, "|")
  28. mailServer := &MailServer{
  29. Server: authStrings[0],
  30. Email: authStrings[1],
  31. Password: authStrings[2],
  32. }
  33. return mailServer
  34. }