-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JET-2026] feature: use dynamic repo to execute action (#8)
- Loading branch information
Showing
33 changed files
with
310 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import Config | ||
|
||
config :resource_kit, ecto_repos: [ResourceKit.Repo] | ||
config :resource_kit, ecto_repos: [ResourceKitCLI.Repo] | ||
|
||
import_config "#{config_env()}.exs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Config | ||
|
||
config :resource_kit, ResourceKit.Repo, adapter: ResourceKitCLI.Repo | ||
|
||
config :resource_kit, ResourceKitCLI.Server, server: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
defmodule ResourceKit.Repo do | ||
use Ecto.Repo, | ||
otp_app: :resource_kit, | ||
adapter: Ecto.Adapters.Postgres | ||
@moduledoc false | ||
|
||
@adapter Application.compile_env!(:resource_kit, [__MODULE__, :adapter]) | ||
|
||
@type dynamic_repo() :: atom() | pid() | ||
|
||
@spec adapter() :: module() | ||
def adapter, do: @adapter | ||
|
||
@spec execute(dynamic_repo :: dynamic_repo(), callback :: (Ecto.Repo.t() -> result)) :: result | ||
when result: var | ||
def execute(dynamic_repo, callback) do | ||
default = adapter().get_dynamic_repo() | ||
|
||
try do | ||
adapter().put_dynamic_repo(dynamic_repo) | ||
callback.(@adapter) | ||
after | ||
adapter().put_dynamic_repo(default) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule ResourceKitCLI.Application do | ||
@moduledoc false | ||
|
||
use Application | ||
|
||
@impl Application | ||
def start(_type, _args) do | ||
children = [ResourceKitCLI.Server] | ||
|
||
Supervisor.start_link(children, strategy: :one_for_one, name: __MODULE__) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,15 @@ | ||
defmodule ResourceKitCLI.Endpoint do | ||
@moduledoc false | ||
|
||
use Supervisor | ||
|
||
@spec start_link(args :: keyword()) :: Supervisor.on_start() | ||
def start_link(args) do | ||
Supervisor.start_link(__MODULE__, configuration(args), name: __MODULE__) | ||
end | ||
|
||
@impl Supervisor | ||
def init(args) do | ||
{server, options} = Keyword.pop(args, :server, false) | ||
|
||
if server do | ||
Supervisor.init([{Bandit, options}], strategy: :one_for_one) | ||
else | ||
:ignore | ||
end | ||
@spec child_spec(args :: keyword()) :: Supervisor.child_spec() | ||
def child_spec(args) do | ||
Supervisor.child_spec({Bandit, configuration(args)}, id: __MODULE__) | ||
end | ||
|
||
defp configuration(args) do | ||
:resource_kit | ||
|> Application.get_env(__MODULE__, []) | ||
|> Keyword.merge(args) | ||
|> Keyword.put(:plug, ResourceKitPlug.Router) | ||
|> Keyword.put(:plug, ResourceKitCLI.Plug) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule ResourceKitCLI.Plug do | ||
@moduledoc false | ||
|
||
use Plug.Builder | ||
|
||
plug :put_dynamic | ||
plug ResourceKitPlug.Router | ||
|
||
defp put_dynamic(conn, _opts) do | ||
ResourceKitPlug.Router.put_dynamic(conn, ResourceKit.Repo.adapter()) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule ResourceKitCLI.Repo do | ||
use Ecto.Repo, | ||
otp_app: :resource_kit, | ||
adapter: Ecto.Adapters.Postgres | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule ResourceKitCLI.Server do | ||
@moduledoc false | ||
|
||
use Supervisor | ||
|
||
@spec start_link(args :: keyword()) :: Supervisor.on_start() | ||
def start_link(args) do | ||
Supervisor.start_link(__MODULE__, configuration(args), name: __MODULE__) | ||
end | ||
|
||
@impl Supervisor | ||
def init(args) do | ||
if Keyword.get(args, :server, false) do | ||
Supervisor.init([ResourceKitCLI.Repo, ResourceKitCLI.Endpoint], strategy: :one_for_one) | ||
else | ||
:ignore | ||
end | ||
end | ||
|
||
defp configuration(args) do | ||
:resource_kit | ||
|> Application.get_env(__MODULE__, []) | ||
|> Keyword.merge(args) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.