From c004d0c8997e8d952c2110c26143d94baa346d70 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 17 Sep 2024 09:15:07 +0300 Subject: [PATCH] examples: migrate vweb examples to veb --- examples/veb/file_transform/file_transform.v | 44 +++++++++++++ .../{vweb => veb}/file_transform/index.html | 0 .../file_transform/sample_input.txt | 0 .../{vweb => veb}/file_transform/upload.html | 0 .../{vweb => veb}/static_website/README.md | 2 +- .../static_website/dist/another.html | 0 .../static_website/dist/index.html | 0 .../{vweb => veb}/static_website/server.v | 0 .../veb_assets}/assets/index.css | 0 .../veb_assets}/assets/v-logo.svg | 0 .../veb_assets}/favicon.ico | Bin .../vweb_assets => veb/veb_assets}/index.html | 0 .../veb_assets}/vweb_assets.v | 32 +++++---- examples/vweb/cors/vweb_cors_example.v | 62 ------------------ examples/vweb/file_transform/file_transform.v | 40 ----------- vlib/vweb/vweb.v | 1 + 16 files changed, 64 insertions(+), 117 deletions(-) create mode 100644 examples/veb/file_transform/file_transform.v rename examples/{vweb => veb}/file_transform/index.html (100%) rename examples/{vweb => veb}/file_transform/sample_input.txt (100%) rename examples/{vweb => veb}/file_transform/upload.html (100%) rename examples/{vweb => veb}/static_website/README.md (70%) rename examples/{vweb => veb}/static_website/dist/another.html (100%) rename examples/{vweb => veb}/static_website/dist/index.html (100%) rename examples/{vweb => veb}/static_website/server.v (100%) rename examples/{vweb/vweb_assets => veb/veb_assets}/assets/index.css (100%) rename examples/{vweb/vweb_assets => veb/veb_assets}/assets/v-logo.svg (100%) rename examples/{vweb/vweb_assets => veb/veb_assets}/favicon.ico (100%) rename examples/{vweb/vweb_assets => veb/veb_assets}/index.html (100%) rename examples/{vweb/vweb_assets => veb/veb_assets}/vweb_assets.v (51%) delete mode 100644 examples/vweb/cors/vweb_cors_example.v delete mode 100644 examples/vweb/file_transform/file_transform.v diff --git a/examples/veb/file_transform/file_transform.v b/examples/veb/file_transform/file_transform.v new file mode 100644 index 0000000000..2abe2b4ec6 --- /dev/null +++ b/examples/veb/file_transform/file_transform.v @@ -0,0 +1,44 @@ +module main + +import veb + +const port = 8082 + +pub struct Context { + veb.Context +} + +pub struct App { +} + +fn main() { + mut app := &App{} + veb.run[App, Context](mut app, 8080) +} + +pub fn (mut app App) index() veb.Result { + return $veb.html() +} + +@['/upload'; post] +pub fn (mut app App) upload(mut ctx Context) veb.Result { + fdata := ctx.files['upfile'] + + data_rows := fdata[0].data.split('\n') + + mut output_data := '' + + for elem in data_rows { + delim_row := elem.split('\t') + output_data += '${delim_row[0]}\t${delim_row[1]}\t${delim_row[0].int() + delim_row[1].int()}\n' + } + + output_data = output_data.all_before_last('\n') + + println(output_data) + + ctx.set_header(.content_disposition, 'attachment; filename=results.txt') + ctx.send_response_to_client('application/octet-stream', output_data) + + return $veb.html() +} diff --git a/examples/vweb/file_transform/index.html b/examples/veb/file_transform/index.html similarity index 100% rename from examples/vweb/file_transform/index.html rename to examples/veb/file_transform/index.html diff --git a/examples/vweb/file_transform/sample_input.txt b/examples/veb/file_transform/sample_input.txt similarity index 100% rename from examples/vweb/file_transform/sample_input.txt rename to examples/veb/file_transform/sample_input.txt diff --git a/examples/vweb/file_transform/upload.html b/examples/veb/file_transform/upload.html similarity index 100% rename from examples/vweb/file_transform/upload.html rename to examples/veb/file_transform/upload.html diff --git a/examples/vweb/static_website/README.md b/examples/veb/static_website/README.md similarity index 70% rename from examples/vweb/static_website/README.md rename to examples/veb/static_website/README.md index 389a77dcf9..8cecd7c987 100644 --- a/examples/vweb/static_website/README.md +++ b/examples/veb/static_website/README.md @@ -1,6 +1,6 @@ # Host a static website -Here is an example on how to use the vweb server's static capabilities, +Here is an example on how to use the Veb server's static capabilities, to host a static website from the `dist/` folder. Just run the server, it will be available at http://localhost:8080/ : diff --git a/examples/vweb/static_website/dist/another.html b/examples/veb/static_website/dist/another.html similarity index 100% rename from examples/vweb/static_website/dist/another.html rename to examples/veb/static_website/dist/another.html diff --git a/examples/vweb/static_website/dist/index.html b/examples/veb/static_website/dist/index.html similarity index 100% rename from examples/vweb/static_website/dist/index.html rename to examples/veb/static_website/dist/index.html diff --git a/examples/vweb/static_website/server.v b/examples/veb/static_website/server.v similarity index 100% rename from examples/vweb/static_website/server.v rename to examples/veb/static_website/server.v diff --git a/examples/vweb/vweb_assets/assets/index.css b/examples/veb/veb_assets/assets/index.css similarity index 100% rename from examples/vweb/vweb_assets/assets/index.css rename to examples/veb/veb_assets/assets/index.css diff --git a/examples/vweb/vweb_assets/assets/v-logo.svg b/examples/veb/veb_assets/assets/v-logo.svg similarity index 100% rename from examples/vweb/vweb_assets/assets/v-logo.svg rename to examples/veb/veb_assets/assets/v-logo.svg diff --git a/examples/vweb/vweb_assets/favicon.ico b/examples/veb/veb_assets/favicon.ico similarity index 100% rename from examples/vweb/vweb_assets/favicon.ico rename to examples/veb/veb_assets/favicon.ico diff --git a/examples/vweb/vweb_assets/index.html b/examples/veb/veb_assets/index.html similarity index 100% rename from examples/vweb/vweb_assets/index.html rename to examples/veb/veb_assets/index.html diff --git a/examples/vweb/vweb_assets/vweb_assets.v b/examples/veb/veb_assets/vweb_assets.v similarity index 51% rename from examples/vweb/vweb_assets/vweb_assets.v rename to examples/veb/veb_assets/vweb_assets.v index 43503dfe6d..bd72e0dfc1 100644 --- a/examples/vweb/vweb_assets/vweb_assets.v +++ b/examples/veb/veb_assets/vweb_assets.v @@ -1,40 +1,44 @@ module main import os -import vweb +import veb // import vweb.assets import time const port = 8081 -struct App { - vweb.Context +pub struct Context { + veb.Context +} + +pub struct App { + veb.StaticHandler } fn main() { mut app := &App{} - app.serve_static('/favicon.ico', 'favicon.ico') + app.serve_static('/favicon.ico', 'favicon.ico')! // Automatically make available known static mime types found in given directory. os.chdir(os.dir(os.executable()))! - app.handle_static('assets', true) - vweb.run(app, port) + app.handle_static('assets', true)! + veb.run[App, Context](mut app, 8080) } -pub fn (mut app App) index() vweb.Result { +pub fn (mut app App) index() veb.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!' + title := 'Veb Assets Example' + subtitle := 'Veb 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() + return $veb.html() } -fn (mut app App) text() vweb.Result { - return app.Context.text('Hello, world from vweb!') +fn (mut app App) text() veb.Result { + return ctx.text('Hello, world from veb!') } -fn (mut app App) time() vweb.Result { - return app.Context.text(time.now().format()) +fn (mut app App) time() veb.Result { + return ctx.text(time.now().format()) } diff --git a/examples/vweb/cors/vweb_cors_example.v b/examples/vweb/cors/vweb_cors_example.v deleted file mode 100644 index 44740491ad..0000000000 --- a/examples/vweb/cors/vweb_cors_example.v +++ /dev/null @@ -1,62 +0,0 @@ -import time -import vweb - -// See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS -// and https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests -// > Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows -// > a server to indicate any origins (domain, scheme, or port) other than its own from -// > which a browser should permit loading resources... - -// Usage: do `./v run examples/vweb/cors/` to start the app, -// then check the headers in another shell: -// -// 1) `curl -vvv -X OPTIONS http://localhost:45678/time` -// 2) `curl -vvv -X POST http://localhost:45678/ - -struct App { - vweb.Context -} - -// Browsers like Chrome use OPTIONS requests for their preflight requests. -// This route method ensures, that they will get the headers they need, to allow CORS. -@['/:path...'; options] -pub fn (mut app App) preflight(path string) vweb.Result { - app.add_header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE') - app.add_header('Vary', 'Access-Control-Request-Headers') - return app.text('ok') -} - -// before_request will be called for each route. *Either* use the add_header here, to allow -// *all* endpoints, or add it for each specific route, including for the preflight one above. -// Note that browsers disallow using the Access-Control-Allow-Origin header more than once. -pub fn (mut app App) before_request() { - app.add_header('Access-Control-Allow-Origin', '*') -} - -// time is a simple POST request handler, that returns the current time. It should be available -// to JS scripts, running on arbitrary other origins/domains. -@[post] -pub fn (mut app App) time() vweb.Result { - // *Either* use the .add_header line here, *or* the one in before_request, but *not both*. - // app.add_header('Access-Control-Allow-Origin', '*') - return app.json({ - 'time': time.now().format_ss_milli() - }) -} - -fn main() { - println(" -To test, if CORS works, copy this JS snippet, then go to for example https://stackoverflow.com/ , -press F12, then paste the snippet in the opened JS console. You should see the vweb server's time: - -var xhr = new XMLHttpRequest(); -xhr.onload = function(data) { - console.log('xhr loaded'); - console.log(xhr.response); -}; -xhr.open('POST', 'http://localhost:45678/time'); -xhr.send(); - - ") - vweb.run(&App{}, 45678) -} diff --git a/examples/vweb/file_transform/file_transform.v b/examples/vweb/file_transform/file_transform.v deleted file mode 100644 index f7533cd9a4..0000000000 --- a/examples/vweb/file_transform/file_transform.v +++ /dev/null @@ -1,40 +0,0 @@ -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'] - - data_rows := fdata[0].data.split('\n') - - mut output_data := '' - - for elem in data_rows { - delim_row := elem.split('\t') - output_data += '${delim_row[0]}\t${delim_row[1]}\t${delim_row[0].int() + delim_row[1].int()}\n' - } - - output_data = output_data.all_before_last('\n') - - println(output_data) - - app.add_header('Content-Disposition', 'attachment; filename=results.txt') - app.send_response_to_client('application/octet-stream', output_data) - - return $vweb.html() -} diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 9ae31ee2de..d084108b06 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -1,6 +1,7 @@ // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. +//@[deprecated: '`vweb` is deprecated `veb`. Please use `veb` instead.'] module vweb import os