v/examples/vweb/vweb_assets/main.v

52 lines
1.1 KiB
V
Raw Normal View History

2020-03-07 14:16:03 +01:00
module main
2020-04-26 13:49:31 +02:00
import vweb
import vweb.assets
import time
2020-03-07 14:16:03 +01:00
const (
port = 8081
)
struct App {
pub mut:
2020-03-07 14:16:03 +01:00
vweb vweb.Context
}
fn main() {
vweb.run<App>(port)
}
2020-05-17 13:51:18 +02:00
pub fn (mut app App) init() {
2020-03-07 14:16:03 +01:00
// Arbitary mime type.
app.vweb.serve_static('/favicon.ico', 'favicon.ico', 'img/x-icon')
// Automatically make available known static mime types found in given directory.
app.vweb.handle_static('assets')
// This would make available all known static mime types from current
// directory and below.
//app.vweb.handle_static('.')
}
2020-05-17 13:51:18 +02:00
pub fn (mut app App) reset() {}
2020-03-07 14:16:03 +01:00
2020-05-17 13:51:18 +02:00
fn (mut app App) index() {
2020-03-07 14:16:03 +01:00
// 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.'
$vweb.html()
}
2020-05-17 13:51:18 +02:00
fn (mut app App) text() {
2020-03-07 14:16:03 +01:00
app.vweb.text('Hello, world from vweb!')
}
2020-05-17 13:51:18 +02:00
fn (mut app App) time() {
2020-03-07 14:16:03 +01:00
app.vweb.text(time.now().format())
}