Skip to content

Commit

Permalink
Update vweb.v
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurZhou authored Mar 17, 2023
1 parent ebc3d9f commit b129b8b
Showing 1 changed file with 16 additions and 35 deletions.
51 changes: 16 additions & 35 deletions vlib/vweb/vweb.v
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ pub mut:

header http.Header // response headers
// ? It doesn't seem to be used anywhere
form_error string
livereload_poll_interval_ms int = 250
form_error string
}

struct FileData {
Expand All @@ -191,8 +190,8 @@ pub fn (ctx Context) init_server() {
}

// Defining this method is optional.
// This method is called before every request (aka middleware).
// You can use it for checking user session cookies or to add headers.
// This method called before every request (aka middleware).
// Probably you can use it for check user session cookie or add header.
pub fn (ctx Context) before_request() {}

// TODO - test
Expand All @@ -203,22 +202,17 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bo
return false
}
ctx.done = true
//
mut resp := http.Response{
body: res
}
$if vweb_livereload ? {
if mimetype == 'text/html' {
resp.body = res.replace('</html>', '<script src="/vweb_livereload/${vweb_livereload_server_start}/script.js"></script>\n</html>')
}
}
// build the header after the potential modification of resp.body from above

// build header
header := http.new_header_from_map({
http.CommonHeader.content_type: mimetype
http.CommonHeader.content_length: resp.body.len.str()
http.CommonHeader.content_length: res.len.str()
}).join(ctx.header)
resp.header = header.join(vweb.headers_close)
//

mut resp := http.Response{
header: header.join(vweb.headers_close)
body: res
}
resp.set_version(.v1_1)
resp.set_status(http.status_from_int(ctx.status.int()))
send_string(mut ctx.conn, resp.bytestr()) or { return false }
Expand Down Expand Up @@ -390,10 +384,10 @@ pub fn run[T](global_app &T, port int) {

[params]
pub struct RunParams {
host string
port int = 8080
family net.AddrFamily = .ip6 // use `family: .ip, host: 'localhost'` when you want it to bind only to 127.0.0.1
startup_message bool = true
host string
port int = 8080
family net.AddrFamily = .ip6 // use `family: .ip, host: 'localhost'` when you want it to bind only to 127.0.0.1
show_startup_message bool = true
}

// run_at - start a new VWeb server, listening only on a specific address `host`, at the specified `port`
Expand Down Expand Up @@ -421,7 +415,7 @@ pub fn run_at[T](global_app &T, params RunParams) ! {
}
}
host := if params.host == '' { 'localhost' } else { params.host }
if params.startup_message {
if params.show_startup_message {
println('[Vweb] Running app on http://${host}:${params.port}/')
}
flush_stdout()
Expand Down Expand Up @@ -513,19 +507,6 @@ fn handle_conn[T](mut conn net.TcpConn, mut app T, routes map[string]Route) {
// Calling middleware...
app.before_request()

$if vweb_livereload ? {
if url.path.starts_with('/vweb_livereload/') {
if url.path.ends_with('current') {
app.handle_vweb_livereload_current()
return
}
if url.path.ends_with('script.js') {
app.handle_vweb_livereload_script()
return
}
}
}

// Static handling
if serve_if_static[T](mut app, url) {
// successfully served a static file
Expand Down

0 comments on commit b129b8b

Please sign in to comment.