From: Alexander Goussas Date: Mon, 2 Feb 2026 03:23:55 +0000 (-0500) Subject: feat: connect to postgres to fetch phrases X-Git-Url: http://git.frustrated-labs.net/?a=commitdiff_plain;h=64bac1b8fd63fef94f4bdf41fb069bf15913a773;p=dns-phrasex.git feat: connect to postgres to fetch phrases --- diff --git a/database/init.sql b/database/init.sql new file mode 100644 index 0000000..848593b --- /dev/null +++ b/database/init.sql @@ -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 index 0000000..31770c5 --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/lib/dns_phrases/application.ex b/lib/dns_phrases/application.ex index 75d94fd..7e36f2a 100644 --- a/lib/dns_phrases/application.ex +++ b/lib/dns_phrases/application.ex @@ -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 index 0000000..61c29f3 --- /dev/null +++ b/lib/dns_phrases/phrases.ex @@ -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 diff --git a/lib/dns_phrases/server.ex b/lib/dns_phrases/server.ex index c787e1e..9263deb 100644 --- a/lib/dns_phrases/server.ex +++ b/lib/dns_phrases/server.ex @@ -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 d7c6c26..283ae01 100644 --- 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 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"}, +}