1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/examples/vweb/vweb_example.v

67 lines
1.1 KiB
V
Raw Normal View History

module main
import vweb
2021-03-01 13:50:52 +03:00
import rand
2023-04-02 02:24:33 +03:00
const port = 8082
struct State {
2020-12-31 19:47:20 +03:00
mut:
cnt int
}
2023-04-02 02:24:33 +03:00
struct App {
vweb.Context
mut:
state shared State
}
2023-04-02 02:24:33 +03:00
pub fn (app &App) before_request() {
$if trace_before_request ? {
eprintln('[vweb] before_request: ${app.req.method} ${app.req.url}')
}
}
2021-03-01 13:50:52 +03:00
['/users/:user']
pub fn (mut app App) user_endpoint(user string) vweb.Result {
id := rand.intn(100) or { 0 }
return app.json({
user: id
})
}
2020-06-20 04:12:35 +03:00
pub fn (mut app App) index() vweb.Result {
2023-04-02 02:24:33 +03:00
mut c := 0
lock app.state {
app.state.cnt++
2023-04-02 02:24:33 +03:00
c = app.state.cnt
//
$if trace_address_of_app_state_cnt ? {
dump(ptr_str(app.state.cnt))
}
}
show := true
2023-04-02 02:24:33 +03:00
hello := 'Hello world from vweb, request number: ${c}'
numbers := [1, 2, 3]
2020-06-20 04:12:35 +03:00
return $vweb.html()
2019-08-11 00:02:48 +03:00
}
2020-12-31 19:07:24 +03:00
pub fn (mut app App) show_text() vweb.Result {
return app.text('Hello world from vweb')
}
2020-06-27 14:56:15 +03:00
pub fn (mut app App) cookie() vweb.Result {
2020-12-31 19:47:20 +03:00
app.set_cookie(name: 'cookie', value: 'test')
return app.text('Response Headers\n${app.header}')
2019-09-20 03:01:52 +03:00
}
[post]
pub fn (mut app App) post() vweb.Result {
return app.text('Post body: ${app.req.data}')
}
2023-04-02 02:24:33 +03:00
fn main() {
println('vweb example')
vweb.run(&App{}, port)
}