vweb: add `file` method to vweb.Context, make the existing `json` method generic (#12211)
parent
e9aa18fcc8
commit
108644d260
|
@ -9,6 +9,7 @@ import net
|
||||||
import net.http
|
import net.http
|
||||||
import net.urllib
|
import net.urllib
|
||||||
import time
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
// A type which don't get filtered inside templates
|
// A type which don't get filtered inside templates
|
||||||
pub type RawHtml = string
|
pub type RawHtml = string
|
||||||
|
@ -236,9 +237,28 @@ pub fn (mut ctx Context) text(s string) Result {
|
||||||
return Result{}
|
return Result{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response HTTP_OK with s as payload with content-type `application/json`
|
// Response HTTP_OK with json_s as payload with content-type `application/json`
|
||||||
pub fn (mut ctx Context) json(s string) Result {
|
pub fn (mut ctx Context) json<T>(j T) Result {
|
||||||
ctx.send_response_to_client('application/json', s)
|
json_s := json.encode(j)
|
||||||
|
ctx.send_response_to_client('application/json', json_s)
|
||||||
|
return Result{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Response HTTP_OK with file as payload
|
||||||
|
pub fn (mut ctx Context) file(f_path string) Result {
|
||||||
|
ext := os.file_ext(f_path)
|
||||||
|
data := os.read_file(f_path) or {
|
||||||
|
eprint(err.msg)
|
||||||
|
ctx.server_error(500)
|
||||||
|
return Result{}
|
||||||
|
}
|
||||||
|
content_type := vweb.mime_types[ext]
|
||||||
|
if content_type == '' {
|
||||||
|
eprintln('no MIME type found for extension $ext')
|
||||||
|
ctx.server_error(500)
|
||||||
|
} else {
|
||||||
|
ctx.send_response_to_client(content_type, data)
|
||||||
|
}
|
||||||
return Result{}
|
return Result{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue