my website https://bigspeed.me
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(nowplaying): cache responses, work in docker

brwr.dev 6d6a2a98 a011fe68

verified
+10 -14
+1
spotify_proxy/Containerfile
··· 10 10 USER webapp 11 11 COPY --from=build /app/build/erlang-shipment /app 12 12 WORKDIR /app 13 + EXPOSE [8000] 13 14 ENTRYPOINT ["/app/entrypoint.sh"] 14 15 CMD ["run"]
+1 -1
spotify_proxy/src/spotify_proxy.gleam
··· 38 38 39 39 let ratelimit_child = 40 40 supervision.worker(fn() { 41 - Ok(actor.Started(ratelimiter.spawn_link(ratelimit_name, 2, 10_000), Nil)) 41 + Ok(actor.Started(ratelimiter.spawn_link(ratelimit_name, 3, 5000), Nil)) 42 42 }) 43 43 44 44 let spotify_child =
+8 -13
spotify_proxy/src/spotify_proxy/api.gleam
··· 31 31 wisp_mist.handler(fn(req) { router(req, ctx) }, secret_key_base) 32 32 |> mist.new 33 33 |> mist.port(8000) 34 + |> mist.bind("0.0.0.0") 34 35 |> mist.supervised 35 36 } 36 37 ··· 50 51 fn router(req: wisp.Request, ctx: Context) -> wisp.Response { 51 52 use req <- middleware(req, ctx) 52 53 case wisp.path_segments(req) { 53 - ["now-playing"] -> now_playing(req, ctx) 54 + [] -> now_playing(req, ctx) 54 55 _ -> wisp.not_found() 55 56 } 56 57 } ··· 58 59 fn now_playing(_req: wisp.Request, ctx: Context) -> wisp.Response { 59 60 let now_playing = actor.call(ctx.status_subject, 250, status.Get) 60 61 let resp = status_to_json(now_playing) 61 - wisp.json_response(json.to_string(resp), 200) 62 + 63 + json.to_string(resp) 64 + |> wisp.json_response(200) 65 + |> wisp.set_header("Cache-Control", "public, max-age=15") 62 66 } 63 67 64 68 fn status_to_json( ··· 72 76 let status.CurrentlyPlaying(song:, set_at:) = currently_playing 73 77 let status.Song(artists:, duration_ms:, progress_ms:, url:, name:) = song 74 78 75 - // add the time since progress was recorded to the progress bar :3 76 - // this doesn't make a huge difference actually unless i increase the refresh timeout thing 77 - // 0 <= progress <= duration_ms 78 - // NOTE: set_at is in SECONDS! 79 - let interpolated_progress = 80 - int.min( 81 - duration_ms, 82 - progress_ms + { int.max(0, util.now() - set_at) * 1000 }, 83 - ) 84 - 85 79 json.object([ 86 80 #("playing", json.bool(True)), 87 81 #("artists", json.array(artists, artist_to_json)), 88 82 #("duration_ms", json.int(duration_ms)), 89 - #("progress_ms", json.int(interpolated_progress)), 83 + #("progress_ms", json.int(progress_ms)), 84 + #("since", json.int(set_at)), 90 85 #("url", json.string(url)), 91 86 #("name", json.string(name)), 92 87 ])