vweb: init_once() => init_server(); init() => before_request()

pull/9739/head
Alexander Medvednikov 2021-04-15 06:27:24 +03:00
parent 3a134acc5a
commit a18f85c8cd
9 changed files with 26 additions and 24 deletions

View File

@ -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')

View File

@ -14,7 +14,7 @@ fn main() {
vweb.run<App>(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('.'), '/')
}

View File

@ -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`:

View File

@ -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 {

View File

@ -3,6 +3,7 @@
<title>V Blog</title>
</header>
<body>
user id = @app.user_id <br>
@for article in articles
<div>
<b>@article.title</b> <br>

View File

@ -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){})
| ~~~~~~~~~~~~

View File

@ -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){})
}

View File

@ -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')

View File

@ -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<T>(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<T>(mut conn net.TcpConn, mut app T) {
return
}
app.init()
app.before_request()
// Call the right action
$if debug {
println('route matching...')