Skip to content

Commit 17f7b56

Browse files
committed
build: migrate from moon.pkg.json to moon.pkg format
This commit transitions the project from the deprecated JSON-based package configuration format (moon.pkg.json) to the newer moon.pkg format. All configuration files have been updated, including the main package, subpackages (js, cors, static_file), and example directories. Additionally, minor code style improvements were made to enhance consistency, such as converting single-expression lambdas to block form and using `append` instead of `iter().each(push(_))` for merging middleware lists.
2 parents b6cbdb0 + b65ce5d commit 17f7b56

33 files changed

Lines changed: 965 additions & 218 deletions

cookie.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn HttpResponse::delete_cookie(self : HttpResponse, key : String) -> Unit {
4444

4545
///|
4646
pub fn cookie_to_string(cookie : Array[CookieItem]) -> String {
47-
cookie.map(_.to_string()).join(";")
47+
cookie.map(x => x.to_string()).join(";")
4848
}
4949

5050
///|

cors/cors.mbt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,29 @@ pub fn handle_cors(
5656
credentials? : Bool = false,
5757
max_age? : Int = 86400,
5858
) -> @mocket.Middleware {
59-
(event, next) => if is_preflight_request(event) {
60-
append_cors_preflight_headers(
61-
event,
62-
origin~,
63-
methods~,
64-
allow_headers~,
65-
credentials~,
66-
max_age~,
67-
)
68-
// 对于预检请求,直接返回空响应,不调用next()
69-
@mocket.HttpResponse::new(OK).to_responder()
70-
} else {
71-
append_cors_headers(
72-
event,
73-
origin~,
74-
methods~,
75-
allow_headers~,
76-
expose_headers~,
77-
credentials~,
78-
)
79-
// 对于普通请求,继续执行后续中间件和处理器
80-
next()
59+
(event, next) => {
60+
if is_preflight_request(event) {
61+
append_cors_preflight_headers(
62+
event,
63+
origin~,
64+
methods~,
65+
allow_headers~,
66+
credentials~,
67+
max_age~,
68+
)
69+
// 对于预检请求,直接返回空响应,不调用next()
70+
@mocket.HttpResponse::new(OK).to_responder()
71+
} else {
72+
append_cors_headers(
73+
event,
74+
origin~,
75+
methods~,
76+
allow_headers~,
77+
expose_headers~,
78+
credentials~,
79+
)
80+
// 对于普通请求,继续执行后续中间件和处理器
81+
next()
82+
}
8183
}
8284
}

cors/moon.pkg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {
2+
"oboard/mocket",
3+
}

cors/moon.pkg.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/body-reader/moon.pkg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {
2+
"oboard/mocket",
3+
}
4+
5+
options(
6+
"is-main": true,
7+
)

examples/body-reader/moon.pkg.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/cookie/moon.pkg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {
2+
"oboard/mocket",
3+
}
4+
5+
options(
6+
"is-main": true,
7+
)

examples/cookie/moon.pkg.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/responder/main.mbt

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,34 @@ fn main {
3030
// "name": "oboard",
3131
// "age": 21
3232
// }'
33-
..post("/json", event => try {
34-
let person : Person = event.req.body()
35-
HttpResponse::new(OK).body(
36-
"Hello, \{person.name}. You are \{person.age} years old.",
37-
)
38-
} catch {
39-
_ => HttpResponse::new(BadRequest).body("Invalid JSON")
33+
..post("/json", event => {
34+
try {
35+
let person : Person = event.req.body()
36+
HttpResponse::new(OK).body(
37+
"Hello, \{person.name}. You are \{person.age} years old.",
38+
)
39+
} catch {
40+
_ => HttpResponse::new(BadRequest).body("Invalid JSON")
41+
}
4042
})
4143

4244
// Echo Server
4345
..post("/echo", e => e.req)
4446

4547
// 404 Page
46-
..get("/404", _ => HttpResponse::new(NotFound).body(
47-
@mocket.html(
48-
(
49-
#|<html>
50-
#|<body>
51-
#| <h1>404</h1>
52-
#|</body>
53-
#|</html>
48+
.get("/404", _ => {
49+
HttpResponse::new(NotFound).body(
50+
@mocket.html(
51+
(
52+
#|<html>
53+
#|<body>
54+
#| <h1>404</h1>
55+
#|</body>
56+
#|</html>
57+
),
5458
),
55-
),
56-
))
59+
)
60+
})
5761

5862
// Print Server URL
5963
for path in app.mappings.keys() {

examples/responder/moon.pkg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {
2+
"oboard/mocket",
3+
"moonbitlang/core/json",
4+
}
5+
6+
options(
7+
"is-main": true,
8+
)

0 commit comments

Comments
 (0)