Skip to content

Commit a18f85c

Browse files
committed
vweb: init_once() => init_server(); init() => before_request()
1 parent 3a134ac commit a18f85c

File tree

9 files changed

+26
-24
lines changed

9 files changed

+26
-24
lines changed

cmd/tools/gen_vc.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,19 @@ fn new_gen_vc(flag_options FlagOptions) &GenVC {
148148
}
149149

150150
// WebhookServer init
151-
pub fn (mut ws WebhookServer) init_once() {
151+
pub fn (mut ws WebhookServer) init_server() {
152152
mut fp := flag.new_flag_parser(os.args.clone())
153153
flag_options := parse_flags(mut fp)
154154
ws.gen_vc = new_gen_vc(flag_options)
155155
ws.gen_vc.init()
156156
// ws.gen_vc = new_gen_vc(flag_options)
157157
}
158158

159+
/*
159160
pub fn (mut ws WebhookServer) init() {
160161
// ws.init_once()
161162
}
163+
*/
162164

163165
pub fn (mut ws WebhookServer) index() {
164166
eprintln('WebhookServer.index() called')

examples/vweb/server_sent_events/server.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
vweb.run<App>(8081)
1515
}
1616

17-
pub fn (mut app App) init_once() {
17+
pub fn (mut app App) init_server() {
1818
app.serve_static('/favicon.ico', 'favicon.ico', 'img/x-icon')
1919
app.mount_static_folder_at(os.resource_abs_path('.'), '/')
2020
}

tutorials/building_a_simple_web_blog_with_vweb/README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ pub fn (mut app App) index() vweb.Result {
7373
return app.text('Hello world from vweb!')
7474
}
7575
76-
pub fn (app &App) init() {
77-
}
78-
79-
pub fn (app &App) init_once() {
80-
}
8176
```
8277

8378
Run it with
@@ -228,10 +223,10 @@ mut:
228223

229224

230225

231-
Modify the `init_once()` method we created earlier to connect to a database:
226+
Add the `init_server()` method where we'll connect to a database:
232227

233228
```v oksyntax
234-
pub fn (mut app App) init_once() {
229+
pub fn (mut app App) init_server() {
235230
db := sqlite.connect(':memory:') or { panic(err) }
236231
db.exec('create table `Article` (id integer primary key, title text default "", text text default "")')
237232
db.exec('insert into Article (title, text) values ("Hello, world!", "V is great.")')
@@ -240,7 +235,7 @@ pub fn (mut app App) init_once() {
240235
}
241236
```
242237

243-
Code in the `init_once()` function is run only once during app's startup, so we are going
238+
Code in the `init_server()` function is run only once during app's startup, so we are going
244239
to have one DB connection for all requests.
245240

246241
Create a new file `article.v`:

tutorials/building_a_simple_web_blog_with_vweb/code/blog/blog.v

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import json
88
struct App {
99
vweb.Context
1010
mut:
11-
db sqlite.DB
11+
db sqlite.DB [server_var]
12+
user_id string
1213
}
1314

1415
fn main() {
@@ -31,7 +32,7 @@ pub fn (app &App) index() vweb.Result {
3132
return $vweb.html()
3233
}
3334

34-
pub fn (mut app App) init_once() {
35+
pub fn (mut app App) init_server() {
3536
app.db = sqlite.connect('blog.db') or { panic(err) }
3637
app.db.create_table('article', [
3738
'id integer primary key',
@@ -40,7 +41,8 @@ pub fn (mut app App) init_once() {
4041
])
4142
}
4243

43-
pub fn (mut app App) init() {
44+
pub fn (mut app App) before_request() {
45+
app.user_id = app.get_cookie('id') or { '0' }
4446
}
4547

4648
pub fn (mut app App) new() vweb.Result {

tutorials/building_a_simple_web_blog_with_vweb/code/blog/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<title>V Blog</title>
44
</header>
55
<body>
6+
user id = @app.user_id <br>
67
@for article in articles
78
<div>
89
<b>@article.title</b> <br>

vlib/v/checker/tests/fn_args.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
vlib/v/checker/tests/fn_args.vv:7:5: error: cannot use `&int` as `byte` in argument 1 to `u8`
22
5 | fn basic() {
33
6 | v := 4
4-
7 | u8(&v)
4+
7 | uu8(&v)
55
| ~~
66
8 | arr([5]!)
77
9 | fun(fn(i &int){})
88
vlib/v/checker/tests/fn_args.vv:8:6: error: cannot use `[1]int` as `[]int` in argument 1 to `arr`
99
6 | v := 4
10-
7 | u8(&v)
10+
7 | uu8(&v)
1111
8 | arr([5]!)
1212
| ~~~~
1313
9 | fun(fn(i &int){})
1414
10 | }
1515
vlib/v/checker/tests/fn_args.vv:9:6: error: cannot use `fn (&int)` as `fn (int)` in argument 1 to `fun`
16-
7 | u8(&v)
16+
7 | uu8(&v)
1717
8 | arr([5]!)
1818
9 | fun(fn(i &int){})
1919
| ~~~~~~~~~~~~

vlib/v/checker/tests/fn_args.vv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
fn u8(a byte) {}
1+
fn uu8(a byte) {}
22
fn arr(a []int) {}
33
fn fun(a fn(int)) {}
44

55
fn basic() {
66
v := 4
7-
u8(&v)
7+
uu8(&v)
88
arr([5]!)
99
fun(fn(i &int){})
1010
}

vlib/v/checker/tests/vweb_routing_checks.vv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ pub fn (mut app App) cow() vweb.Result {
2727
return app.html('works')
2828
}
2929

30+
/*
3031
pub fn (app App) init_once() {
3132
//
3233
}
3334

3435
pub fn (app App) init() {
3536
//
3637
}
38+
*/
3739

3840
pub fn (mut app App) index() {
3941
app.html('hello')

vlib/vweb/vweb.v

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ struct MultiplePathAttributesError {
7979
code int
8080
}
8181

82-
// declaring init_once in your App struct is optional
83-
pub fn (ctx Context) init_once() {}
82+
// declaring init_server in your App struct is optional
83+
pub fn (ctx Context) init_server() {}
8484

85-
// declaring init in your App struct is optional
86-
pub fn (ctx Context) init() {}
85+
// declaring before_request in your App struct is optional
86+
pub fn (ctx Context) before_request() {}
8787

8888
pub struct Cookie {
8989
name string
@@ -295,7 +295,7 @@ pub fn run_app<T>(mut app T, port int) {
295295
app.Context = Context{
296296
conn: 0
297297
}
298-
app.init_once()
298+
app.init_server()
299299
$for method in T.methods {
300300
$if method.return_type is Result {
301301
// check routes for validity
@@ -364,7 +364,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
364364
return
365365
}
366366

367-
app.init()
367+
app.before_request()
368368
// Call the right action
369369
$if debug {
370370
println('route matching...')

0 commit comments

Comments
 (0)