Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- plug :match
- plug Plug.Static, at: "/pub", from: :cerber
- plug :dispatch
- get "/" do
- Logger.info "GET /"
- send_resp(conn, 200, "Hello worldn")
- end
- defmodule Server do
- use Plug.Builder
- plug Plug.Logger
- plug Plug.Static, at: "/", from: "/path/to/static"
- end
- defmodule HttpServer.Application do
- require Logger
- use Application
- def start(_type, _args) do
- children = [
- {Plug.Adapters.Cowboy2, scheme: :http, plug: HttpServer.Router, options: [port: 4002]}
- ]
- opts = [strategy: :one_for_one, name: HttpServer.Supervisor]
- Supervisor.start_link(children, opts)
- end
- end
- defmodule HttpServer.Router do
- use Plug.Router
- plug(Plug.Logger)
- plug(:redirect_index)
- plug(:match)
- plug(:dispatch)
- forward("/static", to: HttpServer.StaticResources)
- get "/sse" do
- # some other stuff...
- conn
- end
- match _ do
- send_resp(conn, 404, "not found")
- end
- def redirect_index(%Plug.Conn{path_info: path} = conn, _opts) do
- case path do
- [] ->
- %{conn | path_info: ["static", "index.html"]}
- ["favicon.ico"] ->
- %{conn | path_info: ["static", "favicon.ico"]}
- _ ->
- conn
- end
- end
- end
- defmodule HttpServer.StaticResources do
- use Plug.Builder
- plug(
- Plug.Static,
- at: "/",
- from: :http_server
- )
- plug(:not_found)
- def not_found(conn, _) do
- send_resp(conn, 404, "static resource not found")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement