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

vweb: fix serving static files

This commit is contained in:
Alexander Medvednikov 2021-05-16 04:28:11 +03:00
parent 170282b2af
commit ed6ad728d9
6 changed files with 19 additions and 4 deletions

View File

@ -123,7 +123,7 @@ pub:
typ int typ int
} }
pub enum AttributeKind { enum AttributeKind {
plain // [name] plain // [name]
string // ['name'] string // ['name']
number // [123] number // [123]

View File

@ -7,6 +7,7 @@ const (
tcp_default_write_timeout = 30 * time.second tcp_default_write_timeout = 30 * time.second
) )
[heap]
pub struct TcpConn { pub struct TcpConn {
pub mut: pub mut:
sock TcpSocket sock TcpSocket

View File

@ -126,6 +126,12 @@ fn test_orm_sqlite() {
assert users3[0].age == 29 assert users3[0].age == 29
assert users3[1].age == 31 assert users3[1].age == 31
// //
missing_user := sql db {
select from User where id == 8777
}
println('missing_user:')
println(missing_user) // zero struct
//
new_user := User{ new_user := User{
name: 'New user' name: 'New user'
age: 30 age: 30

View File

@ -21,6 +21,7 @@ fn test_sqlite() {
code = db.exec_none('vacuum') code = db.exec_none('vacuum')
assert code == 101 assert code == 101
user := db.exec_one('select * from users where id = 3') or { panic(err) } user := db.exec_one('select * from users where id = 3') or { panic(err) }
println(user)
assert user.vals.len == 2 assert user.vals.len == 2
db.close() or { panic(err) } db.close() or { panic(err) }
assert !db.is_open assert !db.is_open

View File

@ -3098,6 +3098,11 @@ pub fn (mut c Checker) enum_decl(decl ast.EnumDecl) {
if decl.fields.len == 0 { if decl.fields.len == 0 {
c.error('enum cannot be empty', decl.pos) c.error('enum cannot be empty', decl.pos)
} }
/*
if decl.is_pub && c.mod == 'builtin' {
c.error('`builtin` module cannot have enums', decl.pos)
}
*/
for i, field in decl.fields { for i, field in decl.fields {
if !c.pref.experimental && util.contains_capital(field.name) { if !c.pref.experimental && util.contains_capital(field.name) {
// TODO C2V uses hundreds of enums with capitals, remove -experimental check once it's handled // TODO C2V uses hundreds of enums with capitals, remove -experimental check once it's handled

View File

@ -322,6 +322,7 @@ pub fn run<T>(global_app &T, port int) {
} $else { } $else {
// println('vweb no db') // println('vweb no db')
} }
request_app.Context = global_app.Context // copy the context ref that contains static files map etc
// request_app.Context = Context{ // request_app.Context = Context{
// conn: 0 // conn: 0
//} //}
@ -351,7 +352,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
} }
app.Context = Context{ app.Context = Context{
req: req req: req
conn: unsafe { conn } conn: conn
form: map[string]string{} form: map[string]string{}
static_files: app.static_files static_files: app.static_files
static_mime_types: app.static_mime_types static_mime_types: app.static_mime_types
@ -385,7 +386,7 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
eprintln('error parsing path: $err') eprintln('error parsing path: $err')
return return
} }
if serve_static<T>(mut app, url) { if serve_if_static<T>(mut app, url) {
// successfully served a static file // successfully served a static file
return return
} }
@ -528,7 +529,8 @@ fn parse_attrs(name string, attrs []string) ?([]http.Method, string) {
// check if request is for a static file and serves it // check if request is for a static file and serves it
// returns true if we served a static file, false otherwise // returns true if we served a static file, false otherwise
fn serve_static<T>(mut app T, url urllib.URL) bool { [manualfree]
fn serve_if_static<T>(mut app T, url urllib.URL) bool {
// TODO: handle url parameters properly - for now, ignore them // TODO: handle url parameters properly - for now, ignore them
static_file := app.static_files[url.path] static_file := app.static_files[url.path]
mime_type := app.static_mime_types[url.path] mime_type := app.static_mime_types[url.path]