mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
examples: migrate vweb examples to veb
This commit is contained in:
parent
f53b5d737f
commit
c004d0c899
16 changed files with 64 additions and 117 deletions
44
examples/veb/file_transform/file_transform.v
Normal file
44
examples/veb/file_transform/file_transform.v
Normal file
|
@ -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()
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
# Host a static website
|
# 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,
|
to host a static website from the `dist/` folder. Just run the server,
|
||||||
it will be available at http://localhost:8080/ :
|
it will be available at http://localhost:8080/ :
|
||||||
|
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
@ -1,40 +1,44 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import vweb
|
import veb
|
||||||
// import vweb.assets
|
// import vweb.assets
|
||||||
import time
|
import time
|
||||||
|
|
||||||
const port = 8081
|
const port = 8081
|
||||||
|
|
||||||
struct App {
|
pub struct Context {
|
||||||
vweb.Context
|
veb.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct App {
|
||||||
|
veb.StaticHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
mut app := &App{}
|
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.
|
// Automatically make available known static mime types found in given directory.
|
||||||
os.chdir(os.dir(os.executable()))!
|
os.chdir(os.dir(os.executable()))!
|
||||||
app.handle_static('assets', true)
|
app.handle_static('assets', true)!
|
||||||
vweb.run(app, port)
|
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.
|
// We can dynamically specify which assets are to be used in template.
|
||||||
// mut am := assets.new_manager()
|
// mut am := assets.new_manager()
|
||||||
// am.add_css('assets/index.css')
|
// am.add_css('assets/index.css')
|
||||||
// css := am.include_css(false)
|
// css := am.include_css(false)
|
||||||
title := 'VWeb Assets Example'
|
title := 'Veb Assets Example'
|
||||||
subtitle := 'VWeb can serve static assets too!'
|
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.'
|
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 {
|
fn (mut app App) text() veb.Result {
|
||||||
return app.Context.text('Hello, world from vweb!')
|
return ctx.text('Hello, world from veb!')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut app App) time() vweb.Result {
|
fn (mut app App) time() veb.Result {
|
||||||
return app.Context.text(time.now().format())
|
return ctx.text(time.now().format())
|
||||||
}
|
}
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -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()
|
|
||||||
}
|
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
//@[deprecated: '`vweb` is deprecated `veb`. Please use `veb` instead.']
|
||||||
module vweb
|
module vweb
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue