Restructure Web/HTTP handling, statically serve acme-challenge dir
This commit is contained in:
parent
83c7c11b97
commit
ce26909daa
5 changed files with 40 additions and 7 deletions
|
@ -2,5 +2,6 @@ import Config
|
||||||
|
|
||||||
config :ha_handler,
|
config :ha_handler,
|
||||||
http_port: 4000,
|
http_port: 4000,
|
||||||
|
acme_challenge_path: "acme-challenge",
|
||||||
haproxy_socket: System.get_env("HAPROXY_SOCKET") || "/var/run/haproxy.sock"
|
haproxy_socket: System.get_env("HAPROXY_SOCKET") || "/var/run/haproxy.sock"
|
||||||
|
|
||||||
|
|
|
@ -3,4 +3,5 @@ defmodule HAHandler do
|
||||||
|
|
||||||
def http_port, do: Application.get_env(:ha_handler, :http_port)
|
def http_port, do: Application.get_env(:ha_handler, :http_port)
|
||||||
def haproxy_socket, do: Application.get_env(:ha_handler, :haproxy_socket)
|
def haproxy_socket, do: Application.get_env(:ha_handler, :haproxy_socket)
|
||||||
|
def acme_challenge_path, do: Application.get_env(:ha_handler, :acme_challenge_path)
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,7 @@ defmodule HAHandler.Application do
|
||||||
@impl true
|
@impl true
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
children = [
|
children = [
|
||||||
{Plug.Cowboy, scheme: :http, plug: HAHandler.Web, options: [port: http_port()]}
|
{Plug.Cowboy, scheme: :http, plug: HAHandler.Web.Router, options: [port: http_port()]}
|
||||||
]
|
]
|
||||||
|
|
||||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||||
|
|
|
@ -1,13 +1,9 @@
|
||||||
defmodule HAHandler.Web do
|
defmodule HAHandler.Web.Controller do
|
||||||
import Plug.Conn
|
import Plug.Conn
|
||||||
|
|
||||||
alias HAHandler.HAProxy
|
alias HAHandler.HAProxy
|
||||||
|
|
||||||
def init(opts) do
|
def index(conn) do
|
||||||
opts
|
|
||||||
end
|
|
||||||
|
|
||||||
def call(conn, _opts) do
|
|
||||||
{:ok, hostname} = :inet.gethostname()
|
{:ok, hostname} = :inet.gethostname()
|
||||||
|
|
||||||
stats = HAProxy.get_stats()
|
stats = HAProxy.get_stats()
|
35
lib/ha_handler/web/router.ex
Normal file
35
lib/ha_handler/web/router.ex
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
defmodule HAHandler.Web.Router do
|
||||||
|
@moduledoc """
|
||||||
|
This module dispatch incoming HTTP requests to their
|
||||||
|
related logic. Please refer to [1] for details.
|
||||||
|
|
||||||
|
[1] https://hexdocs.pm/plug/Plug.Router.html#content
|
||||||
|
"""
|
||||||
|
|
||||||
|
use Plug.Router
|
||||||
|
|
||||||
|
import HAHandler, only: [acme_challenge_path: 0]
|
||||||
|
|
||||||
|
alias HAHandler.Web.Controller
|
||||||
|
|
||||||
|
# Note for plugs: oder is important, as a plug may stop
|
||||||
|
# want to stop the pipeline!
|
||||||
|
|
||||||
|
plug Plug.Logger, log: :debug
|
||||||
|
# Note: `plug` is a macro, hence evaluated at
|
||||||
|
# compile-time. The configuration (which path to
|
||||||
|
# serve) cannot be modified at runtime with this
|
||||||
|
# system.
|
||||||
|
plug Plug.Static,
|
||||||
|
at: "/.well-known/acme-challenge/",
|
||||||
|
from: acme_challenge_path()
|
||||||
|
|
||||||
|
plug :match
|
||||||
|
plug :dispatch
|
||||||
|
|
||||||
|
get "/", do: Controller.index(conn)
|
||||||
|
|
||||||
|
match _ do
|
||||||
|
send_resp(conn, 404, "Not found")
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue