【问题标题】:How to configure the Plug.Static without Phoenix如何在没有 Phoenix 的情况下配置 Plug.Static
【发布时间】:2015-12-09 04:24:19
【问题描述】:

我正在尝试弄清楚如何在没有任何其他框架(Phoenix、Sugar 等)的情况下配置 Plug.Static;只是 Cowboy、Plug 和 Elixir。我只是不知道如何在路由器中把东西放在一起。

  plug :match
  plug Plug.Static, at: "/pub", from: :cerber
  plug :dispatch

  get "/" do
    Logger.info "GET /"
    send_resp(conn, 200, "Hello world\n")
  end
  1. Plug.Static 的声明位置是否正确?不应该在plug :dispatch之后吗?
  2. 我是否需要定义额外的路线
  3. 有了这个声明:
    1. 要到达的 URL 是什么,比如index.html
    2. index.html 应位于文件系统上的什么位置

我只是迷路了。

【问题讨论】:

    标签: elixir phoenix-framework cowboy


    【解决方案1】:

    这就是我一直在寻找的答案。

    在应用启动方法中使用 Plug.Router 和 Cowboy:

    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
    

    这里对“/static”的请求被转发到HttpServer.StaticResources模块,但首先,使用plug(:redirect_index)修改“/”和“/favicon.ico”的请求路径。所有静态文件(*.html、*.ico、*.css、*.js 等)都放置在默认位置(project_dir/priv/static)。

    最后是 StaticResource 模块:

    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
    

    【讨论】:

      【解决方案2】:

      何塞·瓦利姆所说的一切。这是一个最简单的例子:

      defmodule Server do
        use Plug.Builder
        plug Plug.Logger
        plug Plug.Static, at: "/", from: "/path/to/static"
      end
      

      这将在“/”端点提供“/path/to/static”中的所有静态文件。

      查看文档以获得更多选项和更深入的解释。

      【讨论】:

        【解决方案3】:

        查看Plug.Router docs,了解:match:dispatch 的工作原理。 :match 将尝试找到匹配的路由,:dispatch 将调用它。这意味着您的设置中的Plug.Static 只有在您的路由器中有匹配的路由时才会被调用,这没有意义。你想要plug Plug.Static 在一切之前。请记住,插件只是按声明顺序调用的函数。

        除此之外,您的 Plug.Static 设置似乎还可以。您当前的配置将在“/pub”处提供资产,这意味着“/pub/index.html”将在您的应用程序中查找“priv/static/index.html”。更多信息在这里:http://hexdocs.pm/plug/Plug.Static.html

        【讨论】:

          猜你喜欢
          • 2014-02-18
          • 1970-01-01
          • 1970-01-01
          • 2016-10-27
          • 2023-03-08
          • 2017-12-02
          • 2017-03-19
          • 2018-05-01
          • 1970-01-01
          相关资源
          最近更新 更多