Skip to content

Commit

Permalink
step up dev
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed Aug 29, 2017
1 parent 5666c67 commit 6b10448
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/water_cooler/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule WaterCooler.Application do
children = [
# worker(WaterCooler.WWW, [[port: port()]], id: :http),
# worker(WaterCooler.WWW, [[port: secure_port(), tls: tls_options]], id: :https),
supervisor(Ace.HTTP2.Service, [{WaterCooler.WWW, []}, options]),
supervisor(Ace.HTTP2.Service, [{Ace.HTTP2.Worker, [{WaterCooler.WWW, :config}]}, options]),
worker(WaterCooler.DNSDiscovery, [System.get_env("SERVICE_NAME"), options])
]

Expand Down
91 changes: 83 additions & 8 deletions lib/water_cooler/www.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,33 @@ defmodule Raxx.Blueprint do
actions = blueprint_to_actions(blueprint)

routing_ast = for {method, path, module} <- actions do
path = path_template_to_match(path)
quote do
def handle_headers(request = %{method: unquote(method), path: unquote(path)}, state) do
# DEBT live in a state, needs message monad
Process.put({Raxx.Blueprint, :handler}, module)
Process.put({Raxx.Blueprint, :handler}, unquote(module))
return = unquote(module).handle_headers(request, state)
end
end
end

quote do
use Raxx.App
unquote(routing_ast)

def handle_fragment(fragment, state) do
module = Process.put({Raxx.Blueprint, :handler})
unquote(module).handle_fragment(fragment, state)
module = Process.get({Raxx.Blueprint, :handler})
module.handle_fragment(fragment, state)
end

def handle_trailers(trailers, state) do
module = Process.put({Raxx.Blueprint, :handler})
unquote(module).handle_trailers(trailers, state)
module = Process.get({Raxx.Blueprint, :handler})
module.handle_trailers(trailers, state)
end

def handle_info(info, state) do
module = Process.get({Raxx.Blueprint, :handler})
module.handle_info(info, state)
end
end
end
Expand All @@ -43,7 +50,7 @@ defmodule Raxx.Blueprint do
defp path_template_to_match(path_template) do
path_template
|> Raxx.Request.split_path()
|> template_segment_to_match()
|> Enum.map(&template_segment_to_match/1)
end

defp template_segment_to_match(segment) do
Expand All @@ -57,14 +64,82 @@ defmodule Raxx.Blueprint do
end

defmodule WaterCooler.WWW do
use GenServer
defmodule HomePage do
use Raxx.App

require EEx

EEx.function_from_file(:defp, :home_page, Path.join(__DIR__, "./templates/home_page.html.eex"), [])

def handle_headers(request, config) do
body = home_page()
Raxx.Response.new(:ok, [{"content-type", "text/html"}], body)
end
end

defmodule SubscribeToMessages do
use Raxx.App

alias WaterCooler.ChatRoom
require ChatRoom

def handle_headers(request, config) do
{:ok, _} = ChatRoom.join()
response = Raxx.Response.new(:ok, [{"content-type", "text/event-stream"}], true)
response
end
end

defmodule PublishMessage do
use Raxx.App

alias WaterCooler.ChatRoom
require ChatRoom

def handle_headers(request, config) do
{[], {:reading, ""}}
end

def handle_fragment(fragment, {:reading, buffer}) do
{[], {:reading, buffer <> fragment}}
end

def handle_trailers([], {:reading, body}) do
{:ok, %{message: message}} = parse_publish_form(body)
{:ok, _} = ChatRoom.publish(message)
response = Raxx.Response.new(303, [{"location", "/"}], false)
end

def parse_publish_form(raw) do
%{"message" => message} = URI.decode_www_form(raw) |> URI.decode_query
{:ok, %{message: message}}
end

end
def PublishMessage do
use Raxx.Unary

def handle_request(request, config) do
{:ok, %{message: message}} = parse_publish_form(request.body)
{:ok, _} = ChatRoom.publish(message)
Raxx.Response.new(303, [{"location", "/"}], false)
end
end
use Raxx.Blueprint, [
{"/", [
GET: HomePage,
POST: PublishMessage]},
{"/updates", [
GET: SubscribeToMessages
]}
]

alias WaterCooler.ChatRoom
require ChatRoom

require EEx

EEx.function_from_file(:defp, :home_page, Path.join(__DIR__, "./templates/home_page.html.eex"), [])

EEx.function_from_file(:defp, :not_found, Path.join(__DIR__, "./templates/not_found.html.eex"), [])

def start_link() do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule WaterCooler.Mixfile do

defp deps do
[
{:ace, "~> 0.13.0"},
{:ace, github: "crowdhailer/ace", ref: "f634352de73cbf5bddee21814c36f2a244f750f4"},
{:server_sent_event, "~> 0.2.0"},
{:exsync, "~> 0.2.0"},
{:wobserver, "~> 0.1.8"}
Expand Down
6 changes: 3 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
%{"ace": {:hex, :ace, "0.13.0", "f135fbbb24261ff820554fdd655fc77ae7059748cd651a4626493fe75d7d0c80", [:mix], [{:hpack, "~> 0.2.3", [hex: :hpack_erl, repo: "hexpm", optional: false]}, {:raxx, "~> 0.11.1", [hex: :raxx, repo: "hexpm", optional: false]}], "hexpm"},
%{"ace": {:git, "https://github.com/crowdhailer/ace.git", "f634352de73cbf5bddee21814c36f2a244f750f4", [ref: "f634352de73cbf5bddee21814c36f2a244f750f4"]},
"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [], [], "hexpm"},
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [], [], "hexpm"},
"exsync": {:hex, :exsync, "0.2.0", "a460c7c1182a943186bc0b7342a4ceda6955c88549106f13fa58df32b6f7408a", [], [{:file_system, "~> 0.1.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm"},
"file_system": {:hex, :file_system, "0.1.5", "4f20ec7d186cd84ad478bd5477061aeb993b36b5458872041bbabba8b09d36ff", [], [], "hexpm"},
"hackney": {:hex, :hackney, "1.9.0", "51c506afc0a365868469dcfc79a9d0b94d896ec741cfd5bd338f49a5ec515bfe", [], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"hpack": {:hex, :hpack_erl, "0.2.3", "17670f83ff984ae6cd74b1c456edde906d27ff013740ee4d9efaa4f1bf999633", [], [], "hexpm"},
"http_status": {:hex, :http_status, "0.2.1", "74fa942b5874966a25088162e5297ad52dd5e6a29b88ad4a73953aa55e58e3bb", [], [], "hexpm"},
"http_status": {:hex, :http_status, "0.2.2", "9710b536e42aaf2eedf1a5c3543b16dc459088dcfa41f80998ea2ba3715f35e6", [], [], "hexpm"},
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], [], "hexpm"},
Expand All @@ -15,7 +15,7 @@
"plug": {:hex, :plug, "1.4.3", "236d77ce7bf3e3a2668dc0d32a9b6f1f9b1f05361019946aae49874904be4aed", [], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [], [], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [], [], "hexpm"},
"raxx": {:hex, :raxx, "0.11.1", "9e2e8955fae9ecd33dc0a6d9cd50d083b209f3c859c548db0197ce39b9247146", [], [{:http_status, "~> 0.2", [hex: :http_status, repo: "hexpm", optional: false]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.2", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"raxx": {:git, "https://github.com/crowdhailer/raxx.git", "e6ecd5b6450211138e07d9a3338e4d55128f018d", [ref: "e6ecd5b6450211138e07d9a3338e4d55128f018d"]},
"server_sent_event": {:hex, :server_sent_event, "0.2.1", "4aa61ad29355351872cf9742269b75ecfb908bf64bad830dc4010d815037f9c7", [], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [], [], "hexpm"},
Expand Down

0 comments on commit 6b10448

Please sign in to comment.