|
1 | 1 | // Example usage of mocket package in MoonBit |
2 | 2 |
|
| 3 | +// `run_async` spawn a new coroutine and execute an async function in it |
| 4 | +///| |
| 5 | +fn run_async(f : async() -> Unit) -> Unit = "%async.run" |
| 6 | + |
| 7 | +// `suspend` will suspend the execution of the current coroutine. |
| 8 | +// The suspension will be handled by a callback passed to `suspend` |
| 9 | +///| |
| 10 | +// async fn suspend[T, E : Error]( |
| 11 | +// // `f` is a callback for handling suspension |
| 12 | +// // the first parameter of `f` is used to resume the execution of the coroutine normally |
| 13 | +// // the second parameter of `f` is used to cancel the execution of the current coroutine |
| 14 | +// // by throwing an error at suspension point |
| 15 | +// f : ((T) -> Unit, (E) -> Unit) -> Unit |
| 16 | +// ) -> T!E = "%async.suspend" |
| 17 | + |
| 18 | +///| |
3 | 19 | fn main { |
4 | 20 | println("Starting server...") |
5 | 21 | let port = 4000 |
6 | 22 | let server = @mocket.listen(get_context(), port) |
7 | 23 | listen_event("echo", fn(json) { println(json) }) |
8 | | - // readFile example |
9 | | - // @mocket.readFile("./logo.jpg").finally( |
10 | | - // fn(data : Bytes) { println(data.length()) }, |
11 | | - // ) |
12 | | - // html response example |
13 | | - server.get( |
14 | | - "/", |
15 | | - fn(_req : @mocket.HttpRequest, _res : @mocket.HttpResponse) { |
16 | | - @mocket.html("<h1>Hello, World!</h1>") |
17 | | - }, |
18 | | - ) |
| 24 | + |
| 25 | + // 修改文件读取示例 |
| 26 | + // try { |
| 27 | + // let data = @mocket.readFile!!("./logo.jpg") |
| 28 | + // println(data.length()) |
| 29 | + // } catch { |
| 30 | + // err => println("Error reading file: \{err}") |
| 31 | + // } |
| 32 | + |
| 33 | + server.get("/", fn(_req : @mocket.HttpRequest, _res : @mocket.HttpResponse) { |
| 34 | + @mocket.html("<h1>Hello, World!</h1>") |
| 35 | + }) |
19 | 36 | // string response example |
20 | | - server.get( |
21 | | - "/text", |
22 | | - fn(_req : @mocket.HttpRequest, _res : @mocket.HttpResponse) { |
23 | | - String("<h1>Hello, World!</h1>") |
24 | | - }, |
25 | | - ) |
| 37 | + server.get("/text", fn( |
| 38 | + _req : @mocket.HttpRequest, |
| 39 | + _res : @mocket.HttpResponse |
| 40 | + ) { |
| 41 | + String("<h1>Hello, World!</h1>") |
| 42 | + }) |
26 | 43 | // json data example |
27 | | - server.get( |
28 | | - "/data", |
29 | | - fn(_req : @mocket.HttpRequest, _res : @mocket.HttpResponse) { |
30 | | - { "name": "John Doe", "age": 30, "city": "New York" } |
31 | | - }, |
32 | | - ) |
| 44 | + server.get("/data", fn( |
| 45 | + _req : @mocket.HttpRequest, |
| 46 | + _res : @mocket.HttpResponse |
| 47 | + ) { |
| 48 | + { "name": "John Doe", "age": 30, "city": "New York" } |
| 49 | + }) |
33 | 50 | // echo server example |
34 | | - server.post( |
35 | | - "/echo", |
36 | | - fn(req : @mocket.HttpRequest, _res : @mocket.HttpResponse) { |
37 | | - match req.body { |
38 | | - Some(data) => data |
39 | | - _ => String("No data received") |
40 | | - } |
41 | | - }, |
42 | | - ) |
| 51 | + server.post("/echo", fn( |
| 52 | + req : @mocket.HttpRequest, |
| 53 | + _res : @mocket.HttpResponse |
| 54 | + ) { |
| 55 | + match req.body { |
| 56 | + Some(data) => data |
| 57 | + _ => String("No data received") |
| 58 | + } |
| 59 | + }) |
43 | 60 | // file serving example |
44 | | - server.get( |
45 | | - "/image", |
46 | | - fn(_req : @mocket.HttpRequest, _res : @mocket.HttpResponse) { |
47 | | - @mocket.file("logo.jpg") |
48 | | - }, |
49 | | - ) |
| 61 | + server.get("/image", fn( |
| 62 | + _req : @mocket.HttpRequest, |
| 63 | + _res : @mocket.HttpResponse |
| 64 | + ) { |
| 65 | + @mocket.file("logo.jpg") |
| 66 | + }) |
50 | 67 |
|
51 | 68 | // buffer serving example |
52 | | - server.get( |
53 | | - "/buffer", |
54 | | - fn(_req : @mocket.HttpRequest, _res : @mocket.HttpResponse) { |
55 | | - @mocket.buffer( |
56 | | - [ |
57 | | - 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 84, 104, 105, |
58 | | - 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, |
59 | | - 110, 103, 32, 102, 111, 114, 32, 116, 101, 115, 116, 105, 110, 103, 32, |
60 | | - 112, 117, 114, 112, 111, 115, 101, |
61 | | - ].map(fn(x) { x.to_byte() }) |
62 | | - |> Bytes::from_array, |
63 | | - ) |
64 | | - }, |
65 | | - ) |
| 69 | + server.get("/buffer", fn( |
| 70 | + _req : @mocket.HttpRequest, |
| 71 | + _res : @mocket.HttpResponse |
| 72 | + ) { |
| 73 | + @mocket.buffer( |
| 74 | + [ |
| 75 | + 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 32, 84, 104, 105, |
| 76 | + 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116, 32, 115, 116, 114, 105, |
| 77 | + 110, 103, 32, 102, 111, 114, 32, 116, 101, 115, 116, 105, 110, 103, 32, 112, |
| 78 | + 117, 114, 112, 111, 115, 101, |
| 79 | + ].map(fn(x) { x.to_byte() }) |
| 80 | + |> Bytes::from_array, |
| 81 | + ) |
| 82 | + }) |
66 | 83 |
|
67 | 84 | // static file serving example |
68 | 85 | // Example: http://localhost:4000/static/logo.jpg => ./logo.jpg |
69 | 86 | server.resource("/static/", "./") |
70 | | - |
71 | | - // exec example |
72 | | - @mocket.exec("ls").finally(println) |
73 | | - |
74 | | - // fetch example |
75 | | - @mocket.fetch("https://api64.ipify.org/").finally(println) |
| 87 | + run_async(fn() { |
| 88 | + // 修改exec示例 |
| 89 | + try { |
| 90 | + let result = @mocket.exec!!("ls") |
| 91 | + println(result) |
| 92 | + } catch { |
| 93 | + err => println("Error executing command: \{err}") |
| 94 | + } |
| 95 | + }) |
| 96 | + run_async(fn() { |
| 97 | + // 修改fetch示例 |
| 98 | + try { |
| 99 | + let response = @mocket.fetch!!("https://api64.ipify.org/") |
| 100 | + println(response) |
| 101 | + } catch { |
| 102 | + err => println("Error fetching data: \{err}") |
| 103 | + } |
| 104 | + }) |
76 | 105 | } |
0 commit comments