vweb #

vweb - the V Web Server

A simple yet powerful web server with built-in routing, parameter handling, templating, and other features.

Alpha level software

Some features may not be complete, and there may still be bugs. However, it is still a very useful tool. The gitly site is based on vweb.


  • Very fast performance of C on the web.
  • Small binary hello world website is <100 KB.
  • Easy to deploy just one binary file that also includes all templates. No need to install any dependencies.
  • Templates are precompiled all errors are visible at compilation time, not at runtime.

There is no formal documentation yet - here is a simple example

There's also the V forum, vorum

vorum.v contains all GET and POST actions.

pub fn (app mut App) index() {
    posts := app.find_all_posts()

// TODO ['/post/:id/:title']
// TODO `fn (app App) post(id int)`
pub fn (app App) post() {
    id := app.get_post_id()
    post := app.retrieve_post(id) or {
    comments := app.find_comments(id)
    show_form := true

index.html is an example of the V template language:

@for post in posts
    <div class=post>
        <a class=topic href="@post.url">@post.title</a>
        <img class=comment-img>
        <span class=nr-comments>@post.nr_comments</span>
        <span class=time>@post.time</span>

$vweb.html() compiles an HTML template into V during compilation, and embeds the resulting code into the current action.

That means that the template automatically has access to that action's entire environment.

Deploying vweb apps

Everything, including HTML templates, is in one binary file. That's all you need to deploy.

Getting Started

To start with vweb, you have to import the module vweb. After the import, define a struct to hold vweb.Context (and any other variables your program will need).

The web server can be started by calling vweb.run(&App{}, port).


import vweb

struct App {

fn main() {
    vweb.run(&App{}, 8080)

Defining endpoints

To add endpoints to your web server, you have to extend the App struct. For routing you can either use auto-mapping of function names or specify the path as an attribute. The function expects a response of the type vweb.Result.


// This endpoint can be accessed via http://localhost:port/hello
fn (mut app App) hello() vweb.Result {
    return app.text('Hello')

// This endpoint can be accessed via http://localhost:port/foo
fn (mut app App) world() vweb.Result {
    return app.text('World')

To create an HTTP POST endpoint, you simply add a [post] attribute before the function definition.


fn (mut app App) world() vweb.Result {
    return app.text('World')

To pass a parameter to an endpoint, you simply define it inside an attribute, e. g. ['/hello/:user]. After it is defined in the attribute, you have to add it as a function parameter.


fn (mut app App) hello_user(user string) vweb.Result {
    return app.text('Hello $user')

You have access to the raw request data such as headers or the request body by accessing app (which is vweb.Context). If you want to read the request body, you can do that by calling app.req.data. To read the request headers, you just call app.req.header and access the header you want, e.g. app.req.header.get(.content_type). See struct Header for all available methods (v doc net.http Header).

Constants #

const (
	methods_with_form = [http.Method.post, .put, .patch]
	headers_close     = http.new_custom_header_from_map({
		'Server':          'VWeb'
		http.CommonHeader.connection.str(): 'close'
	}) or { panic('should never fail') }

	http_302          = http.new_response(
		status: .found
		text: '302 Found'
		header: headers_close
	http_400          = http.new_response(
		status: .bad_request
		text: '400 Bad Request'
		header: http.new_header(
			key: .content_type
			value: 'text/plain'
	http_404          = http.new_response(
		status: .not_found
		text: '404 Not Found'
		header: http.new_header(
			key: .content_type
			value: 'text/plain'
	http_500          = http.new_response(
		status: .internal_server_error
		text: '500 Internal Server Error'
		header: http.new_header(
			key: .content_type
			value: 'text/plain'
	mime_types        = {
		'.aac':    'audio/aac'
		'.abw':    'application/x-abiword'
		'.arc':    'application/x-freearc'
		'.avi':    'video/x-msvideo'
		'.azw':    'application/vnd.amazon.ebook'
		'.bin':    'application/octet-stream'
		'.bmp':    'image/bmp'
		'.bz':     'application/x-bzip'
		'.bz2':    'application/x-bzip2'
		'.cda':    'application/x-cdf'
		'.csh':    'application/x-csh'
		'.css':    'text/css'
		'.csv':    'text/csv'
		'.doc':    'application/msword'
		'.docx':   'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
		'.eot':    'application/vnd.ms-fontobject'
		'.epub':   'application/epub+zip'
		'.gz':     'application/gzip'
		'.gif':    'image/gif'
		'.htm':    'text/html'
		'.html':   'text/html'
		'.ico':    'image/vnd.microsoft.icon'
		'.ics':    'text/calendar'
		'.jar':    'application/java-archive'
		'.jpeg':   'image/jpeg'
		'.jpg':    'image/jpeg'
		'.js':     'text/javascript'
		'.json':   'application/json'
		'.jsonld': 'application/ld+json'
		'.mid':    'audio/midi audio/x-midi'
		'.midi':   'audio/midi audio/x-midi'
		'.mjs':    'text/javascript'
		'.mp3':    'audio/mpeg'
		'.mp4':    'video/mp4'
		'.mpeg':   'video/mpeg'
		'.mpkg':   'application/vnd.apple.installer+xml'
		'.odp':    'application/vnd.oasis.opendocument.presentation'
		'.ods':    'application/vnd.oasis.opendocument.spreadsheet'
		'.odt':    'application/vnd.oasis.opendocument.text'
		'.oga':    'audio/ogg'
		'.ogv':    'video/ogg'
		'.ogx':    'application/ogg'
		'.opus':   'audio/opus'
		'.otf':    'font/otf'
		'.png':    'image/png'
		'.pdf':    'application/pdf'
		'.php':    'application/x-httpd-php'
		'.ppt':    'application/vnd.ms-powerpoint'
		'.pptx':   'application/vnd.openxmlformats-officedocument.presentationml.presentation'
		'.rar':    'application/vnd.rar'
		'.rtf':    'application/rtf'
		'.sh':     'application/x-sh'
		'.svg':    'image/svg+xml'
		'.swf':    'application/x-shockwave-flash'
		'.tar':    'application/x-tar'
		'.tif':    'image/tiff'
		'.tiff':   'image/tiff'
		'.ts':     'video/mp2t'
		'.ttf':    'font/ttf'
		'.txt':    'text/plain'
		'.vsd':    'application/vnd.visio'
		'.wav':    'audio/wav'
		'.weba':   'audio/webm'
		'.webm':   'video/webm'
		'.webp':   'image/webp'
		'.woff':   'font/woff'
		'.woff2':  'font/woff2'
		'.xhtml':  'application/xhtml+xml'
		'.xls':    'application/vnd.ms-excel'
		'.xlsx':   'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
		'.xml':    'application/xml'
		'.xul':    'application/vnd.mozilla.xul+xml'
		'.zip':    'application/zip'
		'.3gp':    'video/3gpp'
		'.3g2':    'video/3gpp2'
		'.7z':     'application/x-7z-compressed'
	max_http_post_size = 1024 * 1024
	default_port       = 8080

fn not_found #

fn not_found() Result

Returns an empty result

fn run #

fn run<T>(global_app &T, port int)


type RawHtml #

type RawHtml = string

A type which don't get filtered inside templates

struct Context #

struct Context {
	content_type string = 'text/plain'
	status       string = '200 OK'
	req http.Request
pub mut:
	done bool

	page_gen_start i64

	conn              &net.TcpConn
	static_files      map[string]string
	static_mime_types map[string]string

	query map[string]string

	form map[string]string

	files map[string][]http.FileData

	header http.Header

	form_error string

The Context struct represents the Context which hold the HTTP request and response.
It has fields for the query, form, files.

fn (Context) init_server #

fn (ctx Context) init_server()

Defining this method is optional.
This method called at server start.
You can use it for initializing globals.

fn (Context) before_request #

fn (ctx Context) before_request()

Defining this method is optional.
This method called before every request (aka middleware).
Probably you can use it for check user session cookie or add header.

fn (Context) send_response_to_client #

fn (mut ctx Context) send_response_to_client(mimetype string, res string) bool

vweb intern function

fn (Context) html #

fn (mut ctx Context) html(s string) Result

Response HTTP_OK with s as payload with content-type text/html

fn (Context) text #

fn (mut ctx Context) text(s string) Result

Response HTTP_OK with s as payload with content-type text/plain

fn (Context) json #

fn (mut ctx Context) json<T>(j T) Result

Response HTTP_OK with json_s as payload with content-type application/json

fn (Context) file #

fn (mut ctx Context) file(f_path string) Result

Response HTTP_OK with file as payload

fn (Context) ok #

fn (mut ctx Context) ok(s string) Result

Response HTTP_OK with s as payload

fn (Context) server_error #

fn (mut ctx Context) server_error(ecode int) Result

Response a server error

fn (Context) redirect #

fn (mut ctx Context) redirect(url string) Result

Redirect to an url

fn (Context) not_found #

fn (mut ctx Context) not_found() Result

Send an not_found response

fn (Context) set_content_type #

fn (mut ctx Context) set_content_type(typ string)

Sets the response content type

fn (Context) set_status #

fn (mut ctx Context) set_status(code int, desc string)

Sets the response status

fn (Context) add_header #

fn (mut ctx Context) add_header(key string, val string)

Adds an header to the response with key and val

fn (Context) get_header #

fn (ctx &Context) get_header(key string) string

Returns the header data from the key

fn (Context) handle_static #

fn (mut ctx Context) handle_static(directory_path string, root bool) bool

Handles a directory static If root is set the mount path for the dir will be in '/'

fn (Context) mount_static_folder_at #

fn (mut ctx Context) mount_static_folder_at(directory_path string, mount_path string) bool

mount_static_folder_at - makes all static files in directory_path and inside it, available at http://server/mount_path For example: suppose you have called .mount_static_folder_at('/var/share/myassets', '/assets'), and you have a file /var/share/myassets/main.css .
=> That file will be available at URL: http://server/assets/main.css .

fn (Context) serve_static #

fn (mut ctx Context) serve_static(url string, file_path string)

Serves a file static url is the access path on the site, file_path is the real path to the file, mime_type is the file type

fn (Context) ip #

fn (ctx &Context) ip() string

Returns the ip address from the current user

fn (Context) error #

fn (mut ctx Context) error(s string)

Set s to the form error

struct Result #

struct Result {}

A dummy structure that returns from routes to indicate that you actually sent something to a user