From 3386036f0c244b9e05c2701afd09f8d8fa2b2b9a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 16 Dec 2025 12:37:06 +0200 Subject: [PATCH] tools,examples,veb: fix `v -d trace_before_request run examples/veb/veb_example.v` after feedback from https://youtu.be/IuE6Bo1klK0?t=2100 --- cmd/tools/vtest-all.v | 5 +++++ examples/veb/veb_example.v | 4 ++-- vlib/veb/middleware.v | 4 ---- vlib/veb/tests/veb_app_test.v | 26 ++++++++++++++++++++------ vlib/veb/veb.v | 9 +++++++-- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/cmd/tools/vtest-all.v b/cmd/tools/vtest-all.v index fc2fac7e1..e9c69eaa5 100644 --- a/cmd/tools/vtest-all.v +++ b/cmd/tools/vtest-all.v @@ -411,6 +411,11 @@ fn get_all_commands() []Command { okmsg: 'A simple veb app, compiles with `-gc none -no-retry-compilation -cc tcc -d use_openssl` on macos and linux' rmfile: 'examples/veb/todo/main' } + res << Command{ + line: '${vexe} -d trace_before_request examples/veb/veb_example.v' + okmsg: 'examples/veb/veb_example.v compiles with `-d trace_before_request` on macos and linux' + rmfile: 'examples/veb/veb_example' + } } $if linux { res << Command{ diff --git a/examples/veb/veb_example.v b/examples/veb/veb_example.v index 892f44389..bb488dbb6 100644 --- a/examples/veb/veb_example.v +++ b/examples/veb/veb_example.v @@ -19,9 +19,9 @@ struct Context { veb.Context } -pub fn (app &App) before_request() { +pub fn (ctx &Context) before_request() { $if trace_before_request ? { - eprintln('[veb] before_request: ${app.req.method} ${app.req.url}') + eprintln('[veb] before_request: ${ctx.req.method} ${ctx.req.url}') } } diff --git a/vlib/veb/middleware.v b/vlib/veb/middleware.v index 1f63c6743..e5beed325 100644 --- a/vlib/veb/middleware.v +++ b/vlib/veb/middleware.v @@ -282,10 +282,6 @@ pub fn decode_zstd[T]() MiddlewareOptions[T] { } } -interface HasBeforeRequest { - before_request() -} - pub const cors_safelisted_response_headers = [http.CommonHeader.cache_control, .content_language, .content_length, .content_type, .expires, .last_modified, .pragma].map(it.str()).join(',') diff --git a/vlib/veb/tests/veb_app_test.v b/vlib/veb/tests/veb_app_test.v index 12d265c42..a5ae02cbe 100644 --- a/vlib/veb/tests/veb_app_test.v +++ b/vlib/veb/tests/veb_app_test.v @@ -1,6 +1,7 @@ // vtest build: present_sqlite3? // imports db.sqlite import veb import time +import net.http import db.sqlite const port = 13004 @@ -8,7 +9,12 @@ const port = 13004 pub struct Context { veb.Context pub mut: - user_id string + cid int +} + +pub fn (mut ctx Context) before_request() { + dump(@LOCATION) + ctx.cid = 123 } pub struct App { @@ -18,6 +24,7 @@ pub mut: } pub fn (mut app App) before_accept_loop() { + dump(@LOCATION) app.started <- true } @@ -36,10 +43,12 @@ fn test_veb_application_compiles() { spawn veb.run_at[App, Context](mut app, port: port, family: .ip, timeout_in_seconds: 2) // app startup time _ := <-app.started -} - -pub fn (mut ctx Context) before_request() { - ctx.user_id = ctx.get_cookie('id') or { '0' } + res := http.fetch(url: 'http://127.0.0.1:${port}/get_cid')! + assert res.status_code == 200 + assert res.body == '123' + okres := http.fetch(url: 'http://127.0.0.1:${port}/ok')! + assert okres.status_code == 200 + assert okres.body == '{"success":true,"result":123}' } @['/new_article'; post] @@ -103,7 +112,12 @@ fn (mut app App) some_helper[T](result T) ApiSuccessResponse[T] { // should compile, the route method itself is not generic fn (mut app App) ok(mut ctx Context) veb.Result { - return ctx.json(app.some_helper(123)) + return ctx.json(app.some_helper(ctx.cid)) +} + +// should compile, the route method itself is not generic +fn (mut app App) get_cid(mut ctx Context) veb.Result { + return ctx.text(ctx.cid.str()) } struct ExampleStruct { diff --git a/vlib/veb/veb.v b/vlib/veb/veb.v index 78ea70017..32e7a1195 100644 --- a/vlib/veb/veb.v +++ b/vlib/veb/veb.v @@ -148,6 +148,11 @@ mut: before_accept_loop() } +interface HasBeforeRequestOnContext { +mut: + before_request() +} + fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string, routes &map[string]Route) { mut route := Route{} 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 } // first execute before_request - $if A is HasBeforeRequest { - app.before_request() + $if X is HasBeforeRequestOnContext { + user_context.before_request() } // user_context.before_request() if user_context.Context.done { -- 2.39.5