Files
cpdaily_xiaozhushou/backed/internal/service/cpdaily/cookiejar.go
T
becomer 2e2c161770 feat: init cpdaily-xiaozhushou with blue-green deploy
- backed/: Go Fiber backend with cpdaily integration
- frondend/: Vue 3 + Vite frontend
- Dockerfile: multi-stage build (Node → Go → Alpine)
- deploy/docker-compose.yml: production compose file
- .woodpecker/build.yml: CI/CD with blue-green zero-downtime deploy
- /health endpoint for deployment health checks
- Port 8884/8885 blue-green swap via 1Panel OpenResty
2026-07-03 15:37:55 +08:00

70 lines
1.4 KiB
Go

package cpdaily
import (
"net/http"
"net/url"
"sync"
)
// simpleCookieJar 简化的 CookieJar 实现
type simpleCookieJar struct {
mu sync.RWMutex
cookies map[string][]*http.Cookie // key: host
}
func newCookieJar() *simpleCookieJar {
return &simpleCookieJar{
cookies: make(map[string][]*http.Cookie),
}
}
func (j *simpleCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
j.mu.Lock()
defer j.mu.Unlock()
host := u.Host
existing := j.cookies[host]
cookieMap := make(map[string]*http.Cookie)
for _, c := range existing {
cookieMap[c.Name] = c
}
for _, c := range cookies {
cookieMap[c.Name] = c
}
var merged []*http.Cookie
for _, c := range cookieMap {
merged = append(merged, c)
}
j.cookies[host] = merged
}
func (j *simpleCookieJar) Cookies(u *url.URL) []*http.Cookie {
j.mu.RLock()
defer j.mu.RUnlock()
host := u.Host
var result []*http.Cookie
result = append(result, j.cookies[host]...)
// 也返回全局 cookie
result = append(result, j.cookies[""]...)
return result
}
// AllCookies 返回所有域名下的所有 cookie
func (j *simpleCookieJar) AllCookies() []*http.Cookie {
j.mu.RLock()
defer j.mu.RUnlock()
var result []*http.Cookie
seen := make(map[string]bool)
for _, cookies := range j.cookies {
for _, c := range cookies {
if !seen[c.Name] {
result = append(result, c)
seen[c.Name] = true
}
}
}
return result
}