Browse Source

elixir/cowboy: make code more idiomatic

Utkarsh Kukreti 9 years ago
parent
commit
9e6d7ae8c7
1 changed files with 16 additions and 22 deletions
  1. 16 22
      frameworks/Elixir/cowboy/lib/hello.ex

+ 16 - 22
frameworks/Elixir/cowboy/lib/hello.ex

@@ -1,19 +1,13 @@
 defmodule Hello do
-
   def start(_type, _args) do
     dispatch = :cowboy_router.compile([
-
-      { :_,
-        [
-          {"/json", JsonHandler, []},
-          {"/plaintext", PlaintextHandler, []}
-      ]}
+      {:_, [{"/json", JsonHandler, []},
+            {"/plaintext", PlaintextHandler, []}]}
     ])
-    { :ok, _ } = :cowboy.start_http(:http,
-                                    5000,
-                                   [{:port, 8080}],
-                                   [{ :env, [{:dispatch, dispatch}]}]
-                                   )
+    {:ok, _} = :cowboy.start_http(:http,
+                                  5000,
+                                  [port: 8080],
+                                  [env: [dispatch: dispatch]])
   end
 end
 
@@ -23,11 +17,11 @@ defmodule JsonHandler do
   end
 
   def handle(request, state) do
-    { :ok, reply } = :cowboy_req.reply(200,
-      [{"content-type", "application/json"}],
-      Poison.encode!(%{:message => "Hello, World!"}),
-      request)
-    { :ok, reply, state }
+    {:ok, reply} = :cowboy_req.reply(200,
+                                     [{"content-type", "application/json"}],
+                                     Poison.encode!(%{message: "Hello, World!"}),
+                                     request)
+    {:ok, reply, state}
   end
 
   def terminate(_reason, _request, _state) do
@@ -41,11 +35,11 @@ defmodule PlaintextHandler do
   end
 
   def handle(request, state) do
-    { :ok, reply } = :cowboy_req.reply(200,
-      [{"content-type", "text/plain"}],
-      "Hello, World!",
-      request)
-    { :ok, reply, state }
+    {:ok, reply} = :cowboy_req.reply(200,
+                                     [{"content-type", "text/plain"}],
+                                     "Hello, World!",
+                                     request)
+    {:ok, reply, state}
   end
 
   def terminate(_reason, _request, _state) do