Remove ecto repo, add writing to file
This commit is contained in:
parent
386a331956
commit
ce463329f6
20 changed files with 98 additions and 122 deletions
|
|
@ -5,14 +5,11 @@ defmodule BirdyChat.Application do
|
|||
|
||||
use Application
|
||||
|
||||
@env Mix.env
|
||||
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
children = [
|
||||
BirdyChatWeb.Telemetry,
|
||||
BirdyChat.Repo,
|
||||
{BirdyChat.Identity, env: @env},
|
||||
BirdyChat.Identity,
|
||||
{DNSCluster, query: Application.get_env(:birdy_chat, :dns_cluster_query) || :ignore},
|
||||
{Phoenix.PubSub, name: BirdyChat.PubSub},
|
||||
# Start a worker by calling: BirdyChat.Worker.start_link(arg)
|
||||
|
|
|
|||
|
|
@ -32,8 +32,7 @@ defmodule BirdyChat.Identity do
|
|||
def parse_peers(peers) do
|
||||
values = String.split(peers, ";")
|
||||
|
||||
for value <- values,
|
||||
[identity, address] = String.split(value, "::"), into: %{} do
|
||||
for value <- values, [identity, address] = String.split(value, "::"), into: %{} do
|
||||
{identity, address}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -19,28 +19,4 @@ defmodule BirdyChat.Message do
|
|||
{:error, changeset}
|
||||
end
|
||||
end
|
||||
|
||||
def write(%{to: to, from: from, message: message} = msg) do
|
||||
message_to_write = "#{from}: #{message}\n"
|
||||
:ok = create_messages_folder!(Application.app_dir(:birdy_chat, ["priv", "messages"]))
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages", "#{to}.txt"])
|
||||
result = File.write!(path, message_to_write, [:append])
|
||||
{:ok, msg}
|
||||
end
|
||||
|
||||
def create_messages_folder!(path) do
|
||||
case File.mkdir(path) do
|
||||
:ok ->
|
||||
:ok
|
||||
|
||||
{:error, :eexist} ->
|
||||
:ok
|
||||
|
||||
{:error, reason} ->
|
||||
raise File.Error,
|
||||
reason: reason,
|
||||
action: "make directory",
|
||||
path: IO.chardata_to_string(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
16
lib/birdy_chat/message_writer.ex
Normal file
16
lib/birdy_chat/message_writer.ex
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
defmodule BirdyChat.MessageWriter do
|
||||
@moduledoc false
|
||||
|
||||
@spec write(%{to: String.t(), from: String.t(), message: String.t()}) :: :ok
|
||||
def write(message) do
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages"])
|
||||
write(path, message)
|
||||
end
|
||||
|
||||
@spec write(String.t(), %{to: String.t(), from: String.t(), message: String.t()}) :: :ok
|
||||
def write(path, %{to: to, from: from, message: message}) do
|
||||
message_to_write = "#{from}: #{message}\n"
|
||||
path = Path.join([path, "#{to}.txt"])
|
||||
:ok = File.write!(path, message_to_write, [:append])
|
||||
end
|
||||
end
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
defmodule BirdyChat.Repo do
|
||||
use Ecto.Repo,
|
||||
otp_app: :birdy_chat,
|
||||
adapter: Ecto.Adapters.Postgres
|
||||
end
|
||||
|
|
@ -4,11 +4,11 @@ defmodule BirdyChatWeb.Api.Messages.Controller do
|
|||
def create(conn, params) do
|
||||
case BirdyChat.Message.validate(params) do
|
||||
{:ok, changeset} ->
|
||||
case BirdyChat.Message.write(changeset.changes) do
|
||||
{:ok, msg} ->
|
||||
case BirdyChat.MessageWriter.write(changeset.changes) do
|
||||
:ok ->
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> render(:create, message: msg)
|
||||
|> render(:create, message: changeset.changes)
|
||||
end
|
||||
|
||||
{:error, changeset} ->
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ defmodule BirdyChatWeb.Endpoint do
|
|||
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
|
||||
plug Phoenix.LiveReloader
|
||||
plug Phoenix.CodeReloader
|
||||
plug Phoenix.Ecto.CheckRepoStatus, otp_app: :birdy_chat
|
||||
end
|
||||
|
||||
plug Phoenix.LiveDashboard.RequestLogger,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue