res.go 661 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package srv
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. )
  7. type Res struct {
  8. w http.ResponseWriter
  9. r *http.Request
  10. }
  11. func (res *Res) Construct(w http.ResponseWriter, r *http.Request) {
  12. res.w = w
  13. res.r = r
  14. }
  15. func (res *Res) Send(txt string) {
  16. fmt.Fprint(res.w, txt)
  17. }
  18. func (res *Res) Redirect(url string) {
  19. http.Redirect(res.w, res.r, url, http.StatusTemporaryRedirect)
  20. }
  21. func (res *Res) Cookie(name string, val string) {
  22. cookie := &http.Cookie{
  23. Name: name,
  24. Value: val,
  25. Expires: time.Now().Add(365 * 24 * time.Hour),
  26. Path: "/",
  27. Secure: true,
  28. HttpOnly: true,
  29. SameSite: http.SameSiteStrictMode,
  30. }
  31. http.SetCookie(res.w, cookie)
  32. }