Blame


1 d2f8aa1d 2023-12-19 op use errors;
2 2bc5ec6b 2023-12-19 op use fmt;
3 2bc5ec6b 2023-12-19 op use getopt;
4 2bc5ec6b 2023-12-19 op use io;
5 2bc5ec6b 2023-12-19 op use log;
6 718ba070 2023-12-22 op use net::dial;
7 2bc5ec6b 2023-12-19 op use net::ip;
8 2bc5ec6b 2023-12-19 op use net;
9 2bc5ec6b 2023-12-19 op use os;
10 d2f8aa1d 2023-12-19 op use unix::signal;
11 2bc5ec6b 2023-12-19 op
12 2bc5ec6b 2023-12-19 op use ev;
13 bcc284ab 2023-12-21 op use http;
14 ea441465 2023-12-31 op use sqlite3;
15 2bc5ec6b 2023-12-19 op
16 d2f8aa1d 2023-12-19 op fn sighandler(sig: signal::sig, data: nullable *opaque) void = {
17 d2f8aa1d 2023-12-19 op // Normal signal handler rules don't apply because ev
18 d2f8aa1d 2023-12-19 op // decouples for us.
19 d2f8aa1d 2023-12-19 op
20 d2f8aa1d 2023-12-19 op if (sig == signal::sig::TERM || sig == signal::sig::INT) {
21 d2f8aa1d 2023-12-19 op log::printfln("got {}; quitting...", signal::signame(sig));
22 d2f8aa1d 2023-12-19 op ev::loopbreak();
23 d2f8aa1d 2023-12-19 op return;
24 d2f8aa1d 2023-12-19 op };
25 d2f8aa1d 2023-12-19 op
26 d2f8aa1d 2023-12-19 op log::fatalf("unexpected {}", signal::signame(sig));
27 d2f8aa1d 2023-12-19 op };
28 d2f8aa1d 2023-12-19 op
29 cf461aa4 2023-12-21 op fn homepage(req: *http::request, res: *http::reswriter) (void | io::error) = {
30 94691153 2023-12-22 op if (res.code == 0) {
31 b48b5e54 2023-12-21 op http::reply(req, res, 200, "OK")?;
32 05724a92 2023-12-22 op http::header(res, "Content-Type", "text/plain;charset=utf-8")?;
33 b48b5e54 2023-12-21 op fmt::fprintfln(res, "こんにちは世界!")?;
34 b48b5e54 2023-12-21 op res.done = false;
35 b48b5e54 2023-12-21 op return;
36 b48b5e54 2023-12-21 op };
37 b48b5e54 2023-12-21 op
38 b48b5e54 2023-12-21 op fmt::fprintfln(res, "シンシャです。")?;
39 bcc284ab 2023-12-21 op };
40 bcc284ab 2023-12-21 op
41 cf461aa4 2023-12-21 op fn page2(req: *http::request, res: *http::reswriter) (void | io::error) = {
42 b48b5e54 2023-12-21 op http::reply(req, res, 200, "OK")?;
43 05724a92 2023-12-22 op http::header(res, "Content-Type", "text/plain;charset=utf-8")?;
44 b48b5e54 2023-12-21 op fmt::fprintln(res, "Pagina 2")?;
45 bcc284ab 2023-12-21 op };
46 bcc284ab 2023-12-21 op
47 2bc5ec6b 2023-12-19 op export fn main() void = {
48 ea441465 2023-12-31 op let db = sqlite3::open("shinsha.sqlite3")!;
49 ea441465 2023-12-31 op defer sqlite3::close(db)!;
50 ea441465 2023-12-31 op
51 ea441465 2023-12-31 op let stmt = sqlite3::prepare(db, "select $first, $second")!;
52 ea441465 2023-12-31 op defer sqlite3::finalize(stmt)!;
53 ea441465 2023-12-31 op
54 ea441465 2023-12-31 op sqlite3::bind(stmt, "$first", 42)!;
55 ea441465 2023-12-31 op sqlite3::bind(stmt, 2, "hello from hare!")!;
56 ea441465 2023-12-31 op // eg. set to null
57 ea441465 2023-12-31 op //sqlite3::bind(stmt, "$text", void)!;
58 ea441465 2023-12-31 op
59 ea441465 2023-12-31 op for (sqlite3::step(stmt)!) {
60 ea441465 2023-12-31 op fmt::println(sqlite3::column_text(stmt, 0))!;
61 ea441465 2023-12-31 op fmt::println(sqlite3::column_text(stmt, 1))!;
62 ea441465 2023-12-31 op };
63 ea441465 2023-12-31 op
64 ea441465 2023-12-31 op sqlite3::reset(stmt)!;
65 ea441465 2023-12-31 op for (sqlite3::step(stmt)!) {
66 ea441465 2023-12-31 op fmt::println(sqlite3::column_text(stmt, 0))!;
67 ea441465 2023-12-31 op fmt::println(sqlite3::column_text(stmt, 1))!;
68 ea441465 2023-12-31 op };
69 ea441465 2023-12-31 op
70 ea441465 2023-12-31 op sqlite3::reset(stmt)!;
71 ea441465 2023-12-31 op sqlite3::clear_bindings(stmt)!;
72 ea441465 2023-12-31 op sqlite3::bind(stmt, "$first", "he-he-he!")!;
73 ea441465 2023-12-31 op for (sqlite3::step(stmt)!) {
74 ea441465 2023-12-31 op fmt::println(sqlite3::column_text(stmt, 0))!;
75 ea441465 2023-12-31 op fmt::println(sqlite3::column_text(stmt, 1))!;
76 ea441465 2023-12-31 op };
77 ea441465 2023-12-31 op };
78 ea441465 2023-12-31 op
79 ea441465 2023-12-31 op export fn main2() void = {
80 2bc5ec6b 2023-12-19 op const cmd = getopt::parse(os::args,
81 2bc5ec6b 2023-12-19 op "web manga reader",
82 2bc5ec6b 2023-12-19 op ('d', "run in the foreground"),
83 2bc5ec6b 2023-12-19 op ('v', "verbose output"),
84 2bc5ec6b 2023-12-19 op );
85 2bc5ec6b 2023-12-19 op defer getopt::finish(&cmd);
86 2bc5ec6b 2023-12-19 op
87 2bc5ec6b 2023-12-19 op let debug = false;
88 2bc5ec6b 2023-12-19 op let verbose = false;
89 2bc5ec6b 2023-12-19 op
90 2bc5ec6b 2023-12-19 op for (let i = 0z; i < len(cmd.opts); i += 1) {
91 2bc5ec6b 2023-12-19 op const opt = cmd.opts[i];
92 2bc5ec6b 2023-12-19 op switch (opt.0) {
93 2bc5ec6b 2023-12-19 op case 'd' =>
94 2bc5ec6b 2023-12-19 op debug = true;
95 2bc5ec6b 2023-12-19 op case 'v' =>
96 2bc5ec6b 2023-12-19 op verbose = true;
97 2bc5ec6b 2023-12-19 op case =>
98 2bc5ec6b 2023-12-19 op abort();
99 2bc5ec6b 2023-12-19 op };
100 2bc5ec6b 2023-12-19 op };
101 2bc5ec6b 2023-12-19 op
102 2bc5ec6b 2023-12-19 op log::printfln("should use debug {} and verbose {}", debug, verbose);
103 2bc5ec6b 2023-12-19 op
104 718ba070 2023-12-22 op const addr = "localhost";
105 718ba070 2023-12-22 op const port = "9090";
106 718ba070 2023-12-22 op
107 718ba070 2023-12-22 op let (ips, port) = match(dial::resolve("tcp", addr, port)) {
108 718ba070 2023-12-22 op case let err: net::dial::error =>
109 718ba070 2023-12-22 op log::fatal("failed to resolve {}:{}: {}", addr, port,
110 718ba070 2023-12-22 op dial::strerror(err));
111 718ba070 2023-12-22 op case let pair: ([]ip::addr, u16) =>
112 718ba070 2023-12-22 op yield pair;
113 2bc5ec6b 2023-12-19 op };
114 2bc5ec6b 2023-12-19 op
115 718ba070 2023-12-22 op for (let i = 0z; i < len(ips); i += 1) {
116 718ba070 2023-12-22 op match (http::listen(ips[i], port, null)) {
117 718ba070 2023-12-22 op case let err: net::error =>
118 718ba070 2023-12-22 op log::fatal("failed to listen to {}:{}: {}",
119 718ba070 2023-12-22 op ip::string(ips[i]), port, net::strerror(err));
120 718ba070 2023-12-22 op case => yield;
121 718ba070 2023-12-22 op };
122 718ba070 2023-12-22 op };
123 2bc5ec6b 2023-12-19 op
124 bcc284ab 2023-12-21 op http::handle("/", &homepage);
125 bcc284ab 2023-12-21 op http::handle("/2", &page2);
126 bcc284ab 2023-12-21 op
127 d2f8aa1d 2023-12-19 op ev::signal(signal::sig::INT, &sighandler, null)!;
128 d2f8aa1d 2023-12-19 op ev::signal(signal::sig::TERM, &sighandler, null)!;
129 d2f8aa1d 2023-12-19 op
130 718ba070 2023-12-22 op log::printfln("listening on {}:{}", addr, port);
131 d2f8aa1d 2023-12-19 op match (ev::mainloop()) {
132 d2f8aa1d 2023-12-19 op case void => yield;
133 d2f8aa1d 2023-12-19 op case let err: errors::error =>
134 d2f8aa1d 2023-12-19 op log::fatalf("mainloop failure: {}", errors::strerror(err));
135 d2f8aa1d 2023-12-19 op };
136 2bc5ec6b 2023-12-19 op
137 2bc5ec6b 2023-12-19 op log::printfln("exiting...");
138 2bc5ec6b 2023-12-19 op };