Permalink
Browse files

Running configuration should come from the app

  • Loading branch information...
1 parent 4c43eb9 commit 8dc2a85eb6469b430e713b7cd07e95235e612637 @josevalim josevalim committed Sep 6, 2012
Showing with 44 additions and 23 deletions.
  1. +1 −3 examples/simple.exs
  2. +2 −2 lib/dynamo/app.ex
  3. +28 −0 lib/dynamo/app/runner.ex
  4. +10 −10 lib/dynamo/cowboy.ex
  5. +1 −1 lib/dynamo/cowboy/handler.ex
  6. +2 −7 lib/mix/tasks/server.ex
View
4 examples/simple.exs
@@ -14,6 +14,4 @@ end
Code.prepend_path("deps/cowboy/ebin")
Dynamo.start(:prod)
-Dynamo.Cowboy.run MyApp.start, port: 3030
-
-IO.puts "Running MyApp on port 3030"
+MyApp.start.run port: 3030
View
4 lib/dynamo/app.ex
@@ -95,6 +95,7 @@ defmodule Dynamo.App do
use Dynamo.Utils.Once
use_once Dynamo.App.Config
+ use_once Dynamo.App.Runner
use_once Dynamo.Router.Filters
filter Dynamo.Filters.Head
@@ -135,8 +136,7 @@ defmodule Dynamo.App do
reload_modules: false,
source_paths: ["app/*"],
view_paths: ["app/views"],
- root: File.expand_path("../..", file),
- handler: Dynamo.Cowboy ]
+ root: File.expand_path("../..", file) ]
end
@doc false
View
28 lib/dynamo/app/runner.ex
@@ -0,0 +1,28 @@
+defmodule Dynamo.App.Runner do
+ @moduledoc """
+ Basic functionality which allows us to run and
+ customize a Dynamo app. For instance, you can
+ define your own run variation that pass custom
+ options to the handler:
+
+ def run(options) do
+ options = Keyword.merge(options, custom_cowboy_options)
+ Dynamo.Cowboy.run __MODULE__, options
+ end
+
+ """
+
+ @doc false
+ defmacro __using__(_) do
+ quote location: :keep do
+ @doc """
+ Runs the app in a web server.
+ """
+ def run(options // []) do
+ Dynamo.Cowboy.run __MODULE__, options
+ end
+
+ defoverridable [run: 1]
+ end
+ end
+end
View
20 lib/dynamo/cowboy.ex
@@ -9,7 +9,6 @@ defmodule Dynamo.Cowboy do
* :port - the port to run the server
* :acceptors - the number of acceptors for the listener
- * :handler - the Cowboy handler to be used
* :dispatch - the Cowboy HTTP Dispatch info to be used
## Example
@@ -20,28 +19,29 @@ defmodule Dynamo.Cowboy do
def run(app, options // []) do
:application.start(:cowboy)
- port = Keyword.get options, :port, 4000
- acceptors = Keyword.get options, :acceptors, 100
- handler = Keyword.get options, :handler, Dynamo.Cowboy.Handler
- dispatch = Keyword.get options, :dispatch, dispatch_for(app, handler)
+ port = options[:port] || 4000
+ acceptors = options[:acceptors] || 100
+ dispatch = options[:dispatch] || dispatch_for(app)
port = to_i(port)
acceptors = to_i(acceptors)
- options = Enum.reduce [:port, :acceptors, :handler], options, Keyword.delete(&2, &1)
- options = Keyword.put options, :dispatch, dispatch
+
+ unless options[:verbose] == false do
+ IO.puts "Running #{inspect app} at localhost:#{port} with Cowboy on #{Dynamo.env}"
+ end
:cowboy.start_listener(app, acceptors,
:cowboy_tcp_transport, [port: port],
- :cowboy_http_protocol, options
+ :cowboy_http_protocol, [dispatch: dispatch]
)
end
def shutdown(app) do
:cowboy.stop_listener(app)
end
- defp dispatch_for(app, handler) do
- [{ :_, [ {:_, handler, app } ] }]
+ defp dispatch_for(app) do
+ [{ :_, [ {:_, Dynamo.Cowboy.Handler, app } ] }]
end
defp to_i(integer) when is_integer(integer), do: integer
View
2 lib/dynamo/cowboy/handler.ex
@@ -20,7 +20,7 @@ defmodule Dynamo.Cowboy.Handler do
end
{ :ok, conn.cowboy_request, app }
else
- raise "Expected service to return a Dynamo.Cowboy.Response, got #{inspect conn}"
+ raise "Expected service to return a Dynamo.Cowboy.HTTP, got #{inspect conn}"
end
end
View
9 lib/mix/tasks/server.ex
@@ -26,14 +26,9 @@ defmodule Mix.Tasks.Server do
{ opts, _ } = OptionParser.parse(args, aliases: [p: :port])
Mix.Task.run "dynamo.app"
- app = Dynamo.app
- opts = Keyword.merge [port: 4000], opts
- handler = app.config[:dynamo][:handler]
+ opts = Keyword.merge [port: 4000], opts
+ Dynamo.app.run opts
- IO.puts "Running #{inspect app} on port #{opts[:port]} " <>
- "with #{List.last Module.split(handler)} on #{Mix.env}"
-
- handler.run app, opts
:timer.sleep(:infinity)
end
end

0 comments on commit 8dc2a85

Please sign in to comment.