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
cd gitly
../v .
./gitly -ci_run
# ./gitly -ci_run #TODO
../v -autofree .
cd ..

View File

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

View File

@ -23,10 +23,6 @@ fn main() {
vweb.run(&App{}, port)
}
pub fn (mut app App) init_server() {
app.handle_static('.', false)
}
['/users/:user']
pub fn (mut app App) user_endpoint(user string) vweb.Result {
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
// blog.v
pub fn (mut app App) init_server() {
app.db = sqlite.connect(':memory:') or { panic(err) }
fn main() {
mut app := App{
db: sqlite.connect(':memory:') or { panic(err) }
}
sql app.db {
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`:
```v oksyntax
// article.v
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
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
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
// blog.v
pub fn (mut app App) init_server() {
app.db = sqlite.connect('blog.db') or { panic(err) }
sql app.db {
create table Article
}
}
db: sqlite.connect('blog.db') or { panic(err) }
```
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
create the table `Article` if none exists already. Now every time the
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
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

View File

@ -8,13 +8,17 @@ import json
struct App {
vweb.Context
pub mut:
db sqlite.DB [server_var]
db sqlite.DB
user_id string
}
fn main() {
mut app := App{}
app.init_server()
mut app := App{
db: sqlite.connect('blog.db') or { panic(err) }
}
sql app.db {
create table Article
}
vweb.run(app, 8081)
}
@ -35,13 +39,6 @@ pub fn (app &App) index() vweb.Result {
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() {
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() {
//

View File

@ -7,7 +7,7 @@ import sqlite
struct App {
vweb.Context
pub mut:
db sqlite.DB [server_var]
db sqlite.DB
user_id string
}
@ -25,7 +25,9 @@ fn test_a_vweb_application_compiles() {
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.create_table('article', [
'id integer primary key',
@ -33,6 +35,7 @@ pub fn (mut app App) init_server() {
"text text default ''",
])
}
*/
pub fn (mut app App) before_request() {
app.user_id = app.get_cookie('id') or { '0' }