-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:9: error: undefined ident: `ctx` | ||
48 | fn serve_file(name string) veb.Result { | ||
49 | content := os.read_file(name) or { panic('Error reading ${name}') } | ||
50 | return ctx.html(content) | ||
| ~~~ | ||
51 | } | ||
vlib/v/checker/tests/veb_ctx_on_fn_err.vv:50:2: error: `ctx.html(content)` used as value | ||
48 | fn serve_file(name string) veb.Result { | ||
49 | content := os.read_file(name) or { panic('Error reading ${name}') } | ||
50 | return ctx.html(content) | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
51 | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module main | ||
|
||
import veb | ||
import os | ||
|
||
pub struct User { | ||
pub mut: | ||
name string | ||
id int | ||
} | ||
|
||
// Our context struct must embed `veb.Context`! | ||
pub struct Context { | ||
veb.Context | ||
pub mut: | ||
// In the context struct we store data that could be different | ||
// for each request. Like a User struct or a session id | ||
user User | ||
session_id string | ||
} | ||
|
||
pub struct App { | ||
pub: | ||
// In the app struct we store data that should be accessible by all endpoints. | ||
// For example, a database or configuration values. | ||
secret_key string | ||
} | ||
|
||
// This is how endpoints are defined in veb. This is the index route | ||
|
||
fn main() { | ||
mut app := &App{ | ||
secret_key: 'secret' | ||
} | ||
// Pass the App and context type and start the web server on port 8080 | ||
veb.run[App, Context](mut app, 8080) | ||
} | ||
|
||
@['/foo'] | ||
pub fn (app &App) world(mut ctx Context) veb.Result { | ||
return ctx.text('World') | ||
} | ||
|
||
pub fn (app &App) index(mut ctx Context) veb.Result { | ||
return serve_file('html/index.html') | ||
} | ||
|
||
fn serve_file(name string) veb.Result { | ||
content := os.read_file(name) or { panic('Error reading ${name}') } | ||
return ctx.html(content) | ||
} |