|
1 local verse = require "verse"; |
|
2 |
|
3 local server = require "net.server"; |
|
4 local http_server = require "net.http.server"; |
|
5 local http = require "net.http"; |
|
6 local json = require "util.json"; |
|
7 |
|
8 function handle_request() |
|
9 return "Hello world"; |
|
10 end |
|
11 |
|
12 local function run(config, run_script) |
|
13 function handle_run_request(event) |
|
14 local data = json.decode(event.request.body); |
|
15 if not data then |
|
16 return 400; |
|
17 end |
|
18 |
|
19 if not data.script then |
|
20 return 400; |
|
21 end |
|
22 |
|
23 local ok, ret = pcall(run_script, "web", data.script); |
|
24 |
|
25 if not ok then |
|
26 return json.encode({ error = true, message = ret }); |
|
27 end |
|
28 |
|
29 return json.encode(ret); |
|
30 end |
|
31 |
|
32 http_server.add_host("localhost"); |
|
33 http_server.set_default_host("localhost"); |
|
34 http_server.add_handler("GET localhost/*", handle_request); |
|
35 http_server.add_handler("POST localhost/run", handle_run_request); |
|
36 http_server.add_handler("http-error", function (e) |
|
37 verse.log("error", "HTTP error: %s", e.code); |
|
38 end); |
|
39 --http_server.add_handler("GET localhost/stream/*", handle_stream_request); |
|
40 http_server.listen_on(config.port or 8007); |
|
41 verse.log("debug", "Ready") |
|
42 verse.loop(); |
|
43 end |
|
44 |
|
45 return { |
|
46 run = run; |
|
47 } |