Skip to content

checker: fix veb route method param with non ctx name (fix #23105)#23107

Merged
spytheman merged 2 commits intovlang:masterfrom
yuyi98:fix_veb_route
Dec 9, 2024
Merged

checker: fix veb route method param with non ctx name (fix #23105)#23107
spytheman merged 2 commits intovlang:masterfrom
yuyi98:fix_veb_route

Conversation

@yuyi98
Copy link
Copy Markdown
Member

@yuyi98 yuyi98 commented Dec 9, 2024

This PR fix veb route method param with non ctx name (fix #23105).

  • Fix veb route method param with non ctx name.
  • Add test.
import veb

pub struct User {
pub mut:
	name string
	id   int
}

// Our context struct must embed `veb.Context`!
pub struct Context {
	veb.Context
pub mut:
	// In the context struct we store data that could be different
	// for each request. Like a User struct or a session id
	user       User
	session_id string
}

pub struct App {
pub:
	// In the app struct we store data that should be accessible by all endpoints.
	// For example, a database or configuration values.
	secret_key string
}

// This is how endpoints are defined in veb. This is the index route
pub fn (app &App) index(mut context Context) veb.Result {
	return context.text('Hello V! The secret key is "${app.secret_key}"')
}

fn main() {
	mut app := &App{
		secret_key: 'secret'
	}
	// Pass the App and context type and start the web server on port 8080
	veb.run[App, Context](mut app, 8080)
}

PS D:\Test\v\tt1> v run .
[veb] Running app on http://localhost:8080/

Huly®: V_0.6-21544

Copy link
Copy Markdown
Contributor

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman
Copy link
Copy Markdown
Contributor

@medvednikov ... that veb.Context implicit handling in the compiler, is a very big hack, and I suspect that it will continue to trip users :-| .

A big reason for veb instead of vweb, was to minimize the amount of special cases for its support in the compiler, in favor of using general features, supported for all user code :-( ...

@spytheman spytheman merged commit 6f75872 into vlang:master Dec 9, 2024
@yuyi98 yuyi98 deleted the fix_veb_route branch December 10, 2024 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C error in veb example

2 participants