Vital
Web framework for V.
Example
mut app := vital.new()
app.get('/', fn(mut c vital.Context) ! {
c.json("hello world")
})
app.listen(3000)
Documentation
-
Server
mut app := vital_new(Config)
configuration
- case_sensitive (bool)
- read_timeout (time.Duration)
- write_timeout (time.Duration)
- methods ([]string)
mut app := vital.new(case_sensitive: false, methods: ["get", "post"])
methods:
-
listen(port number) Starts the server.
-
error_handler(cb ErrorHandler) To Handle errors in request and send response.
// ErrorHandler
fn error_handler(err vital.Exception, mut c vital.Context){}
-
method_not_allowed_handler(cb Handler) To Handle not allowed methods request.
-
static_file(path string, dir string) To Serve Dir in root system.
-
Router
To configure the endpoints of your application.
app.get(path, handler_function)
app.post(path, handler_function)
app.delete(path, handler_function)
app.put(path, handler_function)
app.patch(path, handler_function)
app.head(path, handler_function)
app.connect(path, handler_function)
app.options(path, handler_function)
app.trace(path, handler_function)
path (string): A URL path to be matched with the requested URL. The path can include parameters like "/", ":id" or "/*all"
handler_function (function): A function that handles the request and generates a response. It takes one argument: Context, and return error.
path := '/hello'
fn handler_function(mut c vital.Context) ! {
c.json({"hello":"world"})
}
app.get(path, handler_function)
path can be static like => "/user", "/posts" or "/". or can be dynamic like => "/:id" or catch all like => "/*"
Also you can group routers.
mut router := vital.new_router("/user")
router.get(path, handler_function)
app.use(router)
-
Errors
Vital allow to return error in handler funtion
fn handler_function(mut c vital.Context) ! {
// Custom error with status code
return c.error("Bad Request", 400)
}
fn handler_function(mut c vital.Context) ! {
// funtion will return error
do_something() or {return err}
}
Vital have Exception for easy and fast error response
fn handler_function(mut c vital.Context) ! {
// funtion will return error
do_something() or {return vital.bad_request_exception}
}
-
bad_request_exception
-
not_found_exception
-
forbidden_exception
-
unauthorized_exception
-
not_acceptable_exception
-
internal_server_error_exception
-
bad_gateway_exception
-
method_not_allowed_exception
You can create Custom Exception:
exception := vital.new_exception(msg, status_code)
-
Context
-
request
Request return the http.Request pointer. -
body
Used in get the request body to.
struct User {
name string
}
user := c.body(User{}) or {panic("")}
-
next
Used in middleware to executes the next handler. -
param
Method can be used to get the route parameter by key, require parameter key as string.
c.param("id") // return value for parameter :id
-
add_custom_header
Method used to add header.
c.add_custom_header(key string, value string)
-
add_header
Method used to add header with key in http.CommonHeader.
c.add_header(key http.CommonHeader, value string)
-
add_custom_header_from_map
Method used to add headers form map.
c.add_custom_header_from_map(header_map map[string]string)
-
add_header_from_map
Method used to add headers map with key in http.CommonHeader.
c.add_header_from_map(header_map map[http.CommonHeader]string)
-
remove_header
Method used to remove any number of headers.
c.remove_header(key ...string)
-
status_code
Method used add the status code that will send in response.
c.status_code(code int)
-
status
Like status_code but accept http.Status.
c.status(code http.Status)
-
send_status
Like status but will send the response too.
c.send_status(code http.Status)
-
json
Method used to send json response.
c.json[T](j T)
-
text
Method used to send text response.
c.text[T](j T)
-
html
Method used to send html file.
c.html(path string)
-
redirect
Redirects to the URL derived from the specified path.
c.redirect(url string)
-
redirect
Redirects to the URL derived from the specified path.
c.redirect(url string)
-
write
Method used to write bytes []u8 into the connection
c.write(bytes []u8)
-
write_response
Method used to write http.Response into the connection
c.write_response(response http.Response)
-
set_cookie
Method to set cookies. cookie options
struct Cookie_options {
name string [required]
value string [required]
path string = '/'
domain string
max_age int
signed bool
http_only bool
secure bool
expires time.Time
raw_expires string
}
c.set_cookie(name: name, value: value)
-
file
To stream files.
struct FileOptions {
path string [required]
start u64
stream bool
chunk int = 65536
require_range bool
range_exception Exception
}
c.file(path: file_path)
-
log
struct can be used to print information from handlers methods:
- info
- error
- debug
- warn
- verbose
c.log.info(msg)