commit 2bdec198d864b805657d6774b86599f2c022845e Author: optclblast Date: Fri Jan 3 16:50:52 2025 -0800 init diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa62fc4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +warden-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..c6b9a49 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Warden + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `warden` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:warden, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at . + diff --git a/lib/server/connection/conn.ex b/lib/server/connection/conn.ex new file mode 100644 index 0000000..bebb2bd --- /dev/null +++ b/lib/server/connection/conn.ex @@ -0,0 +1,34 @@ +defmodule Server.Conn do + require Logger + use GenServer + + @impl true + def init(_args) do + initial_state = %{"num_conns" => 0} + {:ok, initial_state} + end + + @impl true + def handle_call(_socket, _from, _state) do + throw("call is unimplementedd") + end + + @impl true + def handle_cast(socket, state) do + spawn(handle(socket)) + + %{"num_conns" => state.num_conns + 1} + end + + defp handle(socket) do + case :gen_tcp.recv(socket, 1024) do + {:ok, packet} -> + Logger.debug("[Server.Conn] packet #{packet}") + {:error, reason} -> + Logger.error("[Server.Conn] failed to read connection: #{reason}") + + end + + handle(socket) + end +end diff --git a/lib/server/server.ex b/lib/server/server.ex new file mode 100644 index 0000000..09bcd9e --- /dev/null +++ b/lib/server/server.ex @@ -0,0 +1,17 @@ +defmodule Server do + def start(addr) do + accept_loop(addr) + end + + defp accept_loop(addr) do + case :gen_tcp.accept(addr) do + {:ok, socket} -> + accept(socket) + end + accept_loop(addr) + end + + defp accept(socket) do + send(Server.Conn, socket) + end +end diff --git a/lib/warden.ex b/lib/warden.ex new file mode 100644 index 0000000..00239d5 --- /dev/null +++ b/lib/warden.ex @@ -0,0 +1,10 @@ +defmodule Warden do + use Application + + def start(_type, _args) do + children = [ + Server, + ] + Supervisor.start_link(children, strategy: :one_for_one) + end +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..5484cf0 --- /dev/null +++ b/mix.exs @@ -0,0 +1,29 @@ +defmodule Warden.MixProject do + use Mix.Project + + def project do + [ + app: :warden, + version: "0.1.0", + elixir: "~> 1.18", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + mod: {Warden, []}, + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/test/warden_test.exs b/test/warden_test.exs new file mode 100644 index 0000000..019be13 --- /dev/null +++ b/test/warden_test.exs @@ -0,0 +1,8 @@ +defmodule WardenTest do + use ExUnit.Case + doctest Warden + + test "greets the world" do + assert Warden.hello() == :world + end +end