vweb: remove init_server() from all examples, tutorials, and tests
parent
f879b3e221
commit
80976e640c
|
@ -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 ..
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
@ -423,16 +413,10 @@ 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`.
|
||||
|
|
|
@ -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' }
|
||||
}
|
||||
|
|
|
@ -28,9 +28,6 @@ pub fn (mut app App) cow() vweb.Result {
|
|||
}
|
||||
|
||||
/*
|
||||
pub fn (app App) init_server() {
|
||||
//
|
||||
}
|
||||
|
||||
pub fn (app App) before_request() {
|
||||
//
|
||||
|
|
|
@ -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' }
|
||||
|
|
Loading…
Reference in New Issue