src/http.lua

Thu, 22 Jun 2023 21:31:56 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 22 Jun 2023 21:31:56 +0100
changeset 18
b5c4b245e24c
parent 16
68a0c983bf49
permissions
-rw-r--r--

http: Add some helpful comments regarding auth/CSRF

0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local http_server = require"net.http.server";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local files = require "net.http.files";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local http_codes = require"net.http.codes";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local promise = require "util.promise";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local url = require"socket.url";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local uuid = require"util.uuid";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local web = require "util.web";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local render = require "render".render;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local ok, json = pcall(require, "cjson");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 if not ok then json = require"util.json"; end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local errors = require "util.error".init("web", {});
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local templates;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local config;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local log = require "util.logger".init("web");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local csrf_token_len = #uuid.generate();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
18
b5c4b245e24c http: Add some helpful comments regarding auth/CSRF
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
20 -- Add a CSRF token to the view data and cookie (for verification on next request)
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local function check_csrf(event, viewdata)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local request, response = event.request, event.response;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 web.unpack_cookies(request);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local csrf_token = request.cookies.csrf_token;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 log("debug", "csrf_token=%s", tostring(csrf_token));
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if csrf_token and #csrf_token == csrf_token_len then
18
b5c4b245e24c http: Add some helpful comments regarding auth/CSRF
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
27 -- We already have a CSRF token cookie
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 viewdata.csrf_token = csrf_token;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 else
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 csrf_token = uuid.generate();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 viewdata.csrf_token = csrf_token;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 web.set_cookie(response.headers, "csrf_token=" .. csrf_token .. "; Path="..config.base_path.."; HttpOnly");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
36 local function wrap_handler(f, default_tpl, path_prefix_len, check_auth)
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 return function (event)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 log("debug", "Check auth...");
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
39 local request = event.request;
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
40 local authed, handler_override = check_auth(request, config);
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
41 if authed and not request.authenticated then
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
42 request.authenticated = authed;
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
43 end
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 log("debug", "Checked, %s", authed);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 event.config = config;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local sub_path = nil;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if path_prefix_len then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 sub_path = event.request.path:sub(path_prefix_len+2);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
50
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
51 local h = handler_override or f;
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
52
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
53 local p, custom_tpl;
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
54 if web.is_response(h) then
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
55 p = h;
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
56 else
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
57 p, custom_tpl = (handler_override or f)(event, sub_path);
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
58 end
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
59
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
60 -- Process response (may be promises)
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
61 return promise.join(function (resp, tpl)
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
62 if type(resp) == "table" and not web.is_response(resp) then
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local headers = event.response.headers;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local accept = event.request.headers.accept or "";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 web.add_header(headers, "vary", "Accept, Cookie");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if accept:find"application/json" then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 headers.content_type = "application/json";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return json.encode(resp);
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
69 elseif tpl then
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 if authed or event.needs_csrf then check_csrf(event, resp); end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 resp.authenticated = event.request.authenticated;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 resp.config = config;
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
73 resp = render(tpl, resp);
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 if not headers.content_type then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 headers.content_type = "text/html; charset=utf-8";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 else
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return resp;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 return resp;
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
82 end, p, custom_tpl or default_tpl):catch(function (err)
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 log("error", "Failed inside handler wrapper: %s", json.encode(err));
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return promise.reject(errors.wrap(err));
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local function handle_error(error, config)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 if not error.response then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 -- Top-level error, return text string
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if config.debug then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 return error.private_message;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 return "An internal error occurred.";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 error.response.headers.content_type = "text/html; charset=utf-8";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 log("warn", "HTTP error handler triggered (debug: %s): %d %s", not not config.debug, error.code, json.encode(error.error));
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local err = errors.wrap(error.err);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local r = render(templates.error or "{text}", {
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 config = config;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 code = err.code or error.code;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 long = err.condition or http_codes[error.code];
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 text = err.text or "An unexpected error occurred. Please try again later.";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 id = err.instance_id;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 internal_error = config.debug and error.error and error.error.context.wrapped_error;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 });
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 return r;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 local function size_only(request, data)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 request.headers.content_size = #data;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 return 200;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local function head_handler(handler)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 return function (event)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 event.send = size_only;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 return handler(event);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
127 local function register_handlers(handlers, event_base, path_prefix, check_auth)
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 for method_path, handler in pairs(handlers) do
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 if type(handler) == "table" then
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
130 register_handlers(handler, event_base, (path_prefix and (path_prefix.."/") or "")..method_path, check_auth);
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 else
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local method, path, wildcard = method_path:match"^(%w+)_(.-)(_?)$";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 method = method:upper();
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 wildcard = wildcard == "_" and (path == "" and "*" or "/*") or "";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 local template_name = path;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 if path == "" then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 template_name = "index";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 if path_prefix then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 path = path_prefix .. "/" .. path;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 if templates[template_name] then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 log("debug", "handler for %s %s (wildcard: %s) with template %s", method, path, wildcard, template_name);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 else
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 log("debug", "No template (%s) for %s", template_name, path);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 local path_prefix_len;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 if wildcard == "/*" then
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 path_prefix_len = #path+1;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
155 handler = wrap_handler(handler, templates[template_name], path_prefix_len, check_auth);
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 local head_event = event_base:format("HEAD", path, wildcard);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 http_server.add_handler(head_event, head_handler(handler));
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 local event_name = event_base:format(method, path, wildcard);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 http_server.add_handler(event_name, handler);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 log("debug", "Handler for %s added with template %s", event_name, template_name);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 local function init(_config, events)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 config = _config or {};
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 templates = require "templates".init(config);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 local base_url = url.parse(config.base_url or "http://localhost:8006/");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 local base_path = url.parse_path(config.base_path or base_url.path or "/");
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 base_path.is_directory = true;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 base_url.path = url.build_path(base_path);
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
175 config.templates = templates;
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 config.base_host = base_url.host;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 config.base_path = base_url.path;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 config.base_url = url.build(base_url);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 local event_base = ("%%s %s%s%%s%%s"):format(base_url.host, base_url.path);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
181 _G.CONFIG = config;
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
182 local check_auth = require "app.auth".check_auth;
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local handlers = require "app.routes";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 handlers.get_static_ = files.serve { path = "./static", http_base = base_url.path.."static" };
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 http_server.add_host(base_url.host);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 http_server.set_default_host(base_url.host);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 http_server.add_handler("http-error", function (error)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 return handle_error(error, config)
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 end);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
193 register_handlers(handlers, event_base, nil, check_auth);
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 local listen_port = config.listen_port or 8007;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 local listen_interface = config.listen_interface or "*";
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 assert(http_server.listen_on(listen_port, listen_interface));
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 log("info", "Serving web interface on http://localhost:%d/", listen_port);
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 end
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 return {
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 init = init;
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 render = render;
12
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
204 set_auth_cookie = set_auth_cookie;
dfa7cb60647e http: Handler logic improvements (affects auth, config, templates)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
205 get_auth_cookie = get_auth_cookie;
0
6279a7d40ae7 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 }

mercurial