forked from dynamo/dynamo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Run this app with: | ||
# | ||
# elixir -pa ebin -pa deps/misultin/ebin examples/simple.exs | ||
|
||
defmodule MyApp do | ||
use Dynamo::App | ||
|
||
get "/foo/bar" do | ||
|
||
end | ||
end | ||
|
||
MyApp.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
defmodule Dynamo do | ||
def run(name, options) do | ||
options = Orddict.merge default_options_for(name), options | ||
IO.puts "Running #{Orddict.fetch(options, :name, nil)} on #{Orddict.fetch(options, :port, nil)}" | ||
end | ||
|
||
defp default_options_for(name) do | ||
[port: 3000, name: name] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule Dynamo::App do | ||
# Hook invoked when Dynamo::App is used. | ||
# It initializes the app data, registers a | ||
# compile callback and import Dynamo::DSL macros. | ||
defmacro __using__(module) do | ||
Module.merge_data module, routes: [] | ||
Module.add_compile_callback module, __MODULE__ | ||
quote { require Dynamo::DSL, import: true } | ||
end | ||
|
||
defmacro __compiling__(module) do | ||
# Compile routes | ||
routes = Orddict.fetch Module.read_data(module), :routes, [] | ||
Dynamo::Router.compile(module, routes) | ||
|
||
# Clean up any internal state | ||
# TODO: Use Module.remove_data once we add it to Elixir | ||
Module.merge_data module, routes: [] | ||
|
||
# Generate both an service entry points | ||
quote do | ||
def run(options // []) do | ||
Dynamo.run(__MODULE__, options) | ||
end | ||
|
||
def service(request, response) do | ||
path = request.path | ||
verb = request.request_method | ||
case recognize_route(verb, path, []) do | ||
match: { :ok, fun, _ } | ||
apply __MODULE__, fun, [request, response] | ||
match: :error | ||
IO.puts "SERVE 404" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Dynamo::DSL do | ||
defmacro route(verb, path, contents) do | ||
list_path = to_list(path) | ||
bin_path = to_binary(path) | ||
|
||
quote do | ||
path = unquote(list_path) | ||
routes = Orddict.fetch Module.read_data(__MODULE__), :routes, [] | ||
name = :"_action_#{unquote(bin_path)}_#{length(routes)}" | ||
Module.merge_data __MODULE__, routes: [{ path, {unquote(verb), name} }|routes] | ||
def name, [request, response], [], unquote(contents) | ||
end | ||
end | ||
|
||
defmacro get(path, contents) do | ||
route(:GET, path, contents) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters