vweb: embed context

pull/7764/head
Alexander Medvednikov 2020-12-31 17:07:24 +01:00
parent cf978ca1ac
commit 3ffdcd8910
3 changed files with 22 additions and 22 deletions

View File

@ -7,8 +7,8 @@ const (
)
struct App {
vweb.Context
pub mut:
vweb vweb.Context // TODO embed
cnt int
}
@ -18,14 +18,14 @@ fn main() {
}
pub fn (mut app App) init_once() {
app.vweb.handle_static('.')
app.handle_static('.')
}
pub fn (mut app App) init() {
}
pub fn (mut app App) json_endpoint() vweb.Result {
return app.vweb.json('{"a": 3}')
return app.json('{"a": 3}')
}
pub fn (mut app App) index() vweb.Result {
@ -37,14 +37,14 @@ pub fn (mut app App) index() vweb.Result {
return $vweb.html()
}
pub fn (mut app App) text() vweb.Result {
return app.vweb.text('Hello world from vweb')
pub fn (mut app App) show_text() vweb.Result {
return app.text('Hello world from vweb')
}
pub fn (mut app App) cookie() vweb.Result {
app.vweb.set_cookie({
app.set_cookie({
name: 'cookie'
value: 'test'
})
return app.vweb.text('Headers: $app.vweb.headers')
return app.text('Headers: $app.headers')
}

View File

@ -25,7 +25,7 @@ fn (mut g Gen) comptime_call(node ast.ComptimeCall) {
}
if is_html {
// return vweb html template
g.writeln('vweb__Context_html(&app->vweb, _tmpl_res_$g.fn_decl.name); strings__Builder_free(&sb); string_free(&_tmpl_res_$g.fn_decl.name);')
g.writeln('vweb__Context_html(&app->Context, _tmpl_res_$g.fn_decl.name); strings__Builder_free(&sb); string_free(&_tmpl_res_$g.fn_decl.name);')
} else {
// return $tmpl string
fn_name := g.fn_decl.name.replace('.', '__')

View File

@ -41,8 +41,6 @@ pub const (
pub struct Context {
mut:
static_files map[string]string
static_mime_types map[string]string
content_type string = 'text/plain'
status string = '200 OK'
pub:
@ -50,6 +48,8 @@ pub:
conn net.TcpConn
// TODO Response
pub mut:
static_files map[string]string
static_mime_types map[string]string
form map[string]string
query map[string]string
headers string // response headers
@ -69,7 +69,7 @@ pub struct Cookie {
pub struct Result {
}
fn (mut ctx Context) send_response_to_client(mimetype string, res string) bool {
pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bool {
if ctx.done {
return false
}
@ -203,7 +203,7 @@ pub fn run<T>(port int) {
pub fn run_app<T>(mut app T, port int) {
println('Running a Vweb app on http://localhost:$port')
l := net.listen_tcp(port) or { panic('failed to listen') }
app.vweb = Context{}
app.Context = Context{}
app.init_once()
$for method in T.methods {
$if method.return_type is Result {
@ -315,17 +315,17 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
// println('vweb action = "$action"')
}
// mut app := T{
app.vweb = Context{
app.Context = Context{
req: req
conn: conn
form: map[string]string{}
static_files: app.vweb.static_files
static_mime_types: app.vweb.static_mime_types
static_files: app.static_files
static_mime_types: app.static_mime_types
page_gen_start: page_gen_start
}
// }
if req.method in methods_with_form {
app.vweb.parse_form(req.data)
app.parse_form(req.data)
}
if vals.len < 2 {
$if debug {
@ -335,18 +335,18 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
}
// Serve a static file if it is one
// TODO: handle url parameters properly - for now, ignore them
mut static_file_name := app.vweb.req.url
mut static_file_name := app.req.url
if static_file_name.contains('?') {
static_file_name = static_file_name.all_before('?')
}
static_file := app.vweb.static_files[static_file_name]
mime_type := app.vweb.static_mime_types[static_file_name]
static_file := app.static_files[static_file_name]
mime_type := app.static_mime_types[static_file_name]
if static_file != '' && mime_type != '' {
data := os.read_file(static_file) or {
send_string(conn, http_404) or { }
return
}
app.vweb.send_response_to_client(mime_type, data)
app.send_response_to_client(mime_type, data)
data.free()
return
}
@ -372,7 +372,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
url_words[url_words.len - 1] = url_words.last().all_before('?')
for data in tmp_query {
if data.len == 2 {
app.vweb.query[data[0]] = data[1]
app.query[data[0]] = data[1]
}
}
}
@ -509,7 +509,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
}
}
fn (mut ctx Context) parse_form(s string) {
pub fn (mut ctx Context) parse_form(s string) {
if ctx.req.method !in methods_with_form {
return
}