--- /dev/null
+create table phrases (
+ id serial primary key,
+ phrase text not null
+);
+
+insert into phrases (phrase)
+values
+ ('What is the most important step a man can take? The next one, always the next one.'),
+ ('Je comprends ini ce que"on appelle gloire: le droite d"aimer sans mesure.'),
+ ('What the fuck are you talking about GHC?'),
+ ('Merci pour le lait.');
--- /dev/null
+services:
+ db:
+ image: postgres:17
+ restart: unless-stopped
+ environment:
+ POSTGRES_USER: postgres
+ POSTGRES_PASSWORD: postgres
+ POSTGRES_DB: postgres
+ ports:
+ - "5432:5432"
+ volumes:
+ - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
def start(_type, _args) do
children = [
# TODO: Fetch the port from command line
- {DnsPhrases.Server, [listen_port: 3069]}
+ {DnsPhrases.Server, [listen_port: 3069]},
+ {Postgrex, [name: :db]}
]
# See https://hexdocs.pm/elixir/Supervisor.html
--- /dev/null
+defmodule DnsPhrases.Phrases do
+ @spec random() :: String.t()
+ def random() do
+ case Postgrex.query(:db, "select phrase from phrases order by random() limit 1", []) do
+ {:ok, result} ->
+ case result.rows do
+ [[phrase]] -> phrase
+ _ -> "I have nothing to say right now."
+ end
+
+ {:error, _} ->
+ "Am I dead, or is everyone else?"
+ end
+ end
+end
) do
Logger.info("Incoming query from #{inspect(ip)}")
+ phrase = DnsPhrases.Phrases.random()
+
case Parser.parse(data) do
{:ok, msg} ->
- ans = Encoder.encode(%Reply{reply_to: msg, phrase: "Hello, my bro!"})
+ ans = Encoder.encode(%Reply{reply_to: msg, phrase: phrase})
Logger.info("Successfully replied to #{inspect(ip)}")
:gen_udp.send(socket, ip, port, ans)
:error ->
- :gen_udp.send(socket, ip, port, "Invalid DNS message")
+ :gen_udp.send(socket, ip, port, phrase)
end
{:noreply, state}
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
+ {:postgrex, "~> 0.22.0"}
]
end
end
--- /dev/null
+%{
+ "db_connection": {:hex, :db_connection, "2.9.0", "a6a97c5c958a2d7091a58a9be40caf41ab496b0701d21e1d1abff3fa27a7f371", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "17d502eacaf61829db98facf6f20808ed33da6ccf495354a41e64fe42f9c509c"},
+ "decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"},
+ "postgrex": {:hex, :postgrex, "0.22.0", "fb027b58b6eab1f6de5396a2abcdaaeb168f9ed4eccbb594e6ac393b02078cbd", [:mix], [{:db_connection, "~> 2.9", [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", "a68c4261e299597909e03e6f8ff5a13876f5caadaddd0d23af0d0a61afcc5d84"},
+ "telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
+}