|
| 1 | +//// Copyright (C) 2026 Fluxer Contributors |
| 2 | +//// |
| 3 | +//// This file is part of Fluxer. |
| 4 | +//// |
| 5 | +//// Fluxer is free software: you can redistribute it and/or modify |
| 6 | +//// it under the terms of the GNU Affero General Public License as published by |
| 7 | +//// the Free Software Foundation, either version 3 of the License, or |
| 8 | +//// (at your option) any later version. |
| 9 | +//// |
| 10 | +//// Fluxer is distributed in the hope that it will be useful, |
| 11 | +//// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +//// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +//// GNU Affero General Public License for more details. |
| 14 | +//// |
| 15 | +//// You should have received a copy of the GNU Affero General Public License |
| 16 | +//// along with Fluxer. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +import gleam/erlang/process |
| 19 | +import gleam/http/request |
| 20 | +import gleam/httpc |
| 21 | +import gleam/option.{type Option} |
| 22 | +import wisp.{type Response} |
| 23 | + |
| 24 | +const product_hunt_url = "https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=1057558&theme=light" |
| 25 | + |
| 26 | +const stale_after_ms = 300_000 |
| 27 | + |
| 28 | +const receive_timeout_ms = 5000 |
| 29 | + |
| 30 | +const fetch_timeout_ms = 4500 |
| 31 | + |
| 32 | +pub opaque type Cache { |
| 33 | + Cache(subject: process.Subject(ServerMessage)) |
| 34 | +} |
| 35 | + |
| 36 | +type ServerMessage { |
| 37 | + Get(process.Subject(Option(String))) |
| 38 | + RefreshDone(fetched_at: Int, svg: Option(String)) |
| 39 | +} |
| 40 | + |
| 41 | +type CacheEntry { |
| 42 | + CacheEntry(svg: String, fetched_at: Int) |
| 43 | +} |
| 44 | + |
| 45 | +type State { |
| 46 | + State(cache: Option(CacheEntry), is_refreshing: Bool) |
| 47 | +} |
| 48 | + |
| 49 | +pub fn start_cache() -> Cache { |
| 50 | + let started = process.new_subject() |
| 51 | + let _ = process.spawn_unlinked(fn() { run(started) }) |
| 52 | + |
| 53 | + let assert Ok(subject) = process.receive(started, within: 1000) |
| 54 | + Cache(subject: subject) |
| 55 | +} |
| 56 | + |
| 57 | +fn run(started: process.Subject(process.Subject(ServerMessage))) { |
| 58 | + let subject = process.new_subject() |
| 59 | + process.send(started, subject) |
| 60 | + |
| 61 | + let initial = State(cache: option.None, is_refreshing: False) |
| 62 | + loop(subject, initial) |
| 63 | +} |
| 64 | + |
| 65 | +fn loop(subject: process.Subject(ServerMessage), state: State) { |
| 66 | + let new_state = case process.receive(subject, within: stale_after_ms) { |
| 67 | + Ok(Get(reply_to)) -> handle_get(subject, reply_to, state) |
| 68 | + |
| 69 | + Ok(RefreshDone(fetched_at, svg)) -> |
| 70 | + handle_refresh_done(fetched_at, svg, state) |
| 71 | + |
| 72 | + Error(_) -> maybe_refresh_in_background(subject, state) |
| 73 | + } |
| 74 | + |
| 75 | + loop(subject, new_state) |
| 76 | +} |
| 77 | + |
| 78 | +fn handle_get( |
| 79 | + subject: process.Subject(ServerMessage), |
| 80 | + reply_to: process.Subject(Option(String)), |
| 81 | + state: State, |
| 82 | +) -> State { |
| 83 | + let now = monotonic_time_ms() |
| 84 | + |
| 85 | + case state.cache { |
| 86 | + option.None -> { |
| 87 | + let svg = fetch_badge_svg() |
| 88 | + process.send(reply_to, svg) |
| 89 | + |
| 90 | + let new_cache = case svg { |
| 91 | + option.Some(content) -> |
| 92 | + option.Some(CacheEntry(svg: content, fetched_at: now)) |
| 93 | + option.None -> option.None |
| 94 | + } |
| 95 | + |
| 96 | + State(cache: new_cache, is_refreshing: False) |
| 97 | + } |
| 98 | + |
| 99 | + option.Some(entry) -> { |
| 100 | + let is_stale = now - entry.fetched_at > stale_after_ms |
| 101 | + |
| 102 | + process.send(reply_to, option.Some(entry.svg)) |
| 103 | + |
| 104 | + case is_stale && !state.is_refreshing { |
| 105 | + True -> { |
| 106 | + spawn_refresh(subject) |
| 107 | + State(..state, is_refreshing: True) |
| 108 | + } |
| 109 | + False -> state |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +fn handle_refresh_done( |
| 116 | + fetched_at: Int, |
| 117 | + svg: Option(String), |
| 118 | + state: State, |
| 119 | +) -> State { |
| 120 | + let new_cache = case svg { |
| 121 | + option.Some(content) -> |
| 122 | + option.Some(CacheEntry(svg: content, fetched_at: fetched_at)) |
| 123 | + option.None -> state.cache |
| 124 | + } |
| 125 | + |
| 126 | + State(cache: new_cache, is_refreshing: False) |
| 127 | +} |
| 128 | + |
| 129 | +fn maybe_refresh_in_background( |
| 130 | + subject: process.Subject(ServerMessage), |
| 131 | + state: State, |
| 132 | +) -> State { |
| 133 | + let now = monotonic_time_ms() |
| 134 | + |
| 135 | + case state.cache, state.is_refreshing { |
| 136 | + option.Some(entry), False if now - entry.fetched_at > stale_after_ms -> { |
| 137 | + spawn_refresh(subject) |
| 138 | + State(..state, is_refreshing: True) |
| 139 | + } |
| 140 | + |
| 141 | + _, _ -> state |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +fn spawn_refresh(subject: process.Subject(ServerMessage)) { |
| 146 | + let _ = |
| 147 | + process.spawn_unlinked(fn() { |
| 148 | + let fetched_at = monotonic_time_ms() |
| 149 | + let svg = fetch_badge_svg() |
| 150 | + process.send(subject, RefreshDone(fetched_at, svg)) |
| 151 | + }) |
| 152 | + |
| 153 | + Nil |
| 154 | +} |
| 155 | + |
| 156 | +pub fn get_badge(cache: Cache) -> Option(String) { |
| 157 | + let reply_to = process.new_subject() |
| 158 | + process.send(cache.subject, Get(reply_to)) |
| 159 | + |
| 160 | + case process.receive(reply_to, within: receive_timeout_ms) { |
| 161 | + Ok(svg) -> svg |
| 162 | + Error(_) -> option.None |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +pub fn product_hunt(cache: Cache) -> Response { |
| 167 | + case get_badge(cache) { |
| 168 | + option.Some(content) -> { |
| 169 | + wisp.response(200) |
| 170 | + |> wisp.set_header("content-type", "image/svg+xml") |
| 171 | + |> wisp.set_header( |
| 172 | + "cache-control", |
| 173 | + "public, max-age=300, stale-while-revalidate=600", |
| 174 | + ) |
| 175 | + |> wisp.set_header("vary", "Accept") |
| 176 | + |> wisp.string_body(content) |
| 177 | + } |
| 178 | + |
| 179 | + option.None -> { |
| 180 | + wisp.response(503) |
| 181 | + |> wisp.set_header("content-type", "text/plain") |
| 182 | + |> wisp.set_header("retry-after", "60") |
| 183 | + |> wisp.string_body("Badge temporarily unavailable") |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +fn fetch_badge_svg() -> Option(String) { |
| 189 | + let assert Ok(req0) = request.to(product_hunt_url) |
| 190 | + let req = |
| 191 | + req0 |
| 192 | + |> request.prepend_header("accept", "image/svg+xml") |
| 193 | + |> request.prepend_header("user-agent", "FluxerMarketing/1.0") |
| 194 | + |
| 195 | + let config = |
| 196 | + httpc.configure() |
| 197 | + |> httpc.timeout(fetch_timeout_ms) |
| 198 | + |
| 199 | + case httpc.dispatch(config, req) { |
| 200 | + Ok(resp) if resp.status >= 200 && resp.status < 300 -> |
| 201 | + option.Some(resp.body) |
| 202 | + _ -> option.None |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +type TimeUnit { |
| 207 | + Millisecond |
| 208 | +} |
| 209 | + |
| 210 | +@external(erlang, "erlang", "monotonic_time") |
| 211 | +fn erlang_monotonic_time(unit: TimeUnit) -> Int |
| 212 | + |
| 213 | +fn monotonic_time_ms() -> Int { |
| 214 | + erlang_monotonic_time(Millisecond) |
| 215 | +} |
0 commit comments