1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vweb: embed context

This commit is contained in:
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 { struct App {
vweb.Context
pub mut: pub mut:
vweb vweb.Context // TODO embed
cnt int cnt int
} }
@ -18,14 +18,14 @@ fn main() {
} }
pub fn (mut app App) init_once() { 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) init() {
} }
pub fn (mut app App) json_endpoint() vweb.Result { 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 { pub fn (mut app App) index() vweb.Result {
@ -37,14 +37,14 @@ pub fn (mut app App) index() vweb.Result {
return $vweb.html() return $vweb.html()
} }
pub fn (mut app App) text() vweb.Result { pub fn (mut app App) show_text() vweb.Result {
return app.vweb.text('Hello world from vweb') return app.text('Hello world from vweb')
} }
pub fn (mut app App) cookie() vweb.Result { pub fn (mut app App) cookie() vweb.Result {
app.vweb.set_cookie({ app.set_cookie({
name: 'cookie' name: 'cookie'
value: 'test' 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 { if is_html {
// return vweb html template // 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 { } else {
// return $tmpl string // return $tmpl string
fn_name := g.fn_decl.name.replace('.', '__') fn_name := g.fn_decl.name.replace('.', '__')

View File

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