From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- v_windows/v/examples/vweb/file_upload/index.html | 6 +++ v_windows/v/examples/vweb/file_upload/upload.html | 14 ++++++ .../v/examples/vweb/file_upload/vweb_example.v | 32 ++++++++++++ v_windows/v/examples/vweb/index.html | 15 ++++++ .../vweb/server_sent_events/assets/site.css | 19 ++++++++ .../vweb/server_sent_events/assets/v-logo.svg | 1 + .../v/examples/vweb/server_sent_events/favicon.ico | Bin 0 -> 15406 bytes .../v/examples/vweb/server_sent_events/index.html | 38 +++++++++++++++ .../v/examples/vweb/server_sent_events/server.v | 37 ++++++++++++++ .../v/examples/vweb/vweb_assets/assets/index.css | 19 ++++++++ .../v/examples/vweb/vweb_assets/assets/v-logo.svg | 1 + v_windows/v/examples/vweb/vweb_assets/favicon.ico | Bin 0 -> 15406 bytes v_windows/v/examples/vweb/vweb_assets/index.html | 12 +++++ .../v/examples/vweb/vweb_assets/vweb_assets.v | 40 +++++++++++++++ v_windows/v/examples/vweb/vweb_example.v | 54 +++++++++++++++++++++ 15 files changed, 288 insertions(+) create mode 100644 v_windows/v/examples/vweb/file_upload/index.html create mode 100644 v_windows/v/examples/vweb/file_upload/upload.html create mode 100644 v_windows/v/examples/vweb/file_upload/vweb_example.v create mode 100644 v_windows/v/examples/vweb/index.html create mode 100644 v_windows/v/examples/vweb/server_sent_events/assets/site.css create mode 100644 v_windows/v/examples/vweb/server_sent_events/assets/v-logo.svg create mode 100644 v_windows/v/examples/vweb/server_sent_events/favicon.ico create mode 100644 v_windows/v/examples/vweb/server_sent_events/index.html create mode 100644 v_windows/v/examples/vweb/server_sent_events/server.v create mode 100644 v_windows/v/examples/vweb/vweb_assets/assets/index.css create mode 100644 v_windows/v/examples/vweb/vweb_assets/assets/v-logo.svg create mode 100644 v_windows/v/examples/vweb/vweb_assets/favicon.ico create mode 100644 v_windows/v/examples/vweb/vweb_assets/index.html create mode 100644 v_windows/v/examples/vweb/vweb_assets/vweb_assets.v create mode 100644 v_windows/v/examples/vweb/vweb_example.v (limited to 'v_windows/v/examples/vweb') diff --git a/v_windows/v/examples/vweb/file_upload/index.html b/v_windows/v/examples/vweb/file_upload/index.html new file mode 100644 index 0000000..a2bffad --- /dev/null +++ b/v_windows/v/examples/vweb/file_upload/index.html @@ -0,0 +1,6 @@ +
@{files[i]}
+ +@end + +Back diff --git a/v_windows/v/examples/vweb/file_upload/vweb_example.v b/v_windows/v/examples/vweb/file_upload/vweb_example.v new file mode 100644 index 0000000..bde55c3 --- /dev/null +++ b/v_windows/v/examples/vweb/file_upload/vweb_example.v @@ -0,0 +1,32 @@ +module main + +import vweb + +const ( + port = 8082 +) + +struct App { + vweb.Context +} + +fn main() { + vweb.run(&App{}, port) +} + +pub fn (mut app App) index() vweb.Result { + return $vweb.html() +} + +['/upload'; post] +pub fn (mut app App) upload() vweb.Result { + fdata := app.files['upfile'] + + mut files := []vweb.RawHtml{} + + for d in fdata { + files << d.data.replace_each(['\n', '@message
+ + + diff --git a/v_windows/v/examples/vweb/vweb_assets/vweb_assets.v b/v_windows/v/examples/vweb/vweb_assets/vweb_assets.v new file mode 100644 index 0000000..058f459 --- /dev/null +++ b/v_windows/v/examples/vweb/vweb_assets/vweb_assets.v @@ -0,0 +1,40 @@ +module main + +import vweb +// import vweb.assets +import time + +const ( + port = 8081 +) + +struct App { + vweb.Context +} + +fn main() { + mut app := &App{} + app.serve_static('/favicon.ico', 'favicon.ico') + // Automatically make available known static mime types found in given directory. + app.handle_static('assets', true) + vweb.run(app, port) +} + +pub fn (mut app App) index() vweb.Result { + // We can dynamically specify which assets are to be used in template. + // mut am := assets.new_manager() + // am.add_css('assets/index.css') + // css := am.include_css(false) + title := 'VWeb Assets Example' + subtitle := 'VWeb can serve static assets too!' + message := 'It also has an Assets Manager that allows dynamically specifying which CSS and JS files to be used.' + return $vweb.html() +} + +fn (mut app App) text() vweb.Result { + return app.Context.text('Hello, world from vweb!') +} + +fn (mut app App) time() vweb.Result { + return app.Context.text(time.now().format()) +} diff --git a/v_windows/v/examples/vweb/vweb_example.v b/v_windows/v/examples/vweb/vweb_example.v new file mode 100644 index 0000000..f26726c --- /dev/null +++ b/v_windows/v/examples/vweb/vweb_example.v @@ -0,0 +1,54 @@ +module main + +import vweb +import rand + +const ( + port = 8082 +) + +struct App { + vweb.Context +mut: + state shared State +} + +struct State { +mut: + cnt int +} + +fn main() { + println('vweb example') + vweb.run(&App{}, port) +} + +['/users/:user'] +pub fn (mut app App) user_endpoint(user string) vweb.Result { + id := rand.intn(100) + return app.json('{"$user": $id}') +} + +pub fn (mut app App) index() vweb.Result { + lock app.state { + app.state.cnt++ + } + show := true + hello := 'Hello world from vweb' + numbers := [1, 2, 3] + return $vweb.html() +} + +pub fn (mut app App) show_text() vweb.Result { + return app.text('Hello world from vweb') +} + +pub fn (mut app App) cookie() vweb.Result { + app.set_cookie(name: 'cookie', value: 'test') + return app.text('Response Headers\n$app.header') +} + +[post] +pub fn (mut app App) post() vweb.Result { + return app.text('Post body: $app.req.data') +} -- cgit v1.2.3