Skip to content

Commit

Permalink
Refactor web router (#1177)
Browse files Browse the repository at this point in the history
* Reorganize archethic_web folders

* Reorganize archetic_web_test folders

* Rename archethic_web modules

* Rework archethic web routers

* Refactor API folder

* Add error control in router_dispatch
  • Loading branch information
Neylix authored Jul 25, 2023
1 parent d4a2693 commit e0465b6
Show file tree
Hide file tree
Showing 149 changed files with 643 additions and 558 deletions.
8 changes: 4 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ config :archethic,
# Configure the endpoint
config :archethic, ArchethicWeb.Endpoint,
secret_key_base: "5mFu4p5cPMY5Ii0HvjkLfhYZYtC0JAJofu70bzmi5x3xzFIJNlXFgIY5g8YdDPMf",
render_errors: [view: ArchethicWeb.ErrorView, accepts: ~w(json)],
render_errors: [view: ArchethicWeb.Explorer.ErrorView, accepts: ~w(json)],
pubsub_server: ArchethicWeb.PubSub,
live_view: [
signing_salt: "3D6jYvx3",
layout: {ArchethicWeb.LayoutView, "live.html"}
layout: {ArchethicWeb.Explorer.LayoutView, "live.html"}
]

config :archethic, Archethic.Mining.StandaloneWorkflow, global_timeout: 10_000
Expand All @@ -149,7 +149,7 @@ config :archethic, Archethic.OracleChain.Services.UCOPrice,
Archethic.OracleChain.Services.UCOPrice.Providers.CoinPaprika => [refresh_interval: 120_000]
}

config :archethic, ArchethicWeb.FaucetController,
config :archethic, ArchethicWeb.Explorer.FaucetController,
seed:
"3A7B579DBFB7CEBE26293850058F180A65D6A3D2F6964543F5EDE07BEB2EFDA4"
|> Base.decode16!(case: :mixed)
Expand All @@ -165,7 +165,7 @@ config :archethic, Archethic.Networking.IPLookup.RemoteDiscovery,
config :archethic, Archethic.Networking.PortForwarding, port_range: 49_152..65_535
# -----End-of-Networking-configs ------

config :archethic, ArchethicWeb.API.WebHostingController,
config :archethic, ArchethicWeb.AEWeb.WebHostingController,
# The tx_cache is stored on RAM
# 750MB should hold a minimum 250 transactions
tx_cache_bytes: 750 * 1024 * 1024,
Expand Down
6 changes: 3 additions & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ config :archethic, Archethic.SharedSecrets.NodeRenewalScheduler,
config :archethic, Archethic.P2P.Listener,
port: System.get_env("ARCHETHIC_P2P_PORT", "3002") |> String.to_integer()

config :archethic, ArchethicWeb.FaucetController, enabled: true
config :archethic, ArchethicWeb.FaucetRateLimiter, enabled: true
config :archethic, ArchethicWeb.Explorer.FaucetController, enabled: true
config :archethic, ArchethicWeb.Explorer.FaucetRateLimiter, enabled: true

# For development, we disable any cache and enable
# debugging and code reloading.
Expand Down Expand Up @@ -159,7 +159,7 @@ config :archethic, ArchethicWeb.Endpoint,
port: System.get_env("ARCHETHIC_HTTPS_PORT", "5000") |> String.to_integer(),
cipher_suite: :strong,
otp_app: :archethic,
sni_fun: &ArchethicWeb.Domain.sni/1,
sni_fun: &ArchethicWeb.AEWeb.Domain.sni/1,
keyfile: "priv/cert/selfsigned_key.pem",
certfile: "priv/cert/selfsigned.pem"
]
Expand Down
6 changes: 3 additions & 3 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ config :archethic, Archethic.P2P.BootstrappingSeeds,

config :archethic, Archethic.Utils.DetectNodeResponsiveness, timeout: 10_000

config :archethic, ArchethicWeb.FaucetController,
config :archethic, ArchethicWeb.Explorer.FaucetController,
enabled: System.get_env("ARCHETHIC_NETWORK_TYPE") == "testnet"

config :archethic, ArchethicWeb.FaucetRateLimiter,
config :archethic, ArchethicWeb.Explorer.FaucetRateLimiter,
enabled: System.get_env("ARCHETHIC_NETWORK_TYPE") == "testnet"

# For production, don't forget to configure the url host
Expand Down Expand Up @@ -272,7 +272,7 @@ config :archethic, ArchethicWeb.Endpoint,
cipher_suite: :strong,
otp_app: :archethic,
port: System.get_env("ARCHETHIC_HTTPS_PORT", "50000") |> String.to_integer(),
sni_fun: &ArchethicWeb.Domain.sni/1,
sni_fun: &ArchethicWeb.AEWeb.Domain.sni/1,
keyfile: System.get_env("ARCHETHIC_WEB_SSL_KEYFILE", "priv/cert/selfsigned_key.pem"),
certfile: System.get_env("ARCHETHIC_WEB_SSL_CERTFILE", "priv/cert/selfsigned.pem")
]
Expand Down
4 changes: 2 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ config :archethic, Archethic.TransactionChain.MemTablesLoader, enabled: false

config :archethic, Archethic.Utils.DetectNodeResponsiveness, timeout: 1_000

config :archethic, ArchethicWeb.FaucetController, enabled: true
config :archethic, ArchethicWeb.FaucetRateLimiter, enabled: false
config :archethic, ArchethicWeb.Explorer.FaucetController, enabled: true
config :archethic, ArchethicWeb.Explorer.FaucetRateLimiter, enabled: false
config :archethic, ArchethicWeb.TransactionSubscriber, enabled: false

# We don't run a server during test. If one is required,
Expand Down
39 changes: 39 additions & 0 deletions lib/archethic_web/aeweb.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule ArchethicWeb.AEWeb do
@moduledoc false

def controller do
quote do
use Phoenix.Controller, namespace: ArchethicWeb.AEWeb

import Plug.Conn
end
end

def view do
quote do
use Phoenix.View,
root: "lib/archethic_web/aeweb/templates",
namespace: ArchethicWeb.AEWeb

# Import convenience functions from controllers
import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1]
import ArchethicWeb.WebUtils
# Import basic rendering functionality (render, render_layout, etc)
import Phoenix.View
end
end

def router do
quote do
use Phoenix.Router
import Phoenix.Controller
end
end

@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
28 changes: 28 additions & 0 deletions lib/archethic_web/aeweb/aeweb_router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule ArchethicWeb.AEWebRouter do
@moduledoc false
use ArchethicWeb.AEWeb, :router

alias ArchethicWeb.AEWeb.WebHostingController

alias ArchethicWeb.Plug.ThrottleByIPHigh
alias ArchethicWeb.Plug.ThrottleByIPandPath

pipeline :browser do
plug(:accepts, ["html"])
plug(:put_secure_browser_headers)
plug(ThrottleByIPHigh)
plug(ThrottleByIPandPath)
end

scope "/aeweb" do
pipe_through(:browser)

get("/:address/*url_path", WebHostingController, :web_hosting)
end

scope "/api/web_hosting" do
pipe_through(:browser)

get("/:address/*url_path", WebHostingController, :web_hosting)
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
defmodule ArchethicWeb.API.WebHostingController do
defmodule ArchethicWeb.AEWeb.WebHostingController do
@moduledoc false

use ArchethicWeb, :controller
use ArchethicWeb.AEWeb, :controller

alias Archethic.Crypto

require Logger
alias ArchethicWeb.AEWeb.WebHostingController.Resources
alias ArchethicWeb.AEWeb.WebHostingController.DirectoryListing
alias ArchethicWeb.AEWeb.WebHostingController.ReferenceTransaction

alias ArchethicWeb.API.WebHostingController.{
Resources,
DirectoryListing,
ReferenceTransaction
}
require Logger

@spec web_hosting(Plug.Conn.t(), params :: map()) :: Plug.Conn.t()
def web_hosting(conn, params = %{"url_path" => []}) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule ArchethicWeb.API.WebHostingController.DirectoryListing do
defmodule ArchethicWeb.AEWeb.WebHostingController.DirectoryListing do
@moduledoc false

alias ArchethicWeb.API.WebHostingController.ReferenceTransaction
alias ArchethicWeb.AEWeb.DirListingView
alias ArchethicWeb.AEWeb.WebHostingController.ReferenceTransaction

require Logger

Expand Down Expand Up @@ -40,8 +41,9 @@ defmodule ArchethicWeb.API.WebHostingController.DirectoryListing do
address
)

{:ok, Phoenix.View.render_to_iodata(ArchethicWeb.DirListingView, "index.html", assigns),
nil, mime_type, cached?, etag}
render = Phoenix.View.render_to_iodata(DirListingView, "index.html", assigns)

{:ok, render, nil, mime_type, cached?, etag}
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule ArchethicWeb.API.WebHostingController.ReferenceTransaction do
defmodule ArchethicWeb.AEWeb.WebHostingController.ReferenceTransaction do
@moduledoc """
ReferenceTransaction is a subset of a transaction
It is meant to be cached so we use a lighter structure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule ArchethicWeb.API.WebHostingController.Resources do
defmodule ArchethicWeb.AEWeb.WebHostingController.Resources do
@moduledoc false

alias Archethic.TransactionChain.{Transaction, TransactionData}
alias ArchethicCache.LRUDisk
alias ArchethicWeb.API.WebHostingController.ReferenceTransaction
alias ArchethicWeb.AEWeb.WebHostingController.ReferenceTransaction

require Logger

Expand Down
31 changes: 31 additions & 0 deletions lib/archethic_web/aeweb/dnslink_router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule ArchethicWeb.DNSLinkRouter do
@moduledoc """
Catch dns link redirection for AEWeb and call WebHostingController
"""

alias ArchethicWeb.AEWeb.Domain
alias ArchethicWeb.AEWeb.WebHostingController

@behaviour Plug

def init(opts), do: opts

def call(conn = %Plug.Conn{host: host, method: "GET", path_info: url_path}, _) do
case get_dnslink_address(host) do
{:ok, address} ->
WebHostingController.web_hosting(conn, %{"address" => address, "url_path" => url_path})

_ ->
throw("No DNSLink defined")
end
end

def call(_conn, _), do: throw("No DNSLink defined")

defp get_dnslink_address(host) do
if is_ip_address?(host), do: {:error, :ip_address}, else: Domain.lookup_dnslink_address(host)
end

defp is_ip_address?(host),
do: {:ok, _ip} |> match?(host |> String.to_charlist() |> :inet.parse_address())
end
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
defmodule ArchethicWeb.Domain do
defmodule ArchethicWeb.AEWeb.Domain do
@moduledoc """
Manage AEWeb domain logic
"""

alias Archethic
alias Archethic.Crypto
alias Archethic.TransactionChain.TransactionData.Ownership
alias ArchethicWeb.API.WebHostingController.ReferenceTransaction

alias ArchethicWeb.AEWeb.WebHostingController.ReferenceTransaction

require Logger

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule ArchethicWeb.DirListingView do
defmodule ArchethicWeb.AEWeb.DirListingView do
@moduledoc false
use ArchethicWeb, :view
use ArchethicWeb.AEWeb, :view

@spec prepare_addresses(list(String.t())) :: list(map())
def prepare_addresses(addresses) do
Expand Down
20 changes: 0 additions & 20 deletions lib/archethic_web/aeweb_router.ex

This file was deleted.

25 changes: 25 additions & 0 deletions lib/archethic_web/api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule ArchethicWeb.API do
@moduledoc false

def controller do
quote do
use Phoenix.Controller, namespace: ArchethicWeb.API

import Plug.Conn
end
end

def router do
quote do
use Phoenix.Router
import Plug.Conn
end
end

@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
43 changes: 43 additions & 0 deletions lib/archethic_web/api/api_router.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule ArchethicWeb.APIRouter do
@moduledoc false

alias ArchethicWeb.API
alias ArchethicWeb.Plug.ThrottleByIPLow

use ArchethicWeb.Explorer, :router

pipeline :api do
plug(:accepts, ["json"])
plug(ThrottleByIPLow)
plug(ArchethicWeb.API.GraphQL.Context)
end

scope "/api", API do
pipe_through(:api)

get("/last_transaction/:address/content", TransactionController, :last_transaction_content)

post("/origin_key", OriginKeyController, :origin_key)
post("/transaction", TransactionController, :new)
post("/transaction_fee", TransactionController, :transaction_fee)

post("/transaction/contract/simulator", TransactionController, :simulate_contract_execution)
end

scope "/api" do
pipe_through(:api)

forward(
"/graphiql",
Absinthe.Plug.GraphiQL,
schema: ArchethicWeb.API.GraphQL.Schema,
socket: ArchethicWeb.UserSocket
)

forward(
"/",
Absinthe.Plug,
schema: ArchethicWeb.API.GraphQL.Schema
)
end
end
Loading

0 comments on commit e0465b6

Please sign in to comment.