From 6ac974f1e3215b9344fc8ce10652265bef7a2738 Mon Sep 17 00:00:00 2001 From: optclblast Date: Sun, 8 Sep 2024 15:35:20 +0300 Subject: [PATCH] tmp, rreuqest and related decoding behaviour implemented. need to implement repository interaction logic --- Makefile | 11 +++++ README.md | 7 +++ docker-compose.yaml | 36 +++++++-------- lib/draincloud_core/auth/users.ex | 18 ++++++++ lib/draincloud_core/rtc.ex | 45 +++++++++++-------- .../components/layouts/root.html.heex | 9 +++- .../auth_controller/auth_controller.ex | 29 +++++++++--- .../main_controller/main_controller.ex | 1 - lib/draincloud_core_web/router.ex | 9 +--- mix.exs | 1 + mix.lock | 1 + priv/repo/migrations/20240901221317_users.exs | 7 --- .../migrations/20240908091209_add_users.exs | 15 +++++++ schemas/users.sql | 5 --- .../controllers/error_html_test.exs | 3 +- .../controllers/error_json_test.exs | 4 +- 16 files changed, 134 insertions(+), 67 deletions(-) create mode 100644 Makefile create mode 100644 lib/draincloud_core/auth/users.ex delete mode 100644 priv/repo/migrations/20240901221317_users.exs create mode 100644 priv/repo/migrations/20240908091209_add_users.exs delete mode 100644 schemas/users.sql diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..86a113e --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +up: + sudo docker compose up -d + make migrate-up + mix phx.server + +migrate-up: + mix ecto.migrate + +migrate-down: + mix ecto.rollback + diff --git a/README.md b/README.md index 0b50ae0..b420bcb 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,13 @@ Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html). +### Dev DB creds +From *./config/dev.exs* +Host: localhost:5432 +Database: draincloud_core_dev +Username: postgres +Password: postgres + ### Some phoenix related links * Official website: https://www.phoenixframework.org/ diff --git a/docker-compose.yaml b/docker-compose.yaml index 5f06def..d2bf9b9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -9,24 +9,24 @@ services: ports: - 5432:5432 - minio: - image: quay.io/minio/minio:RELEASE.2024-08-29T01-40-52Z - command: server --console-address ":9001" http://minio/data{1...2} - hostname: minio - volumes: - - data-1:/data1 - - data-2:/data2 - expose: - - "9000" - - "9001" - environment: - MINIO_ROOT_USER: minioadmin - MINIO_ROOT_PASSWORD: minioadmin - healthcheck: - test: ["CMD", "mc", "ready", "local"] - interval: 5s - timeout: 5s - retries: 5 + # minio: + # image: quay.io/minio/minio:RELEASE.2024-08-29T01-40-52Z + # command: server --console-address ":9001" http://minio/data{1...2} + # hostname: minio + # volumes: + # - data-1:/data1 + # - data-2:/data2 + # expose: + # - "9000" + # - "9001" + # environment: + # MINIO_ROOT_USER: minioadmin + # MINIO_ROOT_PASSWORD: minioadmin + # healthcheck: + # test: ["CMD", "mc", "ready", "local"] + # interval: 5s + # timeout: 5s + # retries: 5 volumes: draincore-data: diff --git a/lib/draincloud_core/auth/users.ex b/lib/draincloud_core/auth/users.ex new file mode 100644 index 0000000..0f8229e --- /dev/null +++ b/lib/draincloud_core/auth/users.ex @@ -0,0 +1,18 @@ +defmodule DrainCloudCore.Auth.Users do + use Ecto.Schema + import Ecto.Changeset + + schema "users" do + field :login, :string + field :passwoed, :string, redact: true + field :crated_at, :utc_datetime + field :updated_at, :utc_datetime + field :deleted_at, :utc_datetime + end + + def changeset(user, params \\ %{}) do + user + |> cast(params, [:id, :login, :password, :updated_at, :deleted_at]) + |> validate_required([:id]) + end +end diff --git a/lib/draincloud_core/rtc.ex b/lib/draincloud_core/rtc.ex index 22e62f5..21aa64a 100644 --- a/lib/draincloud_core/rtc.ex +++ b/lib/draincloud_core/rtc.ex @@ -10,6 +10,7 @@ defmodule DrainCloudCore.Rtc.Application do type: :supervisor } end + def start_link(_) do children = [ DrainCloudCore.Rtc @@ -27,27 +28,32 @@ defmodule DrainCloudCore.Rtc do use GenServer # Real-time config table schemas - @config_web [attributes: [ - :host, - :enable_https, - :port, - ]] + @config_web [ + attributes: [ + :host, + :enable_https, + :port + ] + ] - @config_pg [attributes: [ - :host, - :port, - :ssl, - :user, - :password, - :db, - ]] + @config_pg [ + attributes: [ + :host, + :port, + :ssl, + :user, + :password, + :db + ] + ] - - @config_s3 [attributes: [ - :host, - :port, - # user / secret / secrets etc... - ]] + @config_s3 [ + attributes: [ + :host, + :port + # user / secret / secrets etc... + ] + ] # def child_spec(opts) do # %{ @@ -81,6 +87,7 @@ defmodule DrainCloudCore.Rtc do def fetch_config() do # Mnesia.read() end + def init() do Log.info("[#{__MODULE__}] start initializin RTC subsystems") diff --git a/lib/draincloud_core_web/components/layouts/root.html.heex b/lib/draincloud_core_web/components/layouts/root.html.heex index 90e81b6..f4782bc 100644 --- a/lib/draincloud_core_web/components/layouts/root.html.heex +++ b/lib/draincloud_core_web/components/layouts/root.html.heex @@ -8,8 +8,13 @@ <%= assigns[:page_title] || "DrainCloudCore" %> - - + diff --git a/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex index e8e681b..dcb0b24 100644 --- a/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex +++ b/lib/draincloud_core_web/controllers/auth_controller/auth_controller.ex @@ -1,12 +1,29 @@ defmodule DrainCloudCoreWeb.AuthController do use DrainCloudCoreWeb, :controller - alias DrainCloudCoreWeb.Request, as: Request - - def logon(conn, _params) do - if Request.hs_token?(conn) do - # TODO validate token here - end + alias DrainCloudCoreWeb.AuthController.RegisterRequest, as: Request + + def register(conn, _params) do + req = Request.from_request(conn) + :logger.warning(req) + json(conn, req) + end +end + +defmodule DrainCloudCoreWeb.AuthController.RegisterRequest do + @derive [Poison.Encoder, Jason.Encoder] + defstruct login: "", password: "" + + alias DrainCloudCoreWeb.AuthController.RegisterRequest, as: RegisterRequest + def from_request(conn) do + res = Plug.Conn.read_body(conn) + + case res do + {:error, _} -> raise "failed to read request body: #{:error}" + {_, data, _} -> + Poison.decode!(data, as: %RegisterRequest{}) + _ -> raise "failed to read request body. uexpected result: #{res}" + end end end diff --git a/lib/draincloud_core_web/controllers/main_controller/main_controller.ex b/lib/draincloud_core_web/controllers/main_controller/main_controller.ex index 7a93c37..6e2f0da 100644 --- a/lib/draincloud_core_web/controllers/main_controller/main_controller.ex +++ b/lib/draincloud_core_web/controllers/main_controller/main_controller.ex @@ -7,5 +7,4 @@ defmodule DrainCloudCoreWeb.MainController do |> put_root_layout(false) |> json(%{data: %{name: "Some Name"}}) end - end diff --git a/lib/draincloud_core_web/router.ex b/lib/draincloud_core_web/router.ex index 9ed3551..b787062 100644 --- a/lib/draincloud_core_web/router.ex +++ b/lib/draincloud_core_web/router.ex @@ -14,17 +14,12 @@ defmodule DrainCloudCoreWeb.Router do plug :accepts, ["json"] end - scope "/", DrainCloudCoreWeb do + scope "/api", DrainCloudCoreWeb do pipe_through :api - get "/api", MainController, :test + post "/register", AuthController, :register end - # Other scopes may use custom stacks. - # scope "/api", DrainCloudCoreWeb do - # pipe_through :api - # end - # Enable LiveDashboard in development if Application.compile_env(:draincloud_core, :dev_routes) do # If you want to use the LiveDashboard in production, you should put diff --git a/mix.exs b/mix.exs index 54fa208..48c6cd5 100644 --- a/mix.exs +++ b/mix.exs @@ -44,6 +44,7 @@ defmodule DrainCloudCore.MixProject do {:phoenix_live_dashboard, "~> 0.8.3"}, {:esbuild, "~> 0.8", runtime: Mix.env() == :dev}, {:tailwind, "~> 0.2", runtime: Mix.env() == :dev}, + {:poison, "~> 6.0"}, {:heroicons, github: "tailwindlabs/heroicons", tag: "v2.1.1", diff --git a/mix.lock b/mix.lock index 04c3e61..13d2b03 100644 --- a/mix.lock +++ b/mix.lock @@ -27,6 +27,7 @@ "plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"}, "plug_cowboy": {:hex, :plug_cowboy, "2.7.1", "87677ffe3b765bc96a89be7960f81703223fe2e21efa42c125fcd0127dd9d6b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "02dbd5f9ab571b864ae39418db7811618506256f6d13b4a45037e5fe78dc5de3"}, "plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"}, + "poison": {:hex, :poison, "6.0.0", "9bbe86722355e36ffb62c51a552719534257ba53f3271dacd20fbbd6621a583a", [:mix], [{:decimal, "~> 2.1", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "bb9064632b94775a3964642d6a78281c07b7be1319e0016e1643790704e739a2"}, "postgrex": {:hex, :postgrex, "0.19.1", "73b498508b69aded53907fe48a1fee811be34cc720e69ef4ccd568c8715495ea", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "8bac7885a18f381e091ec6caf41bda7bb8c77912bb0e9285212829afe5d8a8f8"}, "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, "tailwind": {:hex, :tailwind, "0.2.3", "277f08145d407de49650d0a4685dc062174bdd1ae7731c5f1da86163a24dfcdb", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "8e45e7a34a676a7747d04f7913a96c770c85e6be810a1d7f91e713d3a3655b5d"}, diff --git a/priv/repo/migrations/20240901221317_users.exs b/priv/repo/migrations/20240901221317_users.exs deleted file mode 100644 index 8395c5a..0000000 --- a/priv/repo/migrations/20240901221317_users.exs +++ /dev/null @@ -1,7 +0,0 @@ -defmodule DrainCloudCore.Repo.Migrations.Users do - use Ecto.Migration - - def change do - # TODO users table migration - end -end diff --git a/priv/repo/migrations/20240908091209_add_users.exs b/priv/repo/migrations/20240908091209_add_users.exs new file mode 100644 index 0000000..11df9b1 --- /dev/null +++ b/priv/repo/migrations/20240908091209_add_users.exs @@ -0,0 +1,15 @@ +defmodule DrainCloudCore.Repo.Migrations.AddUsers do + use Ecto.Migration + + def change do + create table(:users) do + add :login, :string, [size: 120] + add :password, :string + add :created_at, :utc_datetime, [null: false, default: fragment("CURRENT_TIMESTAMP")] + add :updated_at, :utc_datetime, [null: false, default: fragment("CURRENT_TIMESTAMP")] + add :deleted_at, :utc_datetime, [null: true, default: nil] + end + + create unique_index(:users, :login) + end +end diff --git a/schemas/users.sql b/schemas/users.sql deleted file mode 100644 index c2ae31e..0000000 --- a/schemas/users.sql +++ /dev/null @@ -1,5 +0,0 @@ -create table users ( - id bigserial, - login varchar(50), - password varchar(256) -); \ No newline at end of file diff --git a/test/draincloud_core_web/controllers/error_html_test.exs b/test/draincloud_core_web/controllers/error_html_test.exs index 73599fb..d24cdc6 100644 --- a/test/draincloud_core_web/controllers/error_html_test.exs +++ b/test/draincloud_core_web/controllers/error_html_test.exs @@ -9,6 +9,7 @@ defmodule DrainCloudCoreWeb.ErrorHTMLTest do end test "renders 500.html" do - assert render_to_string(DrainCloudCoreWeb.ErrorHTML, "500", "html", []) == "Internal Server Error" + assert render_to_string(DrainCloudCoreWeb.ErrorHTML, "500", "html", []) == + "Internal Server Error" end end diff --git a/test/draincloud_core_web/controllers/error_json_test.exs b/test/draincloud_core_web/controllers/error_json_test.exs index 20aa6b0..a44ef0b 100644 --- a/test/draincloud_core_web/controllers/error_json_test.exs +++ b/test/draincloud_core_web/controllers/error_json_test.exs @@ -2,7 +2,9 @@ defmodule DrainCloudCoreWeb.ErrorJSONTest do use DrainCloudCoreWeb.ConnCase, async: true test "renders 404" do - assert DrainCloudCoreWeb.ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}} + assert DrainCloudCoreWeb.ErrorJSON.render("404.json", %{}) == %{ + errors: %{detail: "Not Found"} + } end test "renders 500" do