From 9a7629765194cc8ce5c19462d22a7ab103e153e7 Mon Sep 17 00:00:00 2001 From: saneery Date: Thu, 18 Jul 2019 16:59:13 +0300 Subject: [PATCH] Update routers to hide and show api's docs --- .../lib/block_scout_web/api_router.ex | 21 -- .../lib/block_scout_web/router.ex | 46 +++- .../new.html.eex | 2 +- .../templates/layout/_topnav.html.eex | 204 +++++++++--------- .../address_contract_verification_view.ex | 5 - .../lib/block_scout_web/web_router.ex | 3 - .../api/v1/health_controller_test.exs | 6 - .../controllers/api_docs_controller_test.exs | 2 +- 8 files changed, 147 insertions(+), 142 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/api_router.ex b/apps/block_scout_web/lib/block_scout_web/api_router.ex index 650188b6a2..73386dd4fa 100644 --- a/apps/block_scout_web/lib/block_scout_web/api_router.ex +++ b/apps/block_scout_web/lib/block_scout_web/api_router.ex @@ -5,7 +5,6 @@ defmodule BlockScoutWeb.ApiRouter do use BlockScoutWeb, :router alias BlockScoutWeb.API.RPC - alias BlockScoutWeb.Plug.GraphQL pipeline :api do plug(:accepts, ["json"]) @@ -19,8 +18,6 @@ defmodule BlockScoutWeb.ApiRouter do scope "/", BlockScoutWeb.API.V1 do get("/supply", SupplyController, :supply) - get("/health", HealthController, :health) - post("/decompiled_smart_contract", DecompiledSmartContractController, :create) post("/verified_smart_contracts", VerifiedSmartContractController, :create) @@ -38,24 +35,6 @@ defmodule BlockScoutWeb.ApiRouter do end end - # Needs to be 200 to support the schema introspection for graphiql - @max_complexity 200 - - forward("/graphql", Absinthe.Plug, - schema: BlockScoutWeb.Schema, - analyze_complexity: true, - max_complexity: @max_complexity - ) - - forward("/graphiql", Absinthe.Plug.GraphiQL, - schema: BlockScoutWeb.Schema, - interface: :advanced, - default_query: GraphQL.default_query(), - socket: BlockScoutWeb.UserSocket, - analyze_complexity: true, - max_complexity: @max_complexity - ) - # For backward compatibility. Should be removed scope "/", BlockScoutWeb.API.RPC do pipe_through(:api) diff --git a/apps/block_scout_web/lib/block_scout_web/router.ex b/apps/block_scout_web/lib/block_scout_web/router.ex index 1354c57d56..62c6e09d91 100644 --- a/apps/block_scout_web/lib/block_scout_web/router.ex +++ b/apps/block_scout_web/lib/block_scout_web/router.ex @@ -2,6 +2,10 @@ defmodule BlockScoutWeb.Router do use BlockScoutWeb, :router alias BlockScoutWeb.Plug.GraphQL + alias BlockScoutWeb.{ApiRouter, WebRouter} + + forward("/wobserver", Wobserver.Web.Router) + forward("/admin", BlockScoutWeb.AdminRouter) pipeline :browser do plug(:accepts, ["html"]) @@ -11,13 +15,24 @@ defmodule BlockScoutWeb.Router do plug(BlockScoutWeb.CSPHeader) end - forward("/wobserver", Wobserver.Web.Router) - forward("/admin", BlockScoutWeb.AdminRouter) + pipeline :api do + plug(:accepts, ["json"]) + end + + scope "/", BlockScoutWeb.API.V1, as: :api_v1 do + pipe_through(:api) + get("/api/v1/health", HealthController, :health) + end - if Application.get_env(:block_scout_web, BlockScoutWeb.ApiRouter)[:enabled] do - forward("/api", BlockScoutWeb.ApiRouter) + scope "/verify_smart_contract" do + pipe_through(:api) + + post("/contract_verifications", BlockScoutWeb.AddressContractVerificationController, :create) + end + + if Application.get_env(:block_scout_web, ApiRouter)[:enabled] do + forward("/api", ApiRouter) - # For backward compatibility. Should be removed # Needs to be 200 to support the schema introspection for graphiql @max_complexity 200 @@ -35,9 +50,28 @@ defmodule BlockScoutWeb.Router do analyze_complexity: true, max_complexity: @max_complexity ) + else + scope "/", BlockScoutWeb do + pipe_through(:browser) + get("/api_docs", PageNotFoundController, :index) + get("/eth_rpc_api_docs", PageNotFoundController, :index) + end end - if Application.get_env(:block_scout_web, BlockScoutWeb.WebRouter)[:enabled] do + scope "/", BlockScoutWeb do + pipe_through(:browser) + + get("/api_docs", APIDocsController, :index) + get("/eth_rpc_api_docs", APIDocsController, :eth_rpc) + end + + if Application.get_env(:block_scout_web, WebRouter)[:enabled] do forward("/", BlockScoutWeb.WebRouter) + else + scope "/", BlockScoutWeb do + pipe_through(:browser) + + forward("/", APIDocsController, :index) + end end end diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex index a1a715a734..fd93b0af2b 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address_contract_verification/new.html.eex @@ -9,7 +9,7 @@

<%= gettext "New Smart Contract Verification" %>

<%= form_for @changeset, - api_v1_address_contract_verification_path(@conn, :create), + address_contract_verification_path(@conn, :create), [], fn f -> %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/layout/_topnav.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/layout/_topnav.html.eex index f152ec6bf9..3ae4779284 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/layout/_topnav.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/layout/_topnav.html.eex @@ -8,79 +8,83 @@ - - - - + + <% end %> + <%= if Application.get_env(:block_scout_web, BlockScoutWeb.ApiRouter)[:enabled] do %> + + <% end %> -
- <%= form_for @conn, chain_path(@conn, :search), [class: "form-inline my-2 my-lg-0", method: :get, enforce_utf8: false], fn f -> %> -
- <%= awesomplete(f, :q, - [ - class: "form-control me auto", - placeholder: gettext("Search by address, token symbol name, transaction hash, or block number"), - "aria-describedby": "search-icon", - "aria-label": gettext("Search"), - "data-test": "search_input" - ], - [ url: "#{chain_path(@conn, :token_autocomplete)}?q=", - limit: 0, - minChars: 2, - value: "contract_address_hash", - label: "contract_address_hash", - descrSearch: true, - descr: "symbol" - ]) %> -
- -
-
- - <% end %> -
+ <%= if Application.get_env(:block_scout_web, BlockScoutWeb.WebRouter)[:enabled] do %> +
+ <%= form_for @conn, chain_path(@conn, :search), [class: "form-inline my-2 my-lg-0", method: :get, enforce_utf8: false], fn f -> %> +
+ <%= awesomplete(f, :q, + [ + class: "form-control me auto", + placeholder: gettext("Search by address, token symbol name, transaction hash, or block number"), + "aria-describedby": "search-icon", + "aria-label": gettext("Search"), + "data-test": "search_input" + ], + [ url: "#{chain_path(@conn, :token_autocomplete)}?q=", + limit: 0, + minChars: 2, + value: "contract_address_hash", + label: "contract_address_hash", + descrSearch: true, + descr: "symbol" + ]) %> +
+ +
+
+ + <% end %> +
+ <% end %> diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_contract_verification_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_contract_verification_view.ex index b8e39ab547..874b470406 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_contract_verification_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_contract_verification_view.ex @@ -1,8 +1,3 @@ defmodule BlockScoutWeb.AddressContractVerificationView do use BlockScoutWeb, :view - alias BlockScoutWeb.ApiRouter.Helpers - - def api_v1_address_contract_verification_path(conn, action) do - "/api" <> Helpers.api_v1_address_contract_verification_path(conn, action) - end end diff --git a/apps/block_scout_web/lib/block_scout_web/web_router.ex b/apps/block_scout_web/lib/block_scout_web/web_router.ex index 9b250332db..6b445a7b41 100644 --- a/apps/block_scout_web/lib/block_scout_web/web_router.ex +++ b/apps/block_scout_web/lib/block_scout_web/web_router.ex @@ -199,9 +199,6 @@ defmodule BlockScoutWeb.WebRouter do get("/chain_blocks", ChainController, :chain_blocks, as: :chain_blocks) - get("/api_docs", APIDocsController, :index) - get("/eth_rpc_api_docs", APIDocsController, :eth_rpc) - get("/:page", PageNotFoundController, :index) end end diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/v1/health_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/v1/health_controller_test.exs index 53abd8b880..41735f685a 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/v1/health_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/v1/health_controller_test.exs @@ -1,8 +1,6 @@ defmodule BlockScoutWeb.API.V1.HealthControllerTest do use BlockScoutWeb.ConnCase - alias BlockScoutWeb.ApiRouter.Helpers - describe "GET last_block_status/0" do test "returns error when there are no blocks in db", %{conn: conn} do request = get(conn, api_v1_health_path(conn, :health)) @@ -49,8 +47,4 @@ defmodule BlockScoutWeb.API.V1.HealthControllerTest do } = Poison.decode!(request.resp_body) end end - - def api_v1_health_path(conn, action) do - "/api" <> Helpers.api_v1_health_path(conn, action) - end end diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api_docs_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api_docs_controller_test.exs index 4db9eccd6d..3b2cb99ff0 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api_docs_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api_docs_controller_test.exs @@ -1,7 +1,7 @@ defmodule BlockScoutWeb.APIDocsControllerTest do use BlockScoutWeb.ConnCase - import BlockScoutWeb.WebRouter.Helpers, only: [api_docs_path: 2] + import BlockScoutWeb.Router.Helpers, only: [api_docs_path: 2] describe "GET index/2" do test "renders documentation tiles for each API module#action", %{conn: conn} do