forked from seven1m/30-days-of-elixir
-
Notifications
You must be signed in to change notification settings - Fork 1
/
28-frank-3.exs
133 lines (108 loc) · 3.09 KB
/
28-frank-3.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# A micro web DSL library called "Frank"
# The motivation was mainly to learn about macros.
#
# The third (and hopefully final) version of Frank
# adds support for matching on parameters.
# (Still no regex match, but I realized I didn't care
# as much about that as I thought :-)
require Record
defmodule Frank do
@moduledoc """
Frank is a micro web library that provides a DSL for defining routes.
defmodule MyApp do
use Frank
get "/foo" do
response 200, "foo!"
end
get "/hello/:name" do
response 200, "greetings \#{name}"
end
end
Frank.sing(MyApp)
To run:
$ iex 28-frank-3.exs
... then point your browser to http://localhost:3000
"""
Record.defrecord :mod, Record.extract(:mod, from_lib: "inets/include/httpd.hrl")
@doc """
Start the web server given the app module.
"""
def sing(module) do
:inets.start()
options = [server_name: 'frank', server_root: '/tmp', document_root: '/tmp', port: 3000, modules: [module]]
{:ok, _pid} = :inets.start :httpd, options
IO.puts "running on port 3000"
end
defmodule Path do
defmacro get(path, [do: code]) do
parts = build_pattern(path)
quote do
def handle(unquote(parts), data) do
unquote(code)
end
end
end
defmacro __before_compile__(_env) do
quote do
def handle(_, data) do
response(404, 'not defined')
end
end
end
defp build_pattern(path) do
path = String.lstrip(path, ?/)
for part <- String.split(path, "/") do
cond do
String.starts_with?(part, ":") ->
# expands (when unquoted) to a variable name
{String.to_atom(String.lstrip(part, ?:)), [], nil}
true ->
# literal string match
part
end
end
end
def redirect(path, code \\ 302) do
body = ['redirecting you to <a href="', path, '">', path, '</a>']
response code, body, [location: path]
end
def sanitize(content) do
content = Regex.replace(~r/&/, content, "\\&")
content = Regex.replace(~r/</, content, "\\<")
content = Regex.replace(~r/>/, content, "\\>")
content
end
def response(code, body, headers \\ []) do
if is_binary(body) do
body = :erlang.bitstring_to_list(body)
end
headers = [code: code, content_length: Integer.to_char_list(IO.iodata_length(body))] ++ headers
{:proceed, [response: {:response, headers, body}]}
end
end
defmacro __using__(_opts) do
quote do
import Frank.Path
def unquote(:do)(data) do
[_ | path] = Frank.mod(data, :request_uri)
path = :erlang.list_to_bitstring(path)
parts = String.split(path, "/")
handle(parts, data)
end
@before_compile Path
end
end
end
defmodule Test do
use Frank
get "/" do
response 200, "<a href='/foo'>go to foo</a>"
end
get "/foo" do
response 200, "foo!"
end
get "/hello/:name" do
response 200, "greetings #{name}"
end
end
Frank.sing(Test)