MarkMekhaiel

MarkMekhaiel

Serving static files from a phoenix API

I’m trying to serve a file from my phoenix API, and believe Plug.Static is the way to do it, but am struggling to get it to work.

The file is an apple certificate, and I’m placing it in priv/certificates/apple

I’ve setup the following code in my endpoint.ex file:

plug Plug.Static,
    at: "/.well-known/apple",
    from: :my_app,
    gzip: false,
    only: ~w(certificates apple)

But when I visit the URL I get no route found for GET /.well-known/apple.

Do I need to add this to my routes file? And if so, does it hit a controller? What would that controller action look like?

Thanks in advance

Marked As Solved

stefanchrobot

stefanchrobot

The default path for reading static files is priv/static, so you need to tweak it:

plug Plug.Static,
    # Note the root path.
    at: "/.well-known",
    from: {:my_app, "priv/certificates"},
    gzip: false,
    only: ~w(apple)

But I’d suggest following @silverdr advice and put the files in the usual location. Assuming the actual file is called apple, try this:

  • Put the file into static: /priv/static/.well-known/apple
  • Use “standard” Static plug config:
  plug Plug.Static,
    at: "/",
    from: :my_app,
    gzip: false,
    only: [".well-known"]

Also Liked

silverdr

silverdr

How about simply putting it in priv/static/...?

Last Post!

dimitarvp

dimitarvp

You could post your relevant router config and maybe somebody else can chime in?

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement