diff --git a/cmd/tools/gen_vc.v b/cmd/tools/gen_vc.v index e76aac76ce..6cdaca3790 100644 --- a/cmd/tools/gen_vc.v +++ b/cmd/tools/gen_vc.v @@ -148,7 +148,7 @@ fn new_gen_vc(flag_options FlagOptions) &GenVC { } // WebhookServer init -pub fn (mut ws WebhookServer) init_once() { +pub fn (mut ws WebhookServer) init_server() { mut fp := flag.new_flag_parser(os.args.clone()) flag_options := parse_flags(mut fp) ws.gen_vc = new_gen_vc(flag_options) @@ -156,9 +156,11 @@ pub fn (mut ws WebhookServer) init_once() { // ws.gen_vc = new_gen_vc(flag_options) } +/* pub fn (mut ws WebhookServer) init() { // ws.init_once() } +*/ pub fn (mut ws WebhookServer) index() { eprintln('WebhookServer.index() called') diff --git a/examples/vweb/server_sent_events/server.v b/examples/vweb/server_sent_events/server.v index b4e66f935c..0cbba51370 100644 --- a/examples/vweb/server_sent_events/server.v +++ b/examples/vweb/server_sent_events/server.v @@ -14,7 +14,7 @@ fn main() { vweb.run(8081) } -pub fn (mut app App) init_once() { +pub fn (mut app App) init_server() { app.serve_static('/favicon.ico', 'favicon.ico', 'img/x-icon') app.mount_static_folder_at(os.resource_abs_path('.'), '/') } diff --git a/tutorials/building_a_simple_web_blog_with_vweb/README.md b/tutorials/building_a_simple_web_blog_with_vweb/README.md index 2fb69d5055..74c1cc8a65 100644 --- a/tutorials/building_a_simple_web_blog_with_vweb/README.md +++ b/tutorials/building_a_simple_web_blog_with_vweb/README.md @@ -73,11 +73,6 @@ pub fn (mut app App) index() vweb.Result { return app.text('Hello world from vweb!') } -pub fn (app &App) init() { -} - -pub fn (app &App) init_once() { -} ``` Run it with @@ -228,10 +223,10 @@ mut: -Modify the `init_once()` method we created earlier to connect to a database: +Add the `init_server()` method where we'll connect to a database: ```v oksyntax -pub fn (mut app App) init_once() { +pub fn (mut app App) init_server() { db := sqlite.connect(':memory:') or { panic(err) } db.exec('create table `Article` (id integer primary key, title text default "", text text default "")') db.exec('insert into Article (title, text) values ("Hello, world!", "V is great.")') @@ -240,7 +235,7 @@ pub fn (mut app App) init_once() { } ``` -Code in the `init_once()` function is run only once during app's startup, so we are going +Code in the `init_server()` function is run only once during app's startup, so we are going to have one DB connection for all requests. Create a new file `article.v`: diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v b/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v index 15e17ad586..6e56e59b81 100644 --- a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v +++ b/tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v @@ -8,7 +8,8 @@ import json struct App { vweb.Context mut: - db sqlite.DB + db sqlite.DB [server_var] + user_id string } fn main() { @@ -31,7 +32,7 @@ pub fn (app &App) index() vweb.Result { return $vweb.html() } -pub fn (mut app App) init_once() { +pub fn (mut app App) init_server() { app.db = sqlite.connect('blog.db') or { panic(err) } app.db.create_table('article', [ 'id integer primary key', @@ -40,7 +41,8 @@ pub fn (mut app App) init_once() { ]) } -pub fn (mut app App) init() { +pub fn (mut app App) before_request() { + app.user_id = app.get_cookie('id') or { '0' } } pub fn (mut app App) new() vweb.Result { diff --git a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/index.html b/tutorials/building_a_simple_web_blog_with_vweb/code/blog/index.html index 7ed9bcc85f..399b2670d3 100644 --- a/tutorials/building_a_simple_web_blog_with_vweb/code/blog/index.html +++ b/tutorials/building_a_simple_web_blog_with_vweb/code/blog/index.html @@ -3,6 +3,7 @@ V Blog + user id = @app.user_id
@for article in articles
@article.title
diff --git a/vlib/v/checker/tests/fn_args.out b/vlib/v/checker/tests/fn_args.out index d7282bdfcd..fd5f670d87 100644 --- a/vlib/v/checker/tests/fn_args.out +++ b/vlib/v/checker/tests/fn_args.out @@ -1,19 +1,19 @@ vlib/v/checker/tests/fn_args.vv:7:5: error: cannot use `&int` as `byte` in argument 1 to `u8` 5 | fn basic() { 6 | v := 4 - 7 | u8(&v) + 7 | uu8(&v) | ~~ 8 | arr([5]!) 9 | fun(fn(i &int){}) vlib/v/checker/tests/fn_args.vv:8:6: error: cannot use `[1]int` as `[]int` in argument 1 to `arr` 6 | v := 4 - 7 | u8(&v) + 7 | uu8(&v) 8 | arr([5]!) | ~~~~ 9 | fun(fn(i &int){}) 10 | } vlib/v/checker/tests/fn_args.vv:9:6: error: cannot use `fn (&int)` as `fn (int)` in argument 1 to `fun` - 7 | u8(&v) + 7 | uu8(&v) 8 | arr([5]!) 9 | fun(fn(i &int){}) | ~~~~~~~~~~~~ diff --git a/vlib/v/checker/tests/fn_args.vv b/vlib/v/checker/tests/fn_args.vv index b1da22ce8b..7ba79a97df 100644 --- a/vlib/v/checker/tests/fn_args.vv +++ b/vlib/v/checker/tests/fn_args.vv @@ -1,10 +1,10 @@ -fn u8(a byte) {} +fn uu8(a byte) {} fn arr(a []int) {} fn fun(a fn(int)) {} fn basic() { v := 4 - u8(&v) + uu8(&v) arr([5]!) fun(fn(i &int){}) } diff --git a/vlib/v/checker/tests/vweb_routing_checks.vv b/vlib/v/checker/tests/vweb_routing_checks.vv index e016623388..fbd329f28e 100644 --- a/vlib/v/checker/tests/vweb_routing_checks.vv +++ b/vlib/v/checker/tests/vweb_routing_checks.vv @@ -27,6 +27,7 @@ pub fn (mut app App) cow() vweb.Result { return app.html('works') } +/* pub fn (app App) init_once() { // } @@ -34,6 +35,7 @@ pub fn (app App) init_once() { pub fn (app App) init() { // } +*/ pub fn (mut app App) index() { app.html('hello') diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index e81a5f63ff..5aedb745ac 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -79,11 +79,11 @@ struct MultiplePathAttributesError { code int } -// declaring init_once in your App struct is optional -pub fn (ctx Context) init_once() {} +// declaring init_server in your App struct is optional +pub fn (ctx Context) init_server() {} -// declaring init in your App struct is optional -pub fn (ctx Context) init() {} +// declaring before_request in your App struct is optional +pub fn (ctx Context) before_request() {} pub struct Cookie { name string @@ -295,7 +295,7 @@ pub fn run_app(mut app T, port int) { app.Context = Context{ conn: 0 } - app.init_once() + app.init_server() $for method in T.methods { $if method.return_type is Result { // check routes for validity @@ -364,7 +364,7 @@ fn handle_conn(mut conn net.TcpConn, mut app T) { return } - app.init() + app.before_request() // Call the right action $if debug { println('route matching...')