Skip to content

Commit 5355c67

Browse files
committed
vweb: document live reload
1 parent 4f518c2 commit 5355c67

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

vlib/time/time.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ pub enum FormatDelimiter {
8686
no_delimiter
8787
}
8888

89+
pub fn Time.new(t Time) Time {
90+
return new_time(t)
91+
}
92+
8993
// smonth returns month name abbreviation.
9094
pub fn (t Time) smonth() string {
9195
if t.month <= 0 || t.month > 12 {

vlib/vweb/README.md

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ The [gitly](https://gitly.org/) site is based on vweb.
77
**_Some features may not be complete, and have some bugs._**
88

99
## 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+
1118

1219
## Features
1320

@@ -253,7 +260,7 @@ pub fn (mut app App) controller_get_user_by_id() vweb.Result {
253260
```
254261
#### - Host
255262
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
257264
by adding a host to the "hosts" file of your device.
258265

259266
**Example:**
@@ -293,10 +300,10 @@ pub fn (mut app App) before_request() {
293300
}
294301
```
295302

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.
298305

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
300307
is executed in the following order: `middleware_func`, `other_func`, `global_middleware`.
301308
The middleware is executed in the same order as they are defined and if any function in
302309
the chain returns `false` the propogation is stopped.
@@ -342,7 +349,7 @@ fn global_middleware(mut ctx vweb.Context) bool {
342349
}
343350
```
344351

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,
346353
so they could also be imported from other modules.
347354
```v ignore
348355
pub type Middleware = fn (mut Context) bool
@@ -374,7 +381,7 @@ The middleware is executed in the following order:
374381
3. The middleware in the `[middleware]` attribute
375382

376383
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
378385
can think of it as a chain.
379386

380387
### Context values
@@ -435,7 +442,7 @@ We get this key in `index` and display it to the user if the `'user'` key exists
435442

436443
#### Changing Context values
437444

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
439446
change the value later you have to set it again with `set_value`.
440447

441448
**Example:**
@@ -494,7 +501,7 @@ pub fn (mut app App) with_auth() bool {
494501
```
495502

496503
### 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
498505
matching route is found.
499506

500507
**Example:**
@@ -507,7 +514,7 @@ pub fn (mut app App) not_found() vweb.Result {
507514
```
508515

509516
### 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
511518
copied to each new request.
512519

513520
**Example:**
@@ -536,7 +543,7 @@ fn main() {
536543

537544
### Multithreading
538545
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
540547
change the number of workers (maximum allowed threads) by altering the `nr_workers`
541548
option. The default behaviour is to use the maximum number of jobs (cores in most cases).
542549

@@ -560,7 +567,7 @@ To resolve this issue, you can use the vweb's built-in database pool. The databa
560567
will keep a number of connections open when the app is started and each worker is
561568
assigned its own connection.
562569

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
564571
postgresql server instead.
565572

566573
**Example:**
@@ -593,14 +600,14 @@ fn main() {
593600
}
594601
```
595602

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
597604
it to the same number in `vweb.run_at` as in `vweb.database_pool`
598605

599606
### Extending the App struct with `[vweb_global]`
600607
You can change your `App` struct however you like, but there are some things you
601608
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.
604611

605612
This behaviour ensures that each request is treated equally and in the same context, but
606613
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 {
628635
}
629636
```
630637

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
633640
`"My secret is: "`. This is because of the way vweb works. We can override the default
634641
behaviour by adding the attribute `[vweb_global]` to the `secret` field.
635642

@@ -647,8 +654,8 @@ Now if you visit `localhost:8080/` you see the text `"My secret is: my secret"`.
647654
> next request. You can use shared fields for this.
648655
649656
### 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,
652659
we have to use `shared` fields.
653660

654661
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.
700707
It is best practice to limit the use of shared objects as much as possible.
701708

702709
### 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
704711
per `"/"`. E.g. a struct `Admin` for urls starting with `"/admin"` and a struct `Foo`
705712
for urls starting with `"/foo"`
706713

@@ -734,9 +741,9 @@ fn main() {
734741
}
735742
```
736743

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.
738745
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
740747
will simply be ignored.
741748

742749
#### Routing
@@ -749,10 +756,10 @@ pub fn (mut app Admin) path vweb.Result {
749756
}
750757
```
751758
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
753760
see the text `"Admin"` if we navigate to the url `"/admin/path"`.
754761

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
756763
route to the example the code will produce an error.
757764

758765
```v ignore
@@ -1198,5 +1205,5 @@ pub fn (mut app App) error() vweb.Result {
11981205
```
11991206
# Cross-Site Request Forgery (CSRF) protection
12001207

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
12021209
you can protect your app against CSRF.

0 commit comments

Comments
 (0)