src/http.lua

Thu, 22 Jun 2023 21:30:40 +0100

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

http: Remove unused unexported redirect() function

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

mercurial