]> git.frustrated-labs.net Git - dns-phrasex.git/commitdiff
feat: connect to postgres to fetch phrases
authorAlexander Goussas <[email protected]>
Mon, 2 Feb 2026 03:23:55 +0000 (22:23 -0500)
committerAlexander Goussas <[email protected]>
Mon, 2 Feb 2026 03:23:55 +0000 (22:23 -0500)
database/init.sql [new file with mode: 0644]
docker-compose.yml [new file with mode: 0644]
lib/dns_phrases/application.ex
lib/dns_phrases/phrases.ex [new file with mode: 0644]
lib/dns_phrases/server.ex
mix.exs
mix.lock [new file with mode: 0644]

diff --git a/database/init.sql b/database/init.sql
new file mode 100644 (file)
index 0000000..848593b
--- /dev/null
@@ -0,0 +1,11 @@
+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.');
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644 (file)
index 0000000..31770c5
--- /dev/null
@@ -0,0 +1,12 @@
+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
index 75d94fdb16fc7d757d8e9e74f031b4f32f5def55..7e36f2af14e2a4d3f7e3dfe1da23ff2422e3abe2 100644 (file)
@@ -9,7 +9,8 @@ defmodule DnsPhrases.Application do
   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
diff --git a/lib/dns_phrases/phrases.ex b/lib/dns_phrases/phrases.ex
new file mode 100644 (file)
index 0000000..61c29f3
--- /dev/null
@@ -0,0 +1,15 @@
+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
index c787e1e60e9c4cf26f2c9f674847834663f3ba44..9263deba4c3d2b065b93372867ebfedf979cde4f 100644 (file)
@@ -29,14 +29,16 @@ defmodule DnsPhrases.Server do
       ) 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}
diff --git a/mix.exs b/mix.exs
index d7c6c26ed4ebc846fb0aa47266cedf6c0c14f23c..283ae01b69f8262a958ea4ce1a47b018a420eacd 100644 (file)
--- a/mix.exs
+++ b/mix.exs
@@ -24,6 +24,7 @@ defmodule DnsPhrases.MixProject do
     [
       # {: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
diff --git a/mix.lock b/mix.lock
new file mode 100644 (file)
index 0000000..d24ee16
--- /dev/null
+++ b/mix.lock
@@ -0,0 +1,6 @@
+%{
+  "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"},
+}