This is pre-alpha software.
Lots of things are broken and not implemented yet in V and vweb.
There's no documentation yet, have a look at a simple example:
https://github.com/vlang/v/tree/master/examples/vweb/vweb_example.v
There's also the V forum: https://github.com/vlang/vorum
vorum.v
contains all GET and POST actions.
pub fn (app mut App) index() {
posts := app.find_all_posts()
$vweb.html()
}
// 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 {
app.redirect('/')
return
}
comments := app.find_comments(id)
show_form := true
$vweb.html()
}
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>
</div>
@end
$vweb.html()
compiles an HTML template into V during compilation,
and embeds the resulting code in current action.
That means that the template automatically has access to that action's entire environment.
Everything, including HTML templates, is in one binary file. That's all you need to deploy.
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)
.
Example:
import vweb
struct App {
vweb.Context
}
fn main() {
vweb.run<App>(8080)
}
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
.
Example:
// 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
["/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.
Example:
[post]
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.
Example:
['/hello/:user']
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
).
const (
methods_with_form = [http.Method.post, .put, .patch]
header_server = 'Server: VWeb\r\n'
header_connection_close = 'Connection: close\r\n'
headers_close =''$2header_server$header_connection_close\r\n'
http_400 = 'HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\nContent-Length: 15\r\n${headers_close}400 Bad Request'
http_404 = 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n${headers_close}404 Not Found'
http_500 = 'HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n${headers_close}500 Internal Server Error'
mime_types = map{
'.css': 'text/css; charset=utf-8'
'.gif': 'image/gif'
'.htm': 'text/html; charset=utf-8'
'.html': 'text/html; charset=utf-8'
'.jpg': 'image/jpeg'
'.js': 'application/javascript'
'.json': 'application/json'
'.md': 'text/markdown; charset=utf-8'
'.pdf': 'application/pdf'
'.png': 'image/png'
'.svg': 'image/svg+xml'
'.txt': 'text/plain; charset=utf-8'
'.wasm': 'application/wasm'
'.xml': 'text/xml; charset=utf-8'
}
max_http_post_size = 1024 * 1024
default_port = 8080
)
fn not_found() Result
Returns an empty result
fn run<T>(port int)
fn run_app<T>(mut app T, port int)
type RawHtml = string
A type which don't get filtered inside templates
struct Context {
mut:
content_type string = 'text/plain'
status string = '200 OK'
pub:
req http.Request
pub mut:
conn &net.TcpConn
static_files map[string]string
static_mime_types map[string]string
form map[string]string
query map[string]string
files map[string][]FileData
headers string
done bool
page_gen_start i64
form_error string
chunked_transfer bool
max_chunk_len int = 20
}
fn (ctx Context) init_once()
declaring init_once in your App struct is optional
fn (ctx Context) init()
declaring init in your App struct is optional
fn (mut ctx Context) send_response_to_client(mimetype string, res string) bool
vweb intern function
fn (mut ctx Context) html(s string) Result
Response HTTP_OK with s as payload with content-type text/html
fn (mut ctx Context) text(s string) Result
Response HTTP_OK with s as payload with content-type text/plain
fn (mut ctx Context) json(s string) Result
Response HTTP_OK with s as payload with content-type application/json
fn (mut ctx Context) ok(s string) Result
Response HTTP_OK with s as payload
fn (mut ctx Context) server_error(ecode int) Result
Response a server error
fn (mut ctx Context) redirect(url string) Result
Redirect to an url
fn (mut ctx Context) not_found() Result
Send an not_found response
fn (mut ctx Context) enable_chunked_transfer(max_chunk_len int)
Enables chunk transfer with max_chunk_len per chunk
fn (mut ctx Context) set_content_type(typ string)
Sets the response content type
fn (mut ctx Context) set_status(code int, desc string)
Sets the response status
fn (mut ctx Context) add_header(key string, val string)
Adds an header to the response with key and val
fn (ctx &Context) get_header(key string) string
Returns the header data from the key
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 (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 (mut ctx Context) serve_static(url string, file_path string, mime_type 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 (ctx &Context) ip() string
Returns the ip address from the current user
fn (mut ctx Context) error(s string)
Set s to the form error
struct Cookie {
name string
value string
expires time.Time
secure bool
http_only bool
}
struct Result {
}