v2 / examples / veb / veb_assets / veb_assets.v
42 lines · 35 sloc · 1.04 KB · 4fa8167abbaced0ea99afe662c6ee3a2e2700f40
Raw
1module main
2
3import os
4import veb
5// import veb.assets
6import time
7
8pub struct Context {
9 veb.Context
10}
11
12pub struct App {
13 veb.StaticHandler
14}
15
16fn main() {
17 mut app := &App{}
18 app.serve_static('/favicon.ico', 'favicon.ico')!
19 // Automatically make available known static mime types found in given directory.
20 os.chdir(os.dir(os.executable()))!
21 app.handle_static('assets', true)!
22 veb.run[App, Context](mut app, 8080)
23}
24
25pub fn (mut app App) index(mut ctx Context) veb.Result {
26 // We can dynamically specify which assets are to be used in template.
27 // mut am := assets.new_manager()
28 // am.add_css('assets/index.css')
29 // css := am.include_css(false)
30 title := 'Veb Assets Example'
31 subtitle := 'Veb can serve static assets too!'
32 message := 'It also has an Assets Manager that allows dynamically specifying which CSS and JS files to be used.'
33 return $veb.html()
34}
35
36fn (mut app App) text(mut ctx Context) veb.Result {
37 return ctx.text('Hello, world from veb!')
38}
39
40fn (mut app App) time(mut ctx Context) veb.Result {
41 return ctx.text(time.now().format())
42}
43