@@ -7,7 +7,14 @@ The [gitly](https://gitly.org/) site is based on vweb.
7
7
** _ Some features may not be complete, and have some bugs._ **
8
8
9
9
## Quick Start
10
- Just run ** ` v new <name> web ` ** in your terminal
10
+ Just run ** ` v new <name> web ` ** in your terminal.
11
+
12
+ Run your vweb app with a live reload via ` v -d vweb_livereload watch run . `
13
+
14
+ Now modifying any file in your web app (whether it's a .v file with the backend logic
15
+ or a compiled .html template file) will result in an instant refresh of your app
16
+ in the browser. No need to quit the app, rebuild it, and refresh the page in the browser!
17
+
11
18
12
19
## Features
13
20
@@ -253,7 +260,7 @@ pub fn (mut app App) controller_get_user_by_id() vweb.Result {
253
260
```
254
261
#### - Host
255
262
To restrict an endpoint to a specific host, you can use the ` host ` attribute
256
- followed by a colon ` : ` and the host name. You can test the Host feature locally
263
+ followed by a colon ` : ` and the host name. You can test the Host feature locally
257
264
by adding a host to the "hosts" file of your device.
258
265
259
266
** Example:**
@@ -293,10 +300,10 @@ pub fn (mut app App) before_request() {
293
300
}
294
301
```
295
302
296
- Middleware functions can be passed directly when creating an App instance and is
297
- executed when the url starts with the defined key.
303
+ Middleware functions can be passed directly when creating an App instance and is
304
+ executed when the url starts with the defined key.
298
305
299
- In the following example, if a user navigates to ` /path/to/test ` the middleware
306
+ In the following example, if a user navigates to ` /path/to/test ` the middleware
300
307
is executed in the following order: ` middleware_func ` , ` other_func ` , ` global_middleware ` .
301
308
The middleware is executed in the same order as they are defined and if any function in
302
309
the chain returns ` false ` the propogation is stopped.
@@ -342,7 +349,7 @@ fn global_middleware(mut ctx vweb.Context) bool {
342
349
}
343
350
```
344
351
345
- Middleware functions will be of type ` vweb.Middleware ` and are not methods of App,
352
+ Middleware functions will be of type ` vweb.Middleware ` and are not methods of App,
346
353
so they could also be imported from other modules.
347
354
``` v ignore
348
355
pub type Middleware = fn (mut Context) bool
@@ -374,7 +381,7 @@ The middleware is executed in the following order:
374
381
3 . The middleware in the ` [middleware] ` attribute
375
382
376
383
If any function of step 2 or 3 returns ` false ` the middleware functions that would
377
- come after it are not executed and the app handler will also not be executed. You
384
+ come after it are not executed and the app handler will also not be executed. You
378
385
can think of it as a chain.
379
386
380
387
### Context values
@@ -435,7 +442,7 @@ We get this key in `index` and display it to the user if the `'user'` key exists
435
442
436
443
#### Changing Context values
437
444
438
- By default context values are immutable when retrieved with ` get_value ` . If you want to
445
+ By default context values are immutable when retrieved with ` get_value ` . If you want to
439
446
change the value later you have to set it again with ` set_value ` .
440
447
441
448
** Example:**
@@ -494,7 +501,7 @@ pub fn (mut app App) with_auth() bool {
494
501
```
495
502
496
503
### Fallback route
497
- You can implement a fallback ` not_found ` route that is called when a request is made and no
504
+ You can implement a fallback ` not_found ` route that is called when a request is made and no
498
505
matching route is found.
499
506
500
507
** Example:**
@@ -507,7 +514,7 @@ pub fn (mut app App) not_found() vweb.Result {
507
514
```
508
515
509
516
### Databases
510
- The ` db ` field in a vweb app is reserved for database connections. The connection is
517
+ The ` db ` field in a vweb app is reserved for database connections. The connection is
511
518
copied to each new request.
512
519
513
520
** Example:**
@@ -536,7 +543,7 @@ fn main() {
536
543
537
544
### Multithreading
538
545
By default, a vweb app is multithreaded, that means that multiple requests can
539
- be handled in parallel by using multiple CPU's: a worker pool. You can
546
+ be handled in parallel by using multiple CPU's: a worker pool. You can
540
547
change the number of workers (maximum allowed threads) by altering the ` nr_workers `
541
548
option. The default behaviour is to use the maximum number of jobs (cores in most cases).
542
549
@@ -560,7 +567,7 @@ To resolve this issue, you can use the vweb's built-in database pool. The databa
560
567
will keep a number of connections open when the app is started and each worker is
561
568
assigned its own connection.
562
569
563
- Let's look how we can improve our previous example with database pooling and using a
570
+ Let's look how we can improve our previous example with database pooling and using a
564
571
postgresql server instead.
565
572
566
573
** Example:**
@@ -593,14 +600,14 @@ fn main() {
593
600
}
594
601
```
595
602
596
- If you don't use the default number of workers (` nr_workers ` ) you have to change
603
+ If you don't use the default number of workers (` nr_workers ` ) you have to change
597
604
it to the same number in ` vweb.run_at ` as in ` vweb.database_pool `
598
605
599
606
### Extending the App struct with ` [vweb_global] `
600
607
You can change your ` App ` struct however you like, but there are some things you
601
608
have to keep in mind. Under the hood at each request a new instance of ` App ` is
602
- constructed, and all fields are re-initialized with their default type values,
603
- except for the ` db ` field.
609
+ constructed, and all fields are re-initialized with their default type values,
610
+ except for the ` db ` field.
604
611
605
612
This behaviour ensures that each request is treated equally and in the same context, but
606
613
problems arise when we want to provide more context than just the default ` vweb.Context ` .
@@ -628,8 +635,8 @@ fn (mut app App) index() vweb.Result {
628
635
}
629
636
```
630
637
631
- When you visit ` localhost:8080/ ` you would expect to see the text
632
- ` "My secret is: my secret" ` , but instead there is only the text
638
+ When you visit ` localhost:8080/ ` you would expect to see the text
639
+ ` "My secret is: my secret" ` , but instead there is only the text
633
640
` "My secret is: " ` . This is because of the way vweb works. We can override the default
634
641
behaviour by adding the attribute ` [vweb_global] ` to the ` secret ` field.
635
642
@@ -647,8 +654,8 @@ Now if you visit `localhost:8080/` you see the text `"My secret is: my secret"`.
647
654
> next request. You can use shared fields for this.
648
655
649
656
### Shared Objects across requests
650
- We saw in the previous section that we can persist data across multiple requests,
651
- but what if we want to be able to mutate the data? Since vweb works with threads,
657
+ We saw in the previous section that we can persist data across multiple requests,
658
+ but what if we want to be able to mutate the data? Since vweb works with threads,
652
659
we have to use ` shared ` fields.
653
660
654
661
Let's see how we can add a visitor counter to our ` App ` .
@@ -700,7 +707,7 @@ requests the next requests will have to wait for the lock to be released.
700
707
It is best practice to limit the use of shared objects as much as possible.
701
708
702
709
### Controllers
703
- Controllers can be used to split up app logic so you are able to have one struct
710
+ Controllers can be used to split up app logic so you are able to have one struct
704
711
per ` "/" ` . E.g. a struct ` Admin ` for urls starting with ` "/admin" ` and a struct ` Foo `
705
712
for urls starting with ` "/foo" `
706
713
@@ -734,9 +741,9 @@ fn main() {
734
741
}
735
742
```
736
743
737
- You can do everything with a controller struct as with a regular ` App ` struct.
744
+ You can do everything with a controller struct as with a regular ` App ` struct.
738
745
The only difference being is that only the main app that is being passed to ` vweb.run `
739
- is able to have controllers. If you add ` vweb.Controller ` on a controller struct it
746
+ is able to have controllers. If you add ` vweb.Controller ` on a controller struct it
740
747
will simply be ignored.
741
748
742
749
#### Routing
@@ -749,10 +756,10 @@ pub fn (mut app Admin) path vweb.Result {
749
756
}
750
757
```
751
758
When we created the controller with ` vweb.controller('/admin', &Admin{}) ` we told
752
- vweb that the namespace of that controller is ` "/admin" ` so in this example we would
759
+ vweb that the namespace of that controller is ` "/admin" ` so in this example we would
753
760
see the text ` "Admin" ` if we navigate to the url ` "/admin/path" ` .
754
761
755
- Vweb doesn't support fallback routes or duplicate routes, so if we add the following
762
+ Vweb doesn't support fallback routes or duplicate routes, so if we add the following
756
763
route to the example the code will produce an error.
757
764
758
765
``` v ignore
@@ -1198,5 +1205,5 @@ pub fn (mut app App) error() vweb.Result {
1198
1205
```
1199
1206
# Cross-Site Request Forgery (CSRF) protection
1200
1207
1201
- Vweb has built-in csrf protection. Go to the [ csrf module] ( csrf/ ) to learn how
1208
+ Vweb has built-in csrf protection. Go to the [ csrf module] ( csrf/ ) to learn how
1202
1209
you can protect your app against CSRF.
0 commit comments