vweb: migrate all tests and examples to the new syntax

pull/7764/head
Alexander Medvednikov 2020-12-31 17:22:47 +01:00
parent 3ffdcd8910
commit 2533c706ae
4 changed files with 34 additions and 42 deletions

View File

@ -48,7 +48,8 @@ const (
too_short_file_limit = 5000 too_short_file_limit = 5000
// create a .c file for these os's // create a .c file for these os's
vc_build_oses = [ vc_build_oses = [
'nix', // all nix based os 'nix',
/* all nix based os */
'windows', 'windows',
] ]
) )
@ -87,8 +88,8 @@ mut:
// webhook server // webhook server
struct WebhookServer { struct WebhookServer {
pub mut: vweb.Context
vweb vweb.Context mut:
gen_vc &GenVC gen_vc &GenVC
} }
@ -171,10 +172,10 @@ pub fn (mut ws WebhookServer) genhook() {
ws.gen_vc.generate() ws.gen_vc.generate()
// error in generate // error in generate
if ws.gen_vc.gen_error { if ws.gen_vc.gen_error {
ws.vweb.json('{status: "failed"}') ws.json('{status: "failed"}')
return return
} }
ws.vweb.json('{status: "ok"}') ws.json('{status: "ok"}')
} }
pub fn (ws &WebhookServer) reset() { pub fn (ws &WebhookServer) reset() {
@ -209,9 +210,7 @@ fn (mut gen_vc GenVC) generate() {
// check if gen_vc dir exists // check if gen_vc dir exists
if !os.is_dir(gen_vc.options.work_dir) { if !os.is_dir(gen_vc.options.work_dir) {
// try create // try create
os.mkdir(gen_vc.options.work_dir) or { os.mkdir(gen_vc.options.work_dir) or { panic(err) }
panic(err)
}
// still dosen't exist... we have a problem // still dosen't exist... we have a problem
if !os.is_dir(gen_vc.options.work_dir) { if !os.is_dir(gen_vc.options.work_dir) {
gen_vc.logger.error('error creating directory: $gen_vc.options.work_dir') gen_vc.logger.error('error creating directory: $gen_vc.options.work_dir')
@ -247,12 +246,8 @@ fn (mut gen_vc GenVC) generate() {
ts_v := git_log_v.find_between('Date:', '\n').trim_space() ts_v := git_log_v.find_between('Date:', '\n').trim_space()
ts_vc := git_log_vc.find_between('Date:', '\n').trim_space() ts_vc := git_log_vc.find_between('Date:', '\n').trim_space()
// parse time as string to time.Time // parse time as string to time.Time
last_commit_time_v := time.parse(ts_v) or { last_commit_time_v := time.parse(ts_v) or { panic(err) }
panic(err) last_commit_time_vc := time.parse(ts_vc) or { panic(err) }
}
last_commit_time_vc := time.parse(ts_vc) or {
panic(err)
}
// git dates are in users local timezone and v time.parse does not parse // git dates are in users local timezone and v time.parse does not parse
// timezones at the moment, so for now get unix timestamp from output also // timezones at the moment, so for now get unix timestamp from output also
t_unix_v := git_log_v.find_between('Date Unix:', '\n').trim_space().int() t_unix_v := git_log_v.find_between('Date Unix:', '\n').trim_space().int()

View File

@ -9,8 +9,7 @@ const (
) )
struct App { struct App {
pub mut: vweb.Context
vweb vweb.Context
} }
fn main() { fn main() {
@ -19,9 +18,9 @@ fn main() {
pub fn (mut app App) init_once() { pub fn (mut app App) init_once() {
// Arbitary mime type. // Arbitary mime type.
app.vweb.serve_static('/favicon.ico', 'favicon.ico', 'img/x-icon') app.serve_static('/favicon.ico', 'favicon.ico', 'img/x-icon')
// Automatically make available known static mime types found in given directory. // Automatically make available known static mime types found in given directory.
app.vweb.handle_static('assets') app.handle_static('assets')
// This would make available all known static mime types from current // This would make available all known static mime types from current
// directory and below. // directory and below.
// app.vweb.handle_static('.') // app.vweb.handle_static('.')
@ -41,10 +40,10 @@ pub fn (mut app App) index() {
$vweb.html() $vweb.html()
} }
fn (mut app App) text() { fn (mut app App) text() vweb.Result {
app.vweb.text('Hello, world from vweb!') return app.text('Hello, world from vweb!')
} }
fn (mut app App) time() { fn (mut app App) time() vweb.Result {
app.vweb.text(time.now().format()) return app.text(time.now().format())
} }

View File

@ -1,8 +1,7 @@
import vweb import vweb
struct App { struct App {
pub mut: vweb.Context
vweb vweb.Context
} }
pub fn (mut app App) no_attributes(a string) vweb.Result { pub fn (mut app App) no_attributes(a string) vweb.Result {
@ -13,21 +12,21 @@ pub fn (mut app App) no_attributes(a string) vweb.Result {
['/foo/:bar'] ['/foo/:bar']
pub fn (mut app App) foo(a string) vweb.Result { pub fn (mut app App) foo(a string) vweb.Result {
eprintln('foo') eprintln('foo')
app.vweb.html('works') app.html('works')
return vweb.Result{} return vweb.Result{}
} }
// segfault because path taks 0 vars and fcn takes 1 arg // segfault because path taks 0 vars and fcn takes 1 arg
['/bar'] ['/bar']
pub fn (mut app App) bar(a string) vweb.Result { pub fn (mut app App) bar(a string) vweb.Result {
app.vweb.html('works') app.html('works')
return vweb.Result{} return vweb.Result{}
} }
// no segfault, but it shouldnt compile // no segfault, but it shouldnt compile
['/cow/:low'] ['/cow/:low']
pub fn (mut app App) cow() vweb.Result { pub fn (mut app App) cow() vweb.Result {
app.vweb.html('works') app.html('works')
return vweb.Result{} return vweb.Result{}
} }
@ -40,7 +39,7 @@ pub fn (app App) init() {
} }
pub fn (mut app App) index() { pub fn (mut app App) index() {
app.vweb.html('hello') app.html('hello')
} }
fn main() { fn main() {

View File

@ -9,10 +9,9 @@ const (
) )
struct App { struct App {
vweb.Context
port int port int
timeout int timeout int
pub mut:
vweb vweb.Context
} }
fn exit_after_timeout(timeout_in_ms int) { fn exit_after_timeout(timeout_in_ms int) {
@ -46,16 +45,16 @@ pub fn (mut app App) init_once() {
} }
pub fn (mut app App) index() { pub fn (mut app App) index() {
app.vweb.text('Welcome to VWeb') app.text('Welcome to VWeb')
} }
pub fn (mut app App) simple() vweb.Result { pub fn (mut app App) simple() vweb.Result {
app.vweb.text('A simple result') app.text('A simple result')
return vweb.Result{} return vweb.Result{}
} }
pub fn (mut app App) html_page() vweb.Result { pub fn (mut app App) html_page() vweb.Result {
app.vweb.html('<h1>ok</h1>') app.html('<h1>ok</h1>')
return vweb.Result{} return vweb.Result{}
} }
@ -63,35 +62,35 @@ pub fn (mut app App) html_page() vweb.Result {
['/:user/settings'] ['/:user/settings']
pub fn (mut app App) settings(username string) vweb.Result { pub fn (mut app App) settings(username string) vweb.Result {
if username !in known_users { if username !in known_users {
return app.vweb.not_found() return app.not_found()
} }
app.vweb.html('username: $username') app.html('username: $username')
return vweb.Result{} return vweb.Result{}
} }
['/:user/:repo/settings'] ['/:user/:repo/settings']
pub fn (mut app App) user_repo_settings(username string, repository string) vweb.Result { pub fn (mut app App) user_repo_settings(username string, repository string) vweb.Result {
if username !in known_users { if username !in known_users {
return app.vweb.not_found() return app.not_found()
} }
app.vweb.html('username: $username | repository: $repository') app.html('username: $username | repository: $repository')
return vweb.Result{} return vweb.Result{}
} }
[post] [post]
['/json_echo'] ['/json_echo']
pub fn (mut app App) json() vweb.Result { pub fn (mut app App) json() vweb.Result {
app.vweb.set_content_type(app.vweb.req.headers['Content-Type']) app.set_content_type(app.req.headers['Content-Type'])
return app.vweb.ok(app.vweb.req.data) return app.ok(app.req.data)
} }
pub fn (mut app App) shutdown() vweb.Result { pub fn (mut app App) shutdown() vweb.Result {
session_key := app.vweb.get_cookie('skey') or { return app.vweb.not_found() } session_key := app.get_cookie('skey') or { return app.not_found() }
if session_key != 'superman' { if session_key != 'superman' {
return app.vweb.not_found() return app.not_found()
} }
go app.gracefull_exit() go app.gracefull_exit()
return app.vweb.ok('good bye') return app.ok('good bye')
} }
fn (mut app App) gracefull_exit() { fn (mut app App) gracefull_exit() {