short url web page v1

This commit is contained in:
kenneth
2023-12-25 17:04:22 +08:00
parent f27023c2ac
commit c1f4731669
45 changed files with 1637 additions and 93 deletions

41
pkg/cookie/cookie.go Normal file
View File

@@ -0,0 +1,41 @@
package cookie
import (
"net/http"
"time"
)
const (
AuthorizeName = "authorize"
)
func NewCookie(name, value string, expired time.Time) *http.Cookie {
return &http.Cookie{
Name: name,
Value: value,
Path: "/",
Secure: false, // true->只能https站点操作
HttpOnly: true, // true->js不能捕获
Expires: expired,
}
}
func SetCookie(w http.ResponseWriter, name, value string, expired time.Time) {
cookie := NewCookie(name, value, expired)
http.SetCookie(w, cookie)
}
func ReadCookie(r *http.Request, name string) (string, error) {
cookie, err := r.Cookie(name)
if err != nil {
return "", err
}
return cookie.Value, nil
}
func DeleteCookie(w http.ResponseWriter, name string) {
cookie := NewCookie(name, "", time.Now().Add(time.Duration(-10)*time.Second))
cookie.MaxAge = -1
http.SetCookie(w, cookie)
}