| 1 | // vtest build: present_sqlite3? && !windows // present_sqlite3?: imports db.sqlite; !windows: fasthttp.Server.run not implemented |
| 2 | import veb |
| 3 | import time |
| 4 | import net.http |
| 5 | import db.sqlite |
| 6 | |
| 7 | const port = 13004 |
| 8 | |
| 9 | pub struct Context { |
| 10 | veb.Context |
| 11 | pub mut: |
| 12 | cid int |
| 13 | } |
| 14 | |
| 15 | pub fn (mut ctx Context) before_request() { |
| 16 | dump(@LOCATION) |
| 17 | ctx.cid = 123 |
| 18 | } |
| 19 | |
| 20 | pub struct App { |
| 21 | pub mut: |
| 22 | db sqlite.DB |
| 23 | started chan bool |
| 24 | } |
| 25 | |
| 26 | pub fn (mut app App) before_accept_loop() { |
| 27 | dump(@LOCATION) |
| 28 | app.started <- true |
| 29 | } |
| 30 | |
| 31 | struct Article { |
| 32 | id int |
| 33 | title string |
| 34 | text string |
| 35 | } |
| 36 | |
| 37 | fn test_veb_application_compiles() { |
| 38 | spawn fn () { |
| 39 | time.sleep(15 * time.second) |
| 40 | exit(0) |
| 41 | }() |
| 42 | mut app := &App{} |
| 43 | spawn veb.run_at[App, Context](mut app, port: port, family: .ip, timeout_in_seconds: 2) |
| 44 | // app startup time |
| 45 | _ := <-app.started |
| 46 | res := http.fetch(url: 'http://127.0.0.1:${port}/get_cid')! |
| 47 | assert res.status_code == 200 |
| 48 | assert res.body == '123' |
| 49 | okres := http.fetch(url: 'http://127.0.0.1:${port}/ok')! |
| 50 | assert okres.status_code == 200 |
| 51 | assert okres.body == '{"success":true,"result":123}' |
| 52 | hello := http.fetch(url: 'http://127.0.0.1:${port}/hello')! |
| 53 | assert hello.status_code == 200 |
| 54 | assert hello.body == 'hello world' |
| 55 | } |
| 56 | |
| 57 | @['/new_article'; post] |
| 58 | pub fn (mut app App) new_article(mut ctx Context) veb.Result { |
| 59 | title := ctx.form['title'] |
| 60 | text := ctx.form['text'] |
| 61 | if title == '' || text == '' { |
| 62 | return ctx.text('Empty text/title') |
| 63 | } |
| 64 | article := Article{ |
| 65 | title: title |
| 66 | text: text |
| 67 | } |
| 68 | println('posting article') |
| 69 | println(article) |
| 70 | sql app.db { |
| 71 | insert article into Article |
| 72 | } or {} |
| 73 | |
| 74 | return ctx.redirect('/', typ: .see_other) |
| 75 | } |
| 76 | |
| 77 | pub fn (mut app App) time(mut ctx Context) veb.Result { |
| 78 | return ctx.text(time.now().format()) |
| 79 | } |
| 80 | |
| 81 | pub fn (mut app App) time_json(mut ctx Context) veb.Result { |
| 82 | return ctx.json({ |
| 83 | 'time': time.now().format() |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | fn (app App) hello(mut ctx Context) veb.Result { |
| 88 | return ctx.text('hello world') |
| 89 | } |
| 90 | |
| 91 | fn (mut app App) time_json_pretty(mut ctx Context) veb.Result { |
| 92 | return ctx.json_pretty({ |
| 93 | 'time': time.now().format() |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | struct ApiSuccessResponse[T] { |
| 98 | success bool |
| 99 | result T |
| 100 | } |
| 101 | |
| 102 | fn (mut app App) json_success[T](mut ctx Context, result T) { |
| 103 | response := ApiSuccessResponse[T]{ |
| 104 | success: true |
| 105 | result: result |
| 106 | } |
| 107 | |
| 108 | ctx.json(response) |
| 109 | } |
| 110 | |
| 111 | // should compile, this is a helper method, not exposed as a route |
| 112 | fn (mut app App) some_helper[T](result T) ApiSuccessResponse[T] { |
| 113 | response := ApiSuccessResponse[T]{ |
| 114 | success: true |
| 115 | result: result |
| 116 | } |
| 117 | return response |
| 118 | } |
| 119 | |
| 120 | // should compile, the route method itself is not generic |
| 121 | fn (mut app App) ok(mut ctx Context) veb.Result { |
| 122 | return ctx.json(app.some_helper(ctx.cid)) |
| 123 | } |
| 124 | |
| 125 | // should compile, the route method itself is not generic |
| 126 | fn (mut app App) get_cid(mut ctx Context) veb.Result { |
| 127 | return ctx.text(ctx.cid.str()) |
| 128 | } |
| 129 | |
| 130 | struct ExampleStruct { |
| 131 | example int |
| 132 | } |
| 133 | |
| 134 | fn (mut app App) request_raw_2(mut ctx Context) veb.Result { |
| 135 | stuff := []ExampleStruct{} |
| 136 | app.request_raw(mut ctx, stuff) |
| 137 | return ctx.ok('') |
| 138 | } |
| 139 | |
| 140 | // should compile, this is a helper method, not exposed as a route |
| 141 | fn (mut app App) request_raw(mut ctx Context, foo []ExampleStruct) { |
| 142 | ctx.text('Hello world') |
| 143 | } |
| 144 | |