diff options
Diffstat (limited to 'v_windows/v/old/examples/vweb')
15 files changed, 294 insertions, 0 deletions
diff --git a/v_windows/v/old/examples/vweb/file_upload/index.html b/v_windows/v/old/examples/vweb/file_upload/index.html new file mode 100644 index 0000000..a2bffad --- /dev/null +++ b/v_windows/v/old/examples/vweb/file_upload/index.html @@ -0,0 +1,6 @@ +<h2>File Upload</h2> + +<form method="POST" enctype="multipart/form-data" action="/upload"> + <input type="file" name="upfile" multiple> + <input type="submit" value="Press"> +</form> diff --git a/v_windows/v/old/examples/vweb/file_upload/upload.html b/v_windows/v/old/examples/vweb/file_upload/upload.html new file mode 100644 index 0000000..e80359d --- /dev/null +++ b/v_windows/v/old/examples/vweb/file_upload/upload.html @@ -0,0 +1,14 @@ +<meta charset="utf-8"> + +File amount: @fdata.len + +@for i, data in fdata + +<h2>Filename: @data.filename</h2> +<h2>Type: @data.content_type</h2> + +<p>@{files[i]}</p> + +@end + +<a href="/">Back</a> diff --git a/v_windows/v/old/examples/vweb/file_upload/vweb_example.v b/v_windows/v/old/examples/vweb/file_upload/vweb_example.v new file mode 100644 index 0000000..bde55c3 --- /dev/null +++ b/v_windows/v/old/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', '<br>', '\n\r', '<br>', '\t', ' ', ' ', ' ']) + } + + return $vweb.html() +} diff --git a/v_windows/v/old/examples/vweb/index.html b/v_windows/v/old/examples/vweb/index.html new file mode 100644 index 0000000..d69ff78 --- /dev/null +++ b/v_windows/v/old/examples/vweb/index.html @@ -0,0 +1,15 @@ +Test <b>app</b> +<br> +<h1>@hello</h1> +<hr> +@if show + show = true +@end + +@for number in numbers + @number <br> +@end + + +<hr> +End. diff --git a/v_windows/v/old/examples/vweb/server_sent_events/assets/site.css b/v_windows/v/old/examples/vweb/server_sent_events/assets/site.css new file mode 100644 index 0000000..4ad9eb8 --- /dev/null +++ b/v_windows/v/old/examples/vweb/server_sent_events/assets/site.css @@ -0,0 +1,19 @@ +body { + font-family: Arial, Helvetica, sans-serif; + color: #eee; + background-color: #333; + background-image: url("v-logo.svg"); + background-repeat: no-repeat; + background-size: 10em; + margin: 0; + padding-left: 11em; +} + +h1 { + color: #6699CC; +} + +img.logo { + float: left; + width: 10em; +} diff --git a/v_windows/v/old/examples/vweb/server_sent_events/assets/v-logo.svg b/v_windows/v/old/examples/vweb/server_sent_events/assets/v-logo.svg new file mode 100644 index 0000000..9a4ec60 --- /dev/null +++ b/v_windows/v/old/examples/vweb/server_sent_events/assets/v-logo.svg @@ -0,0 +1 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- Generator: Gravit.io --><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="isolation:isolate" viewBox="0 0 500 500" width="500px" height="500px"><defs><clipPath id="_clipPath_8TWIgR1z3pxinjWBiigzcEIrVJKv9Gq4"><rect width="500" height="500"/></clipPath></defs><g clip-path="url(#_clipPath_8TWIgR1z3pxinjWBiigzcEIrVJKv9Gq4)"><path d=" M 318.422 453.543 L 463.705 49.541 C 466.168 42.689 462.285 37.693 455.037 38.392 L 340.786 49.398 C 333.539 50.097 325.71 56.246 323.316 63.121 L 188.843 449.216 C 186.447 456.091 190.414 461.673 197.695 461.673 L 308.901 461.673 C 312.541 461.673 316.497 458.893 317.729 455.466 L 318.422 453.543 Z " fill="rgb(83,107,138)"/><defs><filter id="Hmac7mZraFWHw0G84Yxj4QuzeTFp0E7Y" x="-200%" y="-200%" width="400%" height="400%" filterUnits="objectBoundingBox" color-interpolation-filters="sRGB"><feGaussianBlur xmlns="http://www.w3.org/2000/svg" in="SourceGraphic" stdDeviation="6.440413594258542"/><feOffset xmlns="http://www.w3.org/2000/svg" dx="0" dy="0" result="pf_100_offsetBlur"/><feFlood xmlns="http://www.w3.org/2000/svg" flood-color="#000000" flood-opacity="0.65"/><feComposite xmlns="http://www.w3.org/2000/svg" in2="pf_100_offsetBlur" operator="in" result="pf_100_dropShadow"/><feBlend xmlns="http://www.w3.org/2000/svg" in="SourceGraphic" in2="pf_100_dropShadow" mode="normal"/></filter></defs><g filter="url(#Hmac7mZraFWHw0G84Yxj4QuzeTFp0E7Y)"><path d=" M 301.848 455.466 L 241.359 280.725 L 250 275.324 L 311.57 453.543 L 301.848 455.466 Z " fill="rgb(235,235,235)"/></g><path d=" M 44.963 38.392 L 159.214 49.398 C 166.461 50.097 174.298 56.243 176.704 63.115 L 314.022 455.448 C 315.224 458.885 313.245 461.673 309.604 461.673 L 197.695 461.673 C 190.414 461.673 182.502 456.111 180.038 449.259 L 36.295 49.541 C 33.832 42.689 37.715 37.693 44.963 38.392 Z " fill="rgb(93,135,191)"/></g></svg> diff --git a/v_windows/v/old/examples/vweb/server_sent_events/favicon.ico b/v_windows/v/old/examples/vweb/server_sent_events/favicon.ico Binary files differnew file mode 100644 index 0000000..fa834c3 --- /dev/null +++ b/v_windows/v/old/examples/vweb/server_sent_events/favicon.ico diff --git a/v_windows/v/old/examples/vweb/server_sent_events/index.html b/v_windows/v/old/examples/vweb/server_sent_events/index.html new file mode 100644 index 0000000..7e500c9 --- /dev/null +++ b/v_windows/v/old/examples/vweb/server_sent_events/index.html @@ -0,0 +1,38 @@ +<html> + <header> + <title>@title</title> + <meta charset="utf-8"/> + @css 'assets/site.css' + </header> + <body> + <h1>@title</h1> + <button>Close the connection</button> + <ul></ul> + <script> + "use strict"; + var button = document.querySelector('button'); + var eventList = document.querySelector('ul'); + const evtSource = new EventSource('/sse'); + evtSource.onerror = function() { console.log("EventSource failed."); }; + console.log(evtSource.withCredentials); + console.log(evtSource.readyState); + console.log(evtSource.url); + evtSource.onopen = function() { + console.log("Connection to server opened."); + }; + evtSource.onmessage = function(e) { + var newElement = document.createElement("li"); + newElement.textContent = "message: " + e.data; + eventList.appendChild(newElement); + }; + evtSource.addEventListener("ping", function(e) { + console.log(e) + var newElement = document.createElement("li"); + var obj = JSON.parse(e.data); + newElement.innerHTML = "ping at " + obj.time + ' server data: ' + e.data; + eventList.appendChild(newElement); + }, false); + button.onclick = function() { console.log('Connection closed'); evtSource.close(); }; + </script> + </body> +</html> diff --git a/v_windows/v/old/examples/vweb/server_sent_events/server.v b/v_windows/v/old/examples/vweb/server_sent_events/server.v new file mode 100644 index 0000000..01e63cb --- /dev/null +++ b/v_windows/v/old/examples/vweb/server_sent_events/server.v @@ -0,0 +1,39 @@ +module main + +import os +import rand +import time +import vweb +import vweb.sse + +struct App { + vweb.Context +} + +fn main() { + vweb.run(&App{}, 8081) +} + +pub fn (mut app App) init_server() { + app.serve_static('/favicon.ico', 'favicon.ico') + app.mount_static_folder_at(os.resource_abs_path('.'), '/') +} + +pub fn (mut app App) index() vweb.Result { + title := 'SSE Example' + return $vweb.html() +} + +fn (mut app App) sse() vweb.Result { + mut session := sse.new_connection(app.conn) + // NB: you can setup session.write_timeout and session.headers here + session.start() or { return app.server_error(501) } + session.send_message(data: 'ok') or { return app.server_error(501) } + for { + data := '{"time": "$time.now().str()", "random_id": "$rand.ulid()"}' + session.send_message(event: 'ping', data: data) or { return app.server_error(501) } + println('> sent event: $data') + time.sleep(1 * time.second) + } + return app.server_error(501) +} diff --git a/v_windows/v/old/examples/vweb/vweb_assets/assets/index.css b/v_windows/v/old/examples/vweb/vweb_assets/assets/index.css new file mode 100644 index 0000000..4ad9eb8 --- /dev/null +++ b/v_windows/v/old/examples/vweb/vweb_assets/assets/index.css @@ -0,0 +1,19 @@ +body { + font-family: Arial, Helvetica, sans-serif; + color: #eee; + background-color: #333; + background-image: url("v-logo.svg"); + background-repeat: no-repeat; + background-size: 10em; + margin: 0; + padding-left: 11em; +} + +h1 { + color: #6699CC; +} + +img.logo { + float: left; + width: 10em; +} diff --git a/v_windows/v/old/examples/vweb/vweb_assets/assets/v-logo.svg b/v_windows/v/old/examples/vweb/vweb_assets/assets/v-logo.svg new file mode 100644 index 0000000..9a4ec60 --- /dev/null +++ b/v_windows/v/old/examples/vweb/vweb_assets/assets/v-logo.svg @@ -0,0 +1 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- Generator: Gravit.io --><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="isolation:isolate" viewBox="0 0 500 500" width="500px" height="500px"><defs><clipPath id="_clipPath_8TWIgR1z3pxinjWBiigzcEIrVJKv9Gq4"><rect width="500" height="500"/></clipPath></defs><g clip-path="url(#_clipPath_8TWIgR1z3pxinjWBiigzcEIrVJKv9Gq4)"><path d=" M 318.422 453.543 L 463.705 49.541 C 466.168 42.689 462.285 37.693 455.037 38.392 L 340.786 49.398 C 333.539 50.097 325.71 56.246 323.316 63.121 L 188.843 449.216 C 186.447 456.091 190.414 461.673 197.695 461.673 L 308.901 461.673 C 312.541 461.673 316.497 458.893 317.729 455.466 L 318.422 453.543 Z " fill="rgb(83,107,138)"/><defs><filter id="Hmac7mZraFWHw0G84Yxj4QuzeTFp0E7Y" x="-200%" y="-200%" width="400%" height="400%" filterUnits="objectBoundingBox" color-interpolation-filters="sRGB"><feGaussianBlur xmlns="http://www.w3.org/2000/svg" in="SourceGraphic" stdDeviation="6.440413594258542"/><feOffset xmlns="http://www.w3.org/2000/svg" dx="0" dy="0" result="pf_100_offsetBlur"/><feFlood xmlns="http://www.w3.org/2000/svg" flood-color="#000000" flood-opacity="0.65"/><feComposite xmlns="http://www.w3.org/2000/svg" in2="pf_100_offsetBlur" operator="in" result="pf_100_dropShadow"/><feBlend xmlns="http://www.w3.org/2000/svg" in="SourceGraphic" in2="pf_100_dropShadow" mode="normal"/></filter></defs><g filter="url(#Hmac7mZraFWHw0G84Yxj4QuzeTFp0E7Y)"><path d=" M 301.848 455.466 L 241.359 280.725 L 250 275.324 L 311.57 453.543 L 301.848 455.466 Z " fill="rgb(235,235,235)"/></g><path d=" M 44.963 38.392 L 159.214 49.398 C 166.461 50.097 174.298 56.243 176.704 63.115 L 314.022 455.448 C 315.224 458.885 313.245 461.673 309.604 461.673 L 197.695 461.673 C 190.414 461.673 182.502 456.111 180.038 449.259 L 36.295 49.541 C 33.832 42.689 37.715 37.693 44.963 38.392 Z " fill="rgb(93,135,191)"/></g></svg> diff --git a/v_windows/v/old/examples/vweb/vweb_assets/favicon.ico b/v_windows/v/old/examples/vweb/vweb_assets/favicon.ico Binary files differnew file mode 100644 index 0000000..fa834c3 --- /dev/null +++ b/v_windows/v/old/examples/vweb/vweb_assets/favicon.ico diff --git a/v_windows/v/old/examples/vweb/vweb_assets/index.html b/v_windows/v/old/examples/vweb/vweb_assets/index.html new file mode 100644 index 0000000..6635329 --- /dev/null +++ b/v_windows/v/old/examples/vweb/vweb_assets/index.html @@ -0,0 +1,12 @@ +<html> +<header> + <title>@title</title> + @css 'index.css' +</header> +<body> + <h1>@title</h1> + <h2>@subtitle</h2> + <p>@message</p> +</body> +</html> + diff --git a/v_windows/v/old/examples/vweb/vweb_assets/vweb_assets.v b/v_windows/v/old/examples/vweb/vweb_assets/vweb_assets.v new file mode 100644 index 0000000..058f459 --- /dev/null +++ b/v_windows/v/old/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/old/examples/vweb/vweb_example.v b/v_windows/v/old/examples/vweb/vweb_example.v new file mode 100644 index 0000000..020a2d8 --- /dev/null +++ b/v_windows/v/old/examples/vweb/vweb_example.v @@ -0,0 +1,58 @@ +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) +} + +pub fn (mut app App) init_server() { + app.handle_static('.', false) +} + +['/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') +} |