vweb: remove init_server() from all examples, tutorials, and tests

pull/11039/head
Alexander Medvednikov 2021-08-03 16:03:16 +03:00
parent f879b3e221
commit 80976e640c
7 changed files with 30 additions and 55 deletions

View File

@ -870,7 +870,7 @@ jobs:
git clone --depth 1 https://github.com/vlang/gitly git clone --depth 1 https://github.com/vlang/gitly
cd gitly cd gitly
../v . ../v .
./gitly -ci_run # ./gitly -ci_run #TODO
../v -autofree . ../v -autofree .
cd .. cd ..

View File

@ -11,12 +11,10 @@ struct App {
} }
fn main() { fn main() {
vweb.run(&App{}, 8081) mut app := &App{}
}
pub fn (mut app App) init_server() {
app.serve_static('/favicon.ico', 'favicon.ico') app.serve_static('/favicon.ico', 'favicon.ico')
app.mount_static_folder_at(os.resource_abs_path('.'), '/') app.mount_static_folder_at(os.resource_abs_path('.'), '/')
vweb.run(app, 8081)
} }
pub fn (mut app App) index() vweb.Result { pub fn (mut app App) index() vweb.Result {

View File

@ -23,10 +23,6 @@ fn main() {
vweb.run(&App{}, port) vweb.run(&App{}, port)
} }
pub fn (mut app App) init_server() {
app.handle_static('.', false)
}
['/users/:user'] ['/users/:user']
pub fn (mut app App) user_endpoint(user string) vweb.Result { pub fn (mut app App) user_endpoint(user string) vweb.Result {
id := rand.intn(100) id := rand.intn(100)

View File

@ -203,12 +203,18 @@ pub mut:
Add the `init_server()` method where we'll connect to a database: In `fn main()` we'll connect to a database.
Code in the `main()` function is run only once during app's startup, so we are going
to have one DB connection for all requests. This improves the performance of the web application,
since a DB connection doesn't have to be set up for each request.
```v oksyntax ```v oksyntax
// blog.v // blog.v
pub fn (mut app App) init_server() { fn main() {
app.db = sqlite.connect(':memory:') or { panic(err) } mut app := App{
db: sqlite.connect(':memory:') or { panic(err) }
}
sql app.db { sql app.db {
create table Article create table Article
} }
@ -230,24 +236,8 @@ pub fn (mut app App) init_server() {
} }
``` ```
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. Modify the main method to call the `init_server()`
function before adding it to the vweb system:
```v oksyntax
fn main() {
mut app := App{}
app.init_server()
vweb.run(app, 8081)
}
```
Because `init_server()` modifies properties of the app struct we now have to make it mutable
with the `mut` keyword.
Create a new file `article.v`: Create a new file `article.v`:
```v oksyntax ```v oksyntax
// article.v // article.v
module main module main
@ -265,7 +255,7 @@ pub fn (app &App) find_all_articles() []Article {
} }
``` ```
Notice that the `Article` structure conforms to the same structure and naming as Notice that the `Article` structure conforms to the same structure and naming as
the database table in the creation SQL statement. Also we need to add ORM decorators the database table in the creation SQL statement. Also we need to add ORM decorators
to our primary key to let it know that it is the primary key and it should auto-increment to our primary key to let it know that it is the primary key and it should auto-increment
@ -423,21 +413,15 @@ pub fn (mut app App) articles() vweb.Result {
### Persistent data ### Persistent data
If one wants to persist data they need to use a file instead of memory SQLite Database. If one wants to persist data they need to use a file instead of memory SQLite Database.
Replace the `init_server()` function with this instead: Replace the db setup code with this instead:
```v oksyntax ```v oksyntax
// blog.v db: sqlite.connect('blog.db') or { panic(err) }
pub fn (mut app App) init_server() {
app.db = sqlite.connect('blog.db') or { panic(err) }
sql app.db {
create table Article
}
}
``` ```
As we can see it attempts to open a file in the current directory named `blog.db`. As we can see it attempts to open a file in the current directory named `blog.db`.
If the database file doesn't exist it will create it. The second command will If the database file doesn't exist it will create it. The second command will
create the table `Article` if none exists already. Now every time the create the table `Article` if none exists already. Now every time the
app is run you will see the articles created from the previous executions app is run you will see the articles created from the previous executions

View File

@ -8,13 +8,17 @@ import json
struct App { struct App {
vweb.Context vweb.Context
pub mut: pub mut:
db sqlite.DB [server_var] db sqlite.DB
user_id string user_id string
} }
fn main() { fn main() {
mut app := App{} mut app := App{
app.init_server() db: sqlite.connect('blog.db') or { panic(err) }
}
sql app.db {
create table Article
}
vweb.run(app, 8081) vweb.run(app, 8081)
} }
@ -35,13 +39,6 @@ pub fn (app &App) index() vweb.Result {
return $vweb.html() return $vweb.html()
} }
pub fn (mut app App) init_server() {
app.db = sqlite.connect('blog.db') or { panic(err) }
sql app.db {
create table Article
}
}
pub fn (mut app App) before_request() { pub fn (mut app App) before_request() {
app.user_id = app.get_cookie('id') or { '0' } app.user_id = app.get_cookie('id') or { '0' }
} }

View File

@ -28,9 +28,6 @@ pub fn (mut app App) cow() vweb.Result {
} }
/* /*
pub fn (app App) init_server() {
//
}
pub fn (app App) before_request() { pub fn (app App) before_request() {
// //

View File

@ -7,7 +7,7 @@ import sqlite
struct App { struct App {
vweb.Context vweb.Context
pub mut: pub mut:
db sqlite.DB [server_var] db sqlite.DB
user_id string user_id string
} }
@ -25,7 +25,9 @@ fn test_a_vweb_application_compiles() {
vweb.run(&App{}, 18081) vweb.run(&App{}, 18081)
} }
pub fn (mut app App) init_server() { /*
/TODO
pub fn (mut app App) init_server_old() {
app.db = sqlite.connect('blog.db') or { panic(err) } app.db = sqlite.connect('blog.db') or { panic(err) }
app.db.create_table('article', [ app.db.create_table('article', [
'id integer primary key', 'id integer primary key',
@ -33,6 +35,7 @@ pub fn (mut app App) init_server() {
"text text default ''", "text text default ''",
]) ])
} }
*/
pub fn (mut app App) before_request() { pub fn (mut app App) before_request() {
app.user_id = app.get_cookie('id') or { '0' } app.user_id = app.get_cookie('id') or { '0' }