diff --git a/main.go b/main.go index 7932212..8086c63 100644 --- a/main.go +++ b/main.go @@ -20,9 +20,7 @@ import ( "time" ) -// -------------------------------------------------------------------- -// Configuration: HTTP client and buffer pool (memory tuned) -// -------------------------------------------------------------------- +// ---------- tuned HTTP client and buffer pool ---------- var httpClient = &http.Client{ Timeout: 15 * time.Second, Transport: &http.Transport{ @@ -39,7 +37,7 @@ var httpClient = &http.Client{ var copyBufPool = sync.Pool{ New: func() any { - b := make([]byte, 32*1024) // 32KB buffer reused + b := make([]byte, 32*1024) // 32KB return &b }, } @@ -47,9 +45,7 @@ var copyBufPool = sync.Pool{ const pinterestSearchURL = "https://www.pinterest.com/resource/BaseSearchResource/get/" const cookieName = "pinata_bm" -// -------------------------------------------------------------------- -// Bookmarks: types, key, limits -// -------------------------------------------------------------------- +// ---------- bookmarks types / config ---------- type BookmarkEntry struct { Type string `json:"type"` // "q" or "img" Value string `json:"value"` // query or image URL @@ -62,13 +58,9 @@ var disableReverse bool const maxBookmarks = 30 const maxItemLen = 256 -// -------------------------------------------------------------------- -// init: read env variables -// - PINATA_BOOKMARK_KEY: base64 32-byte key to enable bookmarks -// - PINATA_DISABLE_REVERSE: "1"/"true"/"yes" disables reverse search link -// -------------------------------------------------------------------- +// ---------- init: read env ---------- func init() { - // PINATA_BOOKMARK_KEY + // PINATA_BOOKMARK_KEY: base64 32-byte key if kb := os.Getenv("PINATA_BOOKMARK_KEY"); kb != "" { if decoded, err := base64.StdEncoding.DecodeString(kb); err == nil && len(decoded) == 32 { bookmarkKey = decoded @@ -76,14 +68,14 @@ func init() { log.Println("Bookmarking enabled") } else { bookmarkingEnabled = false - log.Println("PINATA_BOOKMARK_KEY provided but invalid; bookmarking disabled") + log.Println("PINATA_BOOKMARK_KEY present but invalid; bookmarking disabled") } } else { bookmarkingEnabled = false log.Println("PINATA_BOOKMARK_KEY not set; bookmarking disabled") } - // PINATA_DISABLE_REVERSE + // PINATA_DISABLE_REVERSE: "1"/"true"/"yes" disables reverse search switch strings.ToLower(strings.TrimSpace(os.Getenv("PINATA_DISABLE_REVERSE"))) { case "1", "true", "yes": disableReverse = true @@ -93,9 +85,7 @@ func init() { } } -// -------------------------------------------------------------------- -// Encryption helpers for cookie storage (AES-GCM) -// -------------------------------------------------------------------- +// ---------- encryption helpers (AES-GCM) ---------- func encryptBookmarks(entries []BookmarkEntry) (string, error) { if !bookmarkingEnabled { return "", nil @@ -146,12 +136,12 @@ func decryptBookmarks(encoded string) ([]BookmarkEntry, error) { if err != nil { return nil, err } - // Try new format ([]BookmarkEntry) + // try new format first ([]BookmarkEntry) var entries []BookmarkEntry if err := json.Unmarshal(plain, &entries); err == nil { return entries, nil } - // Fallback to legacy []string -> convert to type "q" + // fallback to legacy []string var arr []string if err := json.Unmarshal(plain, &arr); err == nil { out := make([]BookmarkEntry, 0, len(arr)) @@ -163,9 +153,7 @@ func decryptBookmarks(encoded string) ([]BookmarkEntry, error) { return nil, io.ErrUnexpectedEOF } -// -------------------------------------------------------------------- -// Cookie helpers: read, set, clear -// -------------------------------------------------------------------- +// ---------- cookie helpers ---------- func readBookmarksFromReq(r *http.Request) []BookmarkEntry { if !bookmarkingEnabled { return nil @@ -176,7 +164,6 @@ func readBookmarksFromReq(r *http.Request) []BookmarkEntry { } entries, err := decryptBookmarks(c.Value) if err != nil { - // invalid cookie -> ignore return nil } return entries @@ -196,14 +183,14 @@ func setBookmarksCookie(w http.ResponseWriter, entries []BookmarkEntry) { if len(v) > maxItemLen { v = v[:maxItemLen] } + if e.Type != "q" && e.Type != "img" { + e.Type = "q" + } key := e.Type + "|" + v if seen[key] { continue } seen[key] = true - if e.Type != "q" && e.Type != "img" { - e.Type = "q" - } out = append(out, BookmarkEntry{Type: e.Type, Value: v}) if len(out) >= maxBookmarks { break @@ -211,7 +198,6 @@ func setBookmarksCookie(w http.ResponseWriter, entries []BookmarkEntry) { } enc, err := encryptBookmarks(out) if err != nil { - // fail silently return } c := &http.Cookie{ @@ -220,7 +206,7 @@ func setBookmarksCookie(w http.ResponseWriter, entries []BookmarkEntry) { Path: "/", HttpOnly: true, SameSite: http.SameSiteLaxMode, - // Secure: true, // enable when serving over HTTPS + // Secure: true, // enable in production with HTTPS MaxAge: 60 * 60 * 24 * 365 * 10, } http.SetCookie(w, c) @@ -237,9 +223,7 @@ func clearBookmarksCookie(w http.ResponseWriter) { http.SetCookie(w, c) } -// -------------------------------------------------------------------- -// Stylesheet and minimal HTML templates (no JS) -// -------------------------------------------------------------------- +// ---------- CSS and HTML (no JS) ---------- const cssContent = ` :root{--bg:#0b0f17;--muted:#94a3b8;--text:#e6e6ff;--accent:#7c3aed;--card-shadow:rgba(0,0,0,0.6)} *{box-sizing:border-box}html,body{height:100%}body{margin:0;padding:20px;background:linear-gradient(180deg,#071020 0%,#0b0f17 100%);color:var(--text);font-family:ui-monospace,Menlo,Monaco,monospace} @@ -270,52 +254,19 @@ button[type="submit"],.btn-save{background:linear-gradient(90deg,var(--accent),# @media (max-width:640px){ body{padding:12px;font-size:18px} .brand{font-size:22px} input[type="text"]{min-width:120px;padding:12px 14px;font-size:16px} button[type="submit"],.btn-save{padding:10px 14px;font-size:16px;border-radius:10px} .img-container{column-width:180px;column-gap:12px} .search-block{gap:10px;flex-direction:column} .search-inline{width:100%} .search-box{margin-left:0;width:100%} .bookmarks{order:3;width:100%;margin-top:8px} } ` -<<<<<<< HEAD -// Static index page (no JS). Bookmarks rendered server-side only here. -func indexHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html; charset=utf8") - // header and intro - io.WriteString(w, `