Skip to content

Commit

Permalink
boilerplate layout 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewhrit committed Aug 20, 2024
1 parent 42098fd commit 677f20b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
21 changes: 21 additions & 0 deletions lib/redeyes.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Redeyes.Router do
use Plug.Router

plug(Plug.Logger)
plug(:match)
plug(:dispatch)

post "/shorten" do
conn
|> send_resp(200, "Hello, world!")
end

get "/fetch" do
conn
|> send_resp(200, "Hello, world!")
end

match _ do
send_resp(conn, 404, "Not found")
end
end
19 changes: 19 additions & 0 deletions lib/redeyes/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Redeyes.Application do
@moduledoc false

use Application

@impl true
def start(_type, _args) do
children = [
Plug.Cowboy.child_spec(
scheme: :http,
plug: Redeyes.Router,
options: [port: 4001]
)
]

opts = [strategy: :one_for_one, name: Redeyes.Supervisor]
Supervisor.start_link(children, opts)
end
end
28 changes: 28 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Redeyes.MixProject do
use Mix.Project

def project do
[
app: :redeyes,
version: "0.1.0",
elixir: "~> 1.17",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
mod: {Redeyes.Application, []}
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:plug_cowboy, "~> 2.0"}
]
end
end
11 changes: 11 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%{
"cowboy": {:hex, :cowboy, "2.12.0", "f276d521a1ff88b2b9b4c54d0e753da6c66dd7be6c9fca3d9418b561828a3731", [:make, :rebar3], [{:cowlib, "2.13.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "8a7abe6d183372ceb21caa2709bec928ab2b72e18a3911aa1771639bef82651e"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.13.0", "db8f7505d8332d98ef50a3ef34b34c1afddec7506e4ee4dd4a3a266285d282ca", [:make, :rebar3], [], "hexpm", "e1e1284dc3fc030a64b1ad0d8382ae7e99da46c3246b815318a4b848873800a4"},
"mime": {:hex, :mime, "2.0.6", "8f18486773d9b15f95f4f4f1e39b710045fa1de891fada4516559967276e4dc2", [:mix], [], "hexpm", "c9945363a6b26d747389aac3643f8e0e09d30499a138ad64fe8fd1d13d9b153e"},
"plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"},
"plug_cowboy": {:hex, :plug_cowboy, "2.7.1", "87677ffe3b765bc96a89be7960f81703223fe2e21efa42c125fcd0127dd9d6b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "02dbd5f9ab571b864ae39418db7811618506256f6d13b4a45037e5fe78dc5de3"},
"plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
}
45 changes: 45 additions & 0 deletions test/redeyes_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule RedeyesTest do
use ExUnit.Case, async: true
use Plug.Test

@opts Redeyes.Router.init([])

test "/fetch returns hello world" do
# Create a test connection
conn = conn(:get, "/fetch")

# Invoke the plug
conn = Redeyes.Router.call(conn, @opts)

# Assert the response and status
assert conn.state == :sent
assert conn.status == 200
assert conn.resp_body == "Hello, world!"
end

test "/shorten returns hello world" do
# Create a test connection
conn = conn(:post, "/shorten")

# Invoke the plug
conn = Redeyes.Router.call(conn, @opts)

# Assert the response and status
assert conn.state == :sent
assert conn.status == 200
assert conn.resp_body == "Hello, world!"
end

test "missing page returns 404" do
# Create a test connection
conn = conn(:get, "/missing")

# Invoke the plug
conn = Redeyes.Router.call(conn, @opts)

# Assert the response and status
assert conn.state == :sent
assert conn.status == 404
assert conn.resp_body == "Not found"
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit 677f20b

Please sign in to comment.