initial handling of blog content and serving of blog content files/assets
This commit is contained in:
parent
8269f1f24f
commit
80da842416
7 changed files with 209 additions and 2 deletions
lib/home73k_web
30
lib/home73k_web/controllers/blog_file_controller.ex
Normal file
30
lib/home73k_web/controllers/blog_file_controller.ex
Normal file
|
@ -0,0 +1,30 @@
|
|||
defmodule Home73kWeb.BlogFileController do
|
||||
use Home73kWeb, :controller
|
||||
|
||||
@moduledoc """
|
||||
This controller handles path requests that didn't match an existing route,
|
||||
including the main Plug.Static for the / root path.
|
||||
|
||||
To handle this situation, we'll check if the requested resource exists as
|
||||
a file under the blog content repo folder, by querying the Blog genserve.
|
||||
|
||||
If it exists, we'll redirect to the blog content static path under /_
|
||||
Otherwise, we'll return 404 not found.
|
||||
"""
|
||||
|
||||
alias Home73k.Repo
|
||||
alias Home73k.Blog
|
||||
|
||||
def index(conn, _params) do
|
||||
# What would be the content path of this requested resource?
|
||||
content_path = Repo.content_path() |> Path.join(conn.request_path)
|
||||
|
||||
# Check if it exists in the Blog's known files
|
||||
Blog.get_files()
|
||||
|> Enum.member?(content_path)
|
||||
|> case do
|
||||
true -> redirect(conn, to: "/_#{conn.request_path}")
|
||||
false -> send_resp(conn, 404, "not found")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -26,6 +26,18 @@ defmodule Home73kWeb.Endpoint do
|
|||
gzip: true,
|
||||
only: ~w(css fonts images js favicon.ico robots.txt DF185CEE29A3D443_public_key.asc)
|
||||
|
||||
# Blog static path handler
|
||||
if File.dir?(Home73k.Repo.content_path()) do
|
||||
plug Plug.Static,
|
||||
at: "/_",
|
||||
from: Home73k.Repo.content_path(),
|
||||
gzip: false,
|
||||
only:
|
||||
Home73k.Repo.content_path()
|
||||
|> File.ls!()
|
||||
|> Enum.filter(fn f -> !String.starts_with?(f, ".") end)
|
||||
end
|
||||
|
||||
# Code reloading can be explicitly enabled under the
|
||||
# :code_reloader configuration of your endpoint.
|
||||
if code_reloading? do
|
||||
|
|
|
@ -43,4 +43,9 @@ defmodule Home73kWeb.Router do
|
|||
live_dashboard "/dashboard", metrics: Home73kWeb.Telemetry
|
||||
end
|
||||
end
|
||||
|
||||
# Wildcard path for handling Blog Files from repo
|
||||
scope "/", Home73kWeb do
|
||||
get "/*path", BlogFileController, :index
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue