Skip to content

Commit a0dda92

Browse files
committed
refactor(mocket): centralize not found handling and simplify responder
- Add handle_not_found function for consistent 404 responses - Remove redundant status_code assignments in Responder impls - Simplify static file provider implementation - Clean up unused FFI functions
1 parent d73a7c3 commit a0dda92

8 files changed

Lines changed: 49 additions & 256 deletions

File tree

src/js/pkg.generated.mbti

Lines changed: 0 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -1,207 +1,13 @@
11
// Generated using `moon info`, DON'T EDIT IT
22
package "oboard/mocket/js"
33

4-
import(
5-
"moonbitlang/core/json"
6-
)
7-
84
// Values
9-
async fn[T] async_all(Array[async () -> T]) -> Array[T]
10-
11-
let async_iterator : Symbol
12-
13-
fn async_run(async () -> Unit noraise) -> Unit
14-
15-
fn async_test(async () -> Unit) -> Unit
16-
17-
let globalThis : Value
18-
19-
let iterator : Symbol
20-
21-
fn require(String, keys? : Array[String]) -> Value
22-
23-
fn[T, E : Error] spawn_detach(async () -> T raise E) -> Unit
24-
25-
async fn[T, E : Error] suspend(((T) -> Unit, (E) -> Unit) -> Unit) -> T raise E
265

276
// Errors
28-
pub suberror Error_ Value
29-
fn Error_::cause(Self) -> Value?
30-
fn[T] Error_::wrap(() -> Value, map_ok? : (Value) -> T) -> T raise Self
31-
impl Show for Error_
327

338
// Types and methods
34-
type Nullable[_]
35-
fn[T] Nullable::from_option(T?) -> Self[T]
36-
fn[T] Nullable::get_exn(Self[T]) -> T
37-
fn[T] Nullable::is_null(Self[T]) -> Bool
38-
fn[T] Nullable::null() -> Self[T]
39-
fn[T] Nullable::to_option(Self[T]) -> T?
40-
fn[T] Nullable::unwrap(Self[T]) -> T
41-
42-
pub struct Object(Value)
43-
fn[K, V] Object::extend_iter(Self, Iter[(K, V)]) -> Unit
44-
fn[K, V] Object::extend_iter2(Self, Iter2[K, V]) -> Unit
45-
fn Object::extend_object(Self, Self) -> Self
46-
fn[K, V] Object::from_iter(Iter[(K, V)]) -> Self
47-
fn[K, V] Object::from_iter2(Iter2[K, V]) -> Self
48-
fn Object::from_value(Value) -> Optional[Self]
49-
fn Object::from_value_unchecked(Value) -> Self
50-
#deprecated
51-
fn Object::inner(Self) -> Value
52-
fn Object::new() -> Self
53-
fn[K, V] Object::op_get(Self, K) -> V
54-
fn[K, V] Object::op_set(Self, K, V) -> Unit
55-
fn Object::to_value(Self) -> Value
56-
57-
type Optional[_]
58-
fn[T] Optional::from_option(T?) -> Self[T]
59-
fn[T] Optional::get_exn(Self[T]) -> T
60-
fn[T] Optional::is_undefined(Self[T]) -> Bool
61-
fn[T] Optional::to_option(Self[T]) -> T?
62-
fn[T] Optional::undefined() -> Self[T]
63-
fn[T] Optional::unwrap(Self[T]) -> T
64-
65-
#external
66-
pub type Promise
67-
fn Promise::all(Array[Self]) -> Self
68-
fn[T] Promise::unsafe_new(async () -> T) -> Self
69-
async fn Promise::wait(Self) -> Value
70-
71-
type Symbol
72-
fn Symbol::make() -> Self
73-
fn Symbol::make_with_number(Double) -> Self
74-
fn Symbol::make_with_string(String) -> Self
75-
fn Symbol::make_with_string_js(String) -> Self
76-
77-
type Union2[_, _]
78-
fn[A : Cast, B] Union2::from0(A) -> Self[A, B]
79-
fn[A, B : Cast] Union2::from1(B) -> Self[A, B]
80-
fn[A : Cast, B] Union2::to0(Self[A, B]) -> A?
81-
fn[A, B : Cast] Union2::to1(Self[A, B]) -> B?
82-
83-
type Union3[_, _, _]
84-
fn[A : Cast, B, C] Union3::from0(A) -> Self[A, B, C]
85-
fn[A, B : Cast, C] Union3::from1(B) -> Self[A, B, C]
86-
fn[A, B, C : Cast] Union3::from2(C) -> Self[A, B, C]
87-
fn[A : Cast, B, C] Union3::to0(Self[A, B, C]) -> A?
88-
fn[A, B : Cast, C] Union3::to1(Self[A, B, C]) -> B?
89-
fn[A, B, C : Cast] Union3::to2(Self[A, B, C]) -> C?
90-
91-
type Union4[_, _, _, _]
92-
fn[A : Cast, B, C, D] Union4::from0(A) -> Self[A, B, C, D]
93-
fn[A, B : Cast, C, D] Union4::from1(B) -> Self[A, B, C, D]
94-
fn[A, B, C : Cast, D] Union4::from2(C) -> Self[A, B, C, D]
95-
fn[A, B, C, D : Cast] Union4::from3(D) -> Self[A, B, C, D]
96-
fn[A : Cast, B, C, D] Union4::to0(Self[A, B, C, D]) -> A?
97-
fn[A, B : Cast, C, D] Union4::to1(Self[A, B, C, D]) -> B?
98-
fn[A, B, C : Cast, D] Union4::to2(Self[A, B, C, D]) -> C?
99-
fn[A, B, C, D : Cast] Union4::to3(Self[A, B, C, D]) -> D?
100-
101-
type Union5[_, _, _, _, _]
102-
fn[A : Cast, B, C, D, E] Union5::from0(A) -> Self[A, B, C, D, E]
103-
fn[A, B : Cast, C, D, E] Union5::from1(B) -> Self[A, B, C, D, E]
104-
fn[A, B, C : Cast, D, E] Union5::from2(C) -> Self[A, B, C, D, E]
105-
fn[A, B, C, D : Cast, E] Union5::from3(D) -> Self[A, B, C, D, E]
106-
fn[A, B, C, D, E : Cast] Union5::from4(E) -> Self[A, B, C, D, E]
107-
fn[A : Cast, B, C, D, E] Union5::to0(Self[A, B, C, D, E]) -> A?
108-
fn[A, B : Cast, C, D, E] Union5::to1(Self[A, B, C, D, E]) -> B?
109-
fn[A, B, C : Cast, D, E] Union5::to2(Self[A, B, C, D, E]) -> C?
110-
fn[A, B, C, D : Cast, E] Union5::to3(Self[A, B, C, D, E]) -> D?
111-
fn[A, B, C, D, E : Cast] Union5::to4(Self[A, B, C, D, E]) -> E?
112-
113-
type Union6[_, _, _, _, _, _]
114-
fn[A : Cast, B, C, D, E, F] Union6::from0(A) -> Self[A, B, C, D, E, F]
115-
fn[A, B : Cast, C, D, E, F] Union6::from1(B) -> Self[A, B, C, D, E, F]
116-
fn[A, B, C : Cast, D, E, F] Union6::from2(C) -> Self[A, B, C, D, E, F]
117-
fn[A, B, C, D : Cast, E, F] Union6::from3(D) -> Self[A, B, C, D, E, F]
118-
fn[A, B, C, D, E : Cast, F] Union6::from4(E) -> Self[A, B, C, D, E, F]
119-
fn[A, B, C, D, E, F : Cast] Union6::from5(F) -> Self[A, B, C, D, E, F]
120-
fn[A : Cast, B, C, D, E, F] Union6::to0(Self[A, B, C, D, E, F]) -> A?
121-
fn[A, B : Cast, C, D, E, F] Union6::to1(Self[A, B, C, D, E, F]) -> B?
122-
fn[A, B, C : Cast, D, E, F] Union6::to2(Self[A, B, C, D, E, F]) -> C?
123-
fn[A, B, C, D : Cast, E, F] Union6::to3(Self[A, B, C, D, E, F]) -> D?
124-
fn[A, B, C, D, E : Cast, F] Union6::to4(Self[A, B, C, D, E, F]) -> E?
125-
fn[A, B, C, D, E, F : Cast] Union6::to5(Self[A, B, C, D, E, F]) -> F?
126-
127-
type Union7[_, _, _, _, _, _, _]
128-
fn[A : Cast, B, C, D, E, F, G] Union7::from0(A) -> Self[A, B, C, D, E, F, G]
129-
fn[A, B : Cast, C, D, E, F, G] Union7::from1(B) -> Self[A, B, C, D, E, F, G]
130-
fn[A, B, C : Cast, D, E, F, G] Union7::from2(C) -> Self[A, B, C, D, E, F, G]
131-
fn[A, B, C, D : Cast, E, F, G] Union7::from3(D) -> Self[A, B, C, D, E, F, G]
132-
fn[A, B, C, D, E : Cast, F, G] Union7::from4(E) -> Self[A, B, C, D, E, F, G]
133-
fn[A, B, C, D, E, F : Cast, G] Union7::from5(F) -> Self[A, B, C, D, E, F, G]
134-
fn[A, B, C, D, E, F, G : Cast] Union7::from6(G) -> Self[A, B, C, D, E, F, G]
135-
fn[A : Cast, B, C, D, E, F, G] Union7::to0(Self[A, B, C, D, E, F, G]) -> A?
136-
fn[A, B : Cast, C, D, E, F, G] Union7::to1(Self[A, B, C, D, E, F, G]) -> B?
137-
fn[A, B, C : Cast, D, E, F, G] Union7::to2(Self[A, B, C, D, E, F, G]) -> C?
138-
fn[A, B, C, D : Cast, E, F, G] Union7::to3(Self[A, B, C, D, E, F, G]) -> D?
139-
fn[A, B, C, D, E : Cast, F, G] Union7::to4(Self[A, B, C, D, E, F, G]) -> E?
140-
fn[A, B, C, D, E, F : Cast, G] Union7::to5(Self[A, B, C, D, E, F, G]) -> F?
141-
fn[A, B, C, D, E, F, G : Cast] Union7::to6(Self[A, B, C, D, E, F, G]) -> G?
142-
143-
type Union8[_, _, _, _, _, _, _, _]
144-
fn[A : Cast, B, C, D, E, F, G, H] Union8::from0(A) -> Self[A, B, C, D, E, F, G, H]
145-
fn[A, B : Cast, C, D, E, F, G, H] Union8::from1(B) -> Self[A, B, C, D, E, F, G, H]
146-
fn[A, B, C : Cast, D, E, F, G, H] Union8::from2(C) -> Self[A, B, C, D, E, F, G, H]
147-
fn[A, B, C, D : Cast, E, F, G, H] Union8::from3(D) -> Self[A, B, C, D, E, F, G, H]
148-
fn[A, B, C, D, E : Cast, F, G, H] Union8::from4(E) -> Self[A, B, C, D, E, F, G, H]
149-
fn[A, B, C, D, E, F : Cast, G, H] Union8::from5(F) -> Self[A, B, C, D, E, F, G, H]
150-
fn[A, B, C, D, E, F, G : Cast, H] Union8::from6(G) -> Self[A, B, C, D, E, F, G, H]
151-
fn[A, B, C, D, E, F, G, H : Cast] Union8::from7(H) -> Self[A, B, C, D, E, F, G, H]
152-
fn[A : Cast, B, C, D, E, F, G, H] Union8::to0(Self[A, B, C, D, E, F, G, H]) -> A?
153-
fn[A, B : Cast, C, D, E, F, G, H] Union8::to1(Self[A, B, C, D, E, F, G, H]) -> B?
154-
fn[A, B, C : Cast, D, E, F, G, H] Union8::to2(Self[A, B, C, D, E, F, G, H]) -> C?
155-
fn[A, B, C, D : Cast, E, F, G, H] Union8::to3(Self[A, B, C, D, E, F, G, H]) -> D?
156-
fn[A, B, C, D, E : Cast, F, G, H] Union8::to4(Self[A, B, C, D, E, F, G, H]) -> E?
157-
fn[A, B, C, D, E, F : Cast, G, H] Union8::to5(Self[A, B, C, D, E, F, G, H]) -> F?
158-
fn[A, B, C, D, E, F, G : Cast, H] Union8::to6(Self[A, B, C, D, E, F, G, H]) -> G?
159-
fn[A, B, C, D, E, F, G, H : Cast] Union8::to7(Self[A, B, C, D, E, F, G, H]) -> H?
160-
161-
#external
162-
pub type Value
163-
fn[Arg, Result] Value::apply(Self, Array[Arg]) -> Result
164-
fn[Arg, Result] Value::apply_with_index(Self, Int, Array[Arg]) -> Result
165-
fn[Arg, Result] Value::apply_with_string(Self, String, Array[Arg]) -> Result
166-
fn[Arg, Result] Value::apply_with_symbol(Self, Symbol, Array[Arg]) -> Result
167-
fn[T] Value::cast(Self) -> T
168-
fn[T] Value::cast_from(T) -> Self
169-
fn Value::extends(Self, Self) -> Self
170-
fn Value::from_json(Json) -> Self raise
171-
fn Value::from_json_string(String) -> Self raise
172-
fn[T] Value::get_with_index(Self, Int) -> T
173-
fn[T] Value::get_with_string(Self, String) -> T
174-
fn[T] Value::get_with_symbol(Self, Symbol) -> T
175-
fn Value::is_bool(Self) -> Bool
176-
fn Value::is_null(Self) -> Bool
177-
fn Value::is_number(Self) -> Bool
178-
fn Value::is_object(Self) -> Bool
179-
fn Value::is_string(Self) -> Bool
180-
fn Value::is_symbol(Self) -> Bool
181-
fn Value::is_undefined(Self) -> Bool
182-
fn[Arg, Result] Value::new(Self, Array[Arg]) -> Result
183-
fn[Arg, Result] Value::new_with_index(Self, Int, Array[Arg]) -> Result
184-
fn[Arg, Result] Value::new_with_string(Self, String, Array[Arg]) -> Result
185-
fn[Arg, Result] Value::new_with_symbol(Self, Symbol, Array[Arg]) -> Result
186-
fn[T] Value::set_with_index(Self, Int, T) -> Unit
187-
fn[T] Value::set_with_string(Self, String, T) -> Unit
188-
fn[T] Value::set_with_symbol(Self, Symbol, T) -> Unit
189-
fn Value::to_json(Self) -> Json raise
190-
fn Value::to_json_string(Self) -> String raise
191-
fn Value::to_string(Self) -> String
192-
impl Show for Value
193-
impl @json.FromJson for Value
1949

19510
// Type aliases
19611

19712
// Traits
198-
pub(open) trait Cast {
199-
into(Value) -> Self?
200-
from(Self) -> Value
201-
}
202-
impl Cast for Bool
203-
impl Cast for Int
204-
impl Cast for Double
205-
impl Cast for String
206-
impl[A : Cast] Cast for Array[A]
20713

src/mocket.js.mbt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,7 @@ pub fn serve_ffi(mocket : Mocket, port~ : Int) -> Unit {
273273
let (params, handler) = match
274274
mocket.find_route(req.req_method(), req.url()) {
275275
Some((h, p)) => (p, h)
276-
_ => {
277-
res.write_head(404, @js.Object::new().to_value())
278-
res.end(@js.Value::cast_from("Not Found"))
279-
return
280-
}
276+
_ => ({}, handle_not_found)
281277
}
282278
let event = {
283279
req: {

src/mocket.native.mbt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ extern "c" fn HttpRequestInternal::req_body_len(
5757
self : HttpRequestInternal,
5858
) -> Int = "req_body_len"
5959

60-
///|
61-
#owned(self, body)
62-
extern "c" fn HttpResponseInternal::end(
63-
self : HttpResponseInternal,
64-
body : @native.CStr,
65-
) -> Unit = "res_end"
60+
// ///|
61+
// #owned(self, body)
62+
// extern "c" fn HttpResponseInternal::end(
63+
// self : HttpResponseInternal,
64+
// body : @native.CStr,
65+
// ) -> Unit = "res_end"
6666

6767
///|
6868
#owned(self, body)
@@ -184,11 +184,7 @@ fn handle_request_native(
184184
})
185185
let (params, handler) = match mocket.find_route(http_method, path) {
186186
Some((h, p)) => (p, h)
187-
_ => {
188-
res.status(404)
189-
res.end(to_cstr("Not Found"))
190-
return
191-
}
187+
_ => ({}, handle_not_found)
192188
}
193189
let event = {
194190
req: { http_method, url, raw_body: "", headers: req_headers },
@@ -215,6 +211,9 @@ fn handle_request_native(
215211
to_cstr("Set-Cookie"),
216212
to_cstr(cookie.to_string()),
217213
))
214+
let buf = @buffer.new()
215+
responder.output(buf)
216+
event.res.raw_body = buf.to_bytes()
218217
res.end_bytes(event.res.raw_body)
219218
})
220219
}
@@ -291,7 +290,7 @@ extern "c" fn ws_send_bytes_len_native(
291290
extern "c" fn ws_pong_native(id : @native.CStr) -> Unit = "ws_pong"
292291

293292
///|
294-
extern "c" fn ws_msg_body() -> Bytes = "ws_msg_body"
293+
// extern "c" fn ws_msg_body() -> Bytes = "ws_msg_body"
295294

296295
///|
297296
extern "c" fn ws_msg_body_len() -> Int = "ws_msg_body_len"

src/not_found.mbt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
///|
2+
pub async fn handle_not_found(_ : MocketEvent) -> &Responder noraise {
3+
HttpResponse::new(404).body("Not Found")
4+
}

src/pkg.generated.mbti

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,23 @@
22
package "oboard/mocket"
33

44
import(
5+
"illusory0x0/native"
56
"moonbitlang/core/buffer"
6-
"oboard/mocket/js"
77
)
88

99
// Values
10-
fn __get_port_by_connection_js(String) -> Int
11-
12-
fn __get_port_by_connection_js_export((String) -> Int) -> Unit
13-
14-
fn __ws_emit_js_export((String, Int, String, Bytes) -> Unit) -> Unit
15-
16-
fn __ws_emit_js_port(String, Int, String, Bytes) -> Unit
17-
18-
fn __ws_get_members_js(String) -> Array[String]
19-
20-
fn __ws_state_js_export((String, String) -> Unit, (String, String) -> Unit, (String) -> Array[String]) -> Unit
21-
22-
fn __ws_subscribe_js(String, String) -> Unit
23-
24-
fn __ws_unsubscribe_js(String, String) -> Unit
10+
fn __ws_emit(@native.CStr, @native.CStr, @native.CStr) -> Unit
2511

2612
fn async_run(async () -> Unit noraise) -> Unit
2713

2814
fn cookie_to_string(Array[CookieItem]) -> String
2915

30-
fn create_server((HttpRequestInternal, HttpResponseInternal, () -> Unit) -> Unit, Int) -> Unit
31-
32-
fn encode_multipart(Map[String, MultipartFormValue], String) -> String
33-
3416
async fn execute_middlewares(Array[(String, async (MocketEvent, async () -> &Responder noraise) -> &Responder noraise)], MocketEvent, async (MocketEvent) -> &Responder noraise) -> &Responder noraise
3517

3618
fn form_encode(Map[String, String]) -> String
3719

20+
async fn handle_not_found(MocketEvent) -> &Responder noraise
21+
3822
fn html(&Show) -> &Responder
3923

4024
fn new(base_path? : String) -> Mocket
@@ -108,8 +92,8 @@ impl Responder for HttpRequest
10892

10993
#external
11094
pub type HttpRequestInternal
111-
fn HttpRequestInternal::req_method(Self) -> String
112-
fn HttpRequestInternal::url(Self) -> String
95+
fn HttpRequestInternal::on_complete(Self, FuncRef[() -> Unit]) -> Unit
96+
fn HttpRequestInternal::on_headers(Self, FuncRef[(@native.CStr) -> Unit]) -> Unit
11397

11498
pub(all) struct HttpResponse {
11599
mut status_code : &StatusCoder
@@ -127,8 +111,9 @@ impl Responder for HttpResponse
127111

128112
#external
129113
pub type HttpResponseInternal
130-
fn HttpResponseInternal::end(Self, @js.Value) -> Unit
131-
fn HttpResponseInternal::url(Self) -> String
114+
115+
#external
116+
pub type HttpServerInternal
132117

133118
#alias(T)
134119
pub(all) struct Mocket {
@@ -189,6 +174,7 @@ pub(all) struct StaticAssetMeta {
189174
size : Int64?
190175
encoding : String?
191176
}
177+
fn StaticAssetMeta::new(asset_type? : String, etag? : String, mtime? : Int64, path? : String, size? : Int64, encoding? : String) -> Self
192178

193179
pub(all) enum StatusCode {
194180
Continue

0 commit comments

Comments
 (0)