tmp, rreuqest and related decoding behaviour implemented. need to implement repository interaction logic

This commit is contained in:
r8zavetr8v 2024-09-08 15:35:20 +03:00
parent 52c5a88fb5
commit 6ac974f1e3
16 changed files with 134 additions and 67 deletions

11
Makefile Normal file
View File

@ -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

View File

@ -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/

View File

@ -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:

View File

@ -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

View File

@ -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")

View File

@ -8,8 +8,13 @@
<%= assigns[:page_title] || "DrainCloudCore" %>
</.live_title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script src="https://unpkg.com/htmx.org@2.0.2" integrity="sha384-Y7hw+L/jvKeWIRRkqWYfPcvVxHzVzn5REgzbawhxAuQGwX1XWe70vji+VSeHOThJ" crossorigin="anonymous"></script>
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
<script
src="https://unpkg.com/htmx.org@2.0.2"
integrity="sha384-Y7hw+L/jvKeWIRRkqWYfPcvVxHzVzn5REgzbawhxAuQGwX1XWe70vji+VSeHOThJ"
crossorigin="anonymous"
>
</script>
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>
<body class="bg-white">

View File

@ -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

View File

@ -7,5 +7,4 @@ defmodule DrainCloudCoreWeb.MainController do
|> put_root_layout(false)
|> json(%{data: %{name: "Some Name"}})
end
end

View File

@ -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

View File

@ -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",

View File

@ -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"},

View File

@ -1,7 +0,0 @@
defmodule DrainCloudCore.Repo.Migrations.Users do
use Ecto.Migration
def change do
# TODO users table migration
end
end

View File

@ -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

View File

@ -1,5 +0,0 @@
create table users (
id bigserial,
login varchar(50),
password varchar(256)
);

View File

@ -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

View File

@ -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