Project

Archived

Building the Now Playing widget

How the "Now playing" widget was built.

Source ↗

UPDATE: As of October 2026 this widget has been retired and is no longer on the site. This write-up is kept as a record of how it was built.

If you’ve visited my /about page, you’ve probably noticed a small widget showing whatever I’m currently listening to (or the last thing I played). The widget pulls my playing activity from Last.fm. The music source can be anything which scrobbles data to my Last.fm account. In this case it’s either a Roon server or Spotify.

It’s a lightweight piece of infrastructure enabling a tiny Cloud Run service/container which uses a simple CI/CD pipeline.

Why does this exist in the first place?

I listen to a lot of music and sometimes I’ll visit my Last.fm profile to look at the basic statistics around my playing habits. For a free account there are some basic reports too and if you subscribe, for s small monthly fee, you can access more advanced reporting. So this got me thinking, “what can I build using my data?”

How does it work?

Everything is driven by APIs, which is the entrypoint to the data. If your app logs your data, all it’s doing is using an API to send data (events) as a data source to the Last.fm endpoint. The data is saved to a database which is then queried by the front facing Last.fm website you use to browse your listening.

Last.fm has an API that anyone can use. There are numerous sites and apps which enable all kinds of reports and visualisations based on your music listening. All these do is use the published endpoints to query the data and present it to you in different ways. That’s pretty much it.

I was curious so I got myself an API key and started querying endpoints using Postman. The thing I like about Postman is that even with a free account you can get up and running really quickly when playing with endpoints.

Read more about the Last.fm API and methods offered.

As an exercise I thought about what I would build to dip my toe in, hence the “Now Playing” app. The eventual idea is to use the API to get all my historical data and do something interesting with it. Still figuring out that bit.

Cloud Architecture

cloud architecture

The Now Playing app

The Now Playing app calls a small JSON API that wraps last.fm’s user.getrecenttracks endpoint:

GET https://lastfm-now-playing-ei7ug2a2za-uw.a.run.app/

{
  "isPlaying": true,
  "track": "Ms. Tundra",
  "artist": "Ne-Yo",
  "album": "Ms. Tundra",
  "albumArt": "https://lastfm-img.freetls.fastly.net/i/u/174s/....jpg",
  "url": "https://www.last.fm/music/Ne-Yo/_/Ms.+Tundra"
}

The frontend just polls that and renders it. All the actual interesting decisions live in the service behind it.

What are Cloud Run functions and Cloud Run?

First, let’s define what these services are and the differences between them.

Google Cloud Run functions and Cloud Run are built on the exact same underlying infrastructure, Cloud Run, meaning Cloud Run functions is essentially a wrapper around Cloud Run to simplify developer workflows. While Cloud Run functions is built on top of Cloud Run, they serve slightly different operational processes.

Cloud Run functions uses Cloud Build to package your source code into a container image and automatically deploys it as a Cloud Run service.

You’ll choose this when you want to write simple, single-purpose snippet functions or background event processors (for example handling Cloud Storage file uploads or Pub/Sub events) without managing web servers.

Plus if you prefer direct source-code deployment via Google Cloud Console, gcloud, or Firebase CLI then you would consider Cloud Run functions.

When to choose Cloud Run

If you have a full web application, custom web framework (like Express, Django, or FastAPI), or existing Docker containers then consider Cloud Run. And if you need control over system-level dependencies, native libraries, custom runtime environments, or listening on custom ports, then Cloud Run should be a consideration, especially if you require support for web protocols beyond standard HTTP/CloudEvents (such as gRPC or WebSockets).

Cloud Run functions

A plain Cloud Run service does exactly what I wanted: build an image anywhere, push it to a registry, tell Cloud Run “run this.” So GitHub Actions builds the image, pushes it to Artifact Registry, and Cloud Run runs whatever’s there.

No long-lived credentials anywhere

GitHub → GCP: rather than a downloadable service account key sitting in a GitHub secret forever, the pipeline uses Workload Identity Federation. GitHub Actions presents its own short-lived OIDC token for the workflow run; GCP is configured to trust that token, scoped to this exact repo, and exchanges it for temporary access as a narrowly-scoped deploy service account. No key ever leaves GCP, and there’s nothing long-lived to leak.

API key → app: the last.fm API key never touches application code. Cloud Run’s native Secret Manager integration mounts the secret as an environment variable at the platform level, so the app just reads process.env.LASTFM_API_KEY like any other environment variable — no Secret Manager SDK calls, no key baked into the image, no key in git or in CI logs.

The app itself

Deliberately boring: plain Node/TypeScript with zero runtime dependencies. Node’s built-in http module and global fetch are enough for one route, so that’s all it uses.

  • an HTTP server that sets permissive CORS headers (it’s public, read-only data, so allowing any origin is fine) and routes GET / to a handler
  • a last.fm client that calls user.getrecenttracks, maps the response into the JSON shape above, and keeps a 20-second in-memory cache, short enough that the data stays fresh, long enough to absorb both last.fm’s own rate limits and any burst of visitor traffic without hammering its API

A container with (almost) nothing in it

The Dockerfile is a two-stage build. The first stage installs dependencies and compiles TypeScript on node:24-slim. The second stage copies only the compiled dist/ output into a distroless Node 24 runtime image, running as a non-root user.

The reasoning here goes beyond “smaller is nicer”: node:24-slim still bundles npm, and everything npm itself depends on whether or not the final container ever runs it. A vulnerability scanner will happily keep flagging CVEs in tooling that never actually executes at runtime. Shipping only the compiled output on a distroless base means there’s no npm, no shell, and no package manager in the thing that’s actually running in production, which in turn presents a smaller attack surface and a much quieter security scan.

CI/CD

Two GitHub Actions workflows do the work, written to be reusable across future apps in the same repo rather than one-off:

  • a lint workflow — format check, ESLint, tsc --noEmit, and npm audit (failing on high/critical), runs on every PR
  • a deploy workflow — authenticate via WIF, build the image, scan it with Trivy, push to Artifact Registry, then gcloud run deploy, runs on push to main

The Trivy scan reports rather than blocks. Most findings in a container image land in the base image’s OS layer, which I don’t control and can’t patch faster than its maintainer does. So the base image is pinned by digest, and Dependabot proposes updates to that pin on its own schedule instead of trying to fix findings by hand.

Beyond that: CodeQL runs static analysis on push/PR plus a weekly scheduled scan (so newly-disclosed CVEs in unchanged code still get caught), dependency review flags newly-introduced vulnerable dependencies in every PR diff, and Dependabot opens weekly PRs for outdated npm packages, the base Docker image, and GitHub Action versions.

Infrastructure, kept separate

The GCP side, the Cloud Run service, the Workload Identity Federation pool, the deploy service account, the Secret Manager container, is provisioned by Terraform in a separate infrastructure repo, not alongside the app code. A few choices worth calling out:

  • the Cloud Run service is bootstrapped with Google’s public placeholder image, so the very first terraform apply succeeds before any real image has ever been pushed, with lifecycle.ignore_changes on the image field so CI’s real deploys don’t get reverted the next time Terraform plans
  • the Workload Identity Pool is shared across the whole apps repo rather than created per-app, since WIF trust is repo-scoped rather than path-scoped, each app just gets its own narrowly-scoped deploy service account bound to that shared pool
  • the runtime service account has secretAccessor on exactly one secret, nothing project-wide
  • Terraform manages the Secret Manager container for the API key, never the value itself, the key is set out-of-band via gcloud secrets versions add, so it never ends up in Terraform state or in git history

What I’d reuse next time

The repo this lives in (elliotx-website-apps) is deliberately structured to hold more than one small backend service over time, one self-contained folder per app, sharing the deploy/lint workflow pattern rather than each reinventing it. Adding a second app should mostly be: copy a folder, copy a thin per-app workflow file, and point a new Terraform module at the existing shared WIF pool. That was the actual goal of building this for something as small as a now-playing badge, not because the widget needed it, but because the pattern was worth having.

Now that this is running I think this is something that can be built on in the future. I would like to get historical data and produce some kind of data viz.

Source: github.com/elliotrichards/elliotx-website-apps