diff options
Diffstat (limited to 'main.ha')
-rw-r--r-- | main.ha | 35 |
1 files changed, 14 insertions, 21 deletions
@@ -3,11 +3,13 @@ // Adapted from template by Willow Barraco <contact@willowbarraco.fr> use getopt; +use htmpl; use log; use net; -use net::ip; -use net::http; use net::dial; +use net::http; +use net::ip; +use net::tcp; use os; use memio; use io; @@ -16,8 +18,8 @@ use bufio; use strings; const usage: [_]getopt::help = [ - "HTTP server", - ('a', "address", "listened address (ex: 127.0.0.1:8080)") + "Lindenii Forge Server", + ('c', "config", "path to configuration file") ]; export fn main() void = { @@ -27,25 +29,17 @@ export fn main() void = { let port: u16 = 8080; let ip_addr: ip::addr4 = [127, 0, 0, 1]; - for (let i = 0z; i < len(cmd.opts); i += 1) { - const opt = cmd.opts[i]; + for (let opt .. cmd.opts) { switch (opt.0) { - case 'a' => - match (dial::splitaddr(opt.1, "")) { - case let value: (str, u16) => - ip_addr = ip::parsev4(value.0)!; - port = value.1; - case dial::invalid_address => - abort("Invalid address"); - }; + case 'c' => yield; // TODO: actually handle the config case => abort(); // unreachable }; }; - const server = match (http::listen(ip_addr, port)) { + const server = match (http::listen(ip_addr, port, net::tcp::reuseport, net::tcp::reuseaddr)) { case let this: *http::server => yield this; - case net::error => abort("failure while listening"); + case => abort("failure while listening"); }; defer http::server_finish(server); @@ -64,9 +58,8 @@ export fn main() void = { }; }; -export fn handlereq(conn: io::handle, request: *http::request) (void | io::error) = { - fmt::fprint(conn, "HTTP/1.1 200 OK\r\n")?; - fmt::fprint(conn, "Content-Type: text/plain\r\n\r\n")?; - - fmt::fprintln(conn, "Hi there!")?; +export fn handlereq(conn: io::handle, request: *http::request) (void | io::error | nomem) = { + htmpl::write(conn, "HTTP/1.1 200 OK\r\n")?; + htmpl::write(conn, "Content-Type: text/html\r\n\r\n")?; + tp_index(conn)?; }; |