Skip to content

Commit 3386036

Browse files
committed
tools,examples,veb: fix v -d trace_before_request run examples/veb/veb_example.v after feedback from https://youtu.be/IuE6Bo1klK0?t=2100
1 parent 0e60775 commit 3386036

5 files changed

Lines changed: 34 additions & 14 deletions

File tree

‎cmd/tools/vtest-all.v‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ fn get_all_commands() []Command {
411411
okmsg: 'A simple veb app, compiles with `-gc none -no-retry-compilation -cc tcc -d use_openssl` on macos and linux'
412412
rmfile: 'examples/veb/todo/main'
413413
}
414+
res << Command{
415+
line: '${vexe} -d trace_before_request examples/veb/veb_example.v'
416+
okmsg: 'examples/veb/veb_example.v compiles with `-d trace_before_request` on macos and linux'
417+
rmfile: 'examples/veb/veb_example'
418+
}
414419
}
415420
$if linux {
416421
res << Command{

‎examples/veb/veb_example.v‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ struct Context {
1919
veb.Context
2020
}
2121

22-
pub fn (app &App) before_request() {
22+
pub fn (ctx &Context) before_request() {
2323
$if trace_before_request ? {
24-
eprintln('[veb] before_request: ${app.req.method} ${app.req.url}')
24+
eprintln('[veb] before_request: ${ctx.req.method} ${ctx.req.url}')
2525
}
2626
}
2727

‎vlib/veb/middleware.v‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,6 @@ pub fn decode_zstd[T]() MiddlewareOptions[T] {
282282
}
283283
}
284284

285-
interface HasBeforeRequest {
286-
before_request()
287-
}
288-
289285
pub const cors_safelisted_response_headers = [http.CommonHeader.cache_control, .content_language,
290286
.content_length, .content_type, .expires, .last_modified, .pragma].map(it.str()).join(',')
291287

‎vlib/veb/tests/veb_app_test.v‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// vtest build: present_sqlite3? // imports db.sqlite
22
import veb
33
import time
4+
import net.http
45
import db.sqlite
56

67
const port = 13004
78

89
pub struct Context {
910
veb.Context
1011
pub mut:
11-
user_id string
12+
cid int
13+
}
14+
15+
pub fn (mut ctx Context) before_request() {
16+
dump(@LOCATION)
17+
ctx.cid = 123
1218
}
1319

1420
pub struct App {
@@ -18,6 +24,7 @@ pub mut:
1824
}
1925

2026
pub fn (mut app App) before_accept_loop() {
27+
dump(@LOCATION)
2128
app.started <- true
2229
}
2330

@@ -36,10 +43,12 @@ fn test_veb_application_compiles() {
3643
spawn veb.run_at[App, Context](mut app, port: port, family: .ip, timeout_in_seconds: 2)
3744
// app startup time
3845
_ := <-app.started
39-
}
40-
41-
pub fn (mut ctx Context) before_request() {
42-
ctx.user_id = ctx.get_cookie('id') or { '0' }
46+
res := http.fetch(url: 'http://127.0.0.1:${port}/get_cid')!
47+
assert res.status_code == 200
48+
assert res.body == '123'
49+
okres := http.fetch(url: 'http://127.0.0.1:${port}/ok')!
50+
assert okres.status_code == 200
51+
assert okres.body == '{"success":true,"result":123}'
4352
}
4453

4554
@['/new_article'; post]
@@ -103,7 +112,12 @@ fn (mut app App) some_helper[T](result T) ApiSuccessResponse[T] {
103112

104113
// should compile, the route method itself is not generic
105114
fn (mut app App) ok(mut ctx Context) veb.Result {
106-
return ctx.json(app.some_helper(123))
115+
return ctx.json(app.some_helper(ctx.cid))
116+
}
117+
118+
// should compile, the route method itself is not generic
119+
fn (mut app App) get_cid(mut ctx Context) veb.Result {
120+
return ctx.text(ctx.cid.str())
107121
}
108122

109123
struct ExampleStruct {

‎vlib/veb/veb.v‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ mut:
148148
before_accept_loop()
149149
}
150150

151+
interface HasBeforeRequestOnContext {
152+
mut:
153+
before_request()
154+
}
155+
151156
fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string, routes &map[string]Route) {
152157
mut route := Route{}
153158
mut middleware_has_sent_response := false
@@ -201,8 +206,8 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
201206
}
202207

203208
// first execute before_request
204-
$if A is HasBeforeRequest {
205-
app.before_request()
209+
$if X is HasBeforeRequestOnContext {
210+
user_context.before_request()
206211
}
207212
// user_context.before_request()
208213
if user_context.Context.done {

0 commit comments

Comments
 (0)