-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathash_json_api.exs
92 lines (77 loc) · 1.83 KB
/
ash_json_api.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Mix.install(
[
{:ash, "~> 3.0"},
{:ash_json_api, "~> 1.0"},
{:bandit, "~> 1.0"},
{:open_api_spex, "~> 3.16"}
],
consolidate_protocols: false
)
defmodule Accounts.Profile do
use Ash.Resource,
domain: Accounts,
data_layer: Ash.DataLayer.Ets,
extensions: [AshJsonApi.Resource]
json_api do
type "profile"
end
actions do
defaults [:read, :destroy, create: [:name], update: [:name]]
end
attributes do
uuid_primary_key :id
attribute :name, :string
end
end
defmodule Accounts do
use Ash.Domain,
extensions: [AshJsonApi.Domain],
validate_config_inclusion?: false # only necessary in this context because there is no config
json_api do
prefix "/api"
routes do
base_route "/profiles", Accounts.Profile do
index :read
get :read
post :create
patch :update
delete :destroy
end
end
end
resources do
resource Accounts.Profile do
define :all_profiles, action: :read
define :profile_by_id, get_by: [:id], action: :read
define :create_profile, args: [:name], action: :create
define :update_profile, args: [:name], action: :update
define :delete_profile, action: :destroy
end
end
end
defmodule Accounts.JsonApiRouter do
use AshJsonApi.Router,
domains: [Accounts],
json_schema: "/json_schema",
open_api: "/open_api"
end
defmodule Router do
use Plug.Router
plug(Plug.Logger)
plug(:match)
plug(:dispatch)
forward "/api/swaggerui",
to: OpenApiSpex.Plug.SwaggerUI,
init_opts: [
path: "/api/open_api",
default_model_expand_depth: 4
]
forward "/api", to: Accounts.JsonApiRouter
match _ do
send_resp(conn, 404, "not found")
end
end
{:ok, _} = Bandit.start_link(plug: Router, port: 4000)
unless IEx.started?() do
Process.sleep(:infinity)
end