Serving static JSON assets with a show action

I’m building an API connecting with various front-end UI’s. I have a context that is still being built while I hash out the business logic properly with non-development teams BUT in the meanwhile I need to serve at least a sample version of the API data (the “show” action) at the right location so that frontend development can continue.

The API would be hit at /models/1, 1 being the sample ID, and then serve up a JSON file containing all the static data. I have a static YML file I’ve been using to construct this sample, but how exactly would I serve it? As a static asset?

I’m thinking of replacing the default show action with an implementation that disregards the ID parameter and simply uses Plug.Conn.send_file/5 to serve sample_model.json but I don’t think this is the right way of going about this.

Alternatively, maybe I should short-circuit the model = Context.get_model(id) cast and directly specify a map inside there. This would look quite dirty (almost doubling the line-length of the file) but it’s temporary anyway.

Solved - all I had to do was overwrite the model_view.render/2 function that begins with

def render("model.json", %{model: model}) do

It’s dirty but it works.

2 Likes