Repository navigation
Expand file tree
/
Copy pathhttp.ex
More file actions
67 lines (55 loc) · 1.75 KB
/
Copy pathhttp.ex
File metadata and controls
67 lines (55 loc) · 1.75 KB
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
defmodule HLTE.HTTP do
require Logger
use Task
def start_link(args), do: Task.start_link(__MODULE__, :run, [args])
def run([listen_port, headerName]) when is_number(listen_port) do
{:ok, _} =
:cowboy.start_clear(
:http,
[{:port, listen_port}],
%{:env => %{:dispatch => build_dispatch(headerName)}}
)
Logger.notice("HTTP listening on port #{listen_port}")
:ok
end
def build_dispatch(headerName) do
:cowboy_router.compile([
# bind to all interfaces, a la "0.0.0.0"
{:_,
[
# POST
{"/", HLTE.HTTP.Route.PostHilite, [headerName]},
{"/sns", HLTE.HTTP.Route.SNSIngest, []},
# GET
{"/version", HLTE.HTTP.Route.Version, []},
{"/search", HLTE.HTTP.Route.Search, [headerName]}
]}
])
end
@doc """
Called by route modules to provide the requisite CORS headers in
an OPTIONS pre-flight response.
Returns a request with the appropriate headers set.
"""
def cors_preflight_options(method, req, headerName) do
r1 = :cowboy_req.set_resp_header("Access-Control-Allow-Methods", "#{method}, OPTIONS", req)
r2 = :cowboy_req.set_resp_header("Access-Control-Allow-Origin", "*", r1)
:cowboy_req.set_resp_header(
"Access-Control-Allow-Headers",
"Content-Type, content-type, #{headerName}",
r2
)
end
def cors_preflight_options(method, req) do
:cowboy_req.set_resp_header(
"Access-Control-Allow-Origin",
"*",
:cowboy_req.set_resp_header("Access-Control-Allow-Methods", "#{method}, OPTIONS", req)
)
end
def calculate_body_hmac(bodyText) do
:crypto.mac(:hmac, :sha256, :persistent_term.get(:key), bodyText)
|> :binary.encode_hex()
|> :string.lowercase()
end
end