mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
http: change server handler from function to interface (#10994)
This commit is contained in:
parent
836ac54d12
commit
cc9463401e
@ -7,31 +7,21 @@ import io
|
||||
import net
|
||||
import time
|
||||
|
||||
interface Handler {
|
||||
handle(Request) Response
|
||||
}
|
||||
|
||||
pub struct Server {
|
||||
pub mut:
|
||||
port int = 8080
|
||||
handler fn (Request) Response
|
||||
port int = 8080
|
||||
handler Handler = DebugHandler{}
|
||||
read_timeout time.Duration = 30 * time.second
|
||||
write_timeout time.Duration = 30 * time.second
|
||||
}
|
||||
|
||||
pub fn (mut s Server) listen_and_serve() ? {
|
||||
if voidptr(s.handler) == 0 {
|
||||
if s.handler is DebugHandler {
|
||||
eprintln('Server handler not set, using debug handler')
|
||||
s.handler = fn (req Request) Response {
|
||||
$if debug {
|
||||
eprintln('[$time.now()] $req.method $req.url\n\r$req.header\n\r$req.data - 200 OK')
|
||||
} $else {
|
||||
eprintln('[$time.now()] $req.method $req.url - 200')
|
||||
}
|
||||
mut r := Response{
|
||||
text: req.data
|
||||
header: req.header
|
||||
}
|
||||
r.set_status(.ok)
|
||||
r.set_version(req.version)
|
||||
return r
|
||||
}
|
||||
}
|
||||
mut l := net.listen_tcp(.ip6, ':$s.port') ?
|
||||
eprintln('Listening on :$s.port')
|
||||
@ -63,9 +53,28 @@ fn (mut s Server) parse_and_respond(mut conn net.TcpConn) {
|
||||
}
|
||||
return
|
||||
}
|
||||
mut resp := s.handler(req)
|
||||
mut resp := s.handler.handle(req)
|
||||
if resp.version() == .unknown {
|
||||
resp.set_version(req.version)
|
||||
}
|
||||
conn.write(resp.bytes()) or { eprintln('error sending response: $err') }
|
||||
}
|
||||
|
||||
// DebugHandler implements the Handler interface by echoing the request
|
||||
// in the response
|
||||
struct DebugHandler {}
|
||||
|
||||
fn (d DebugHandler) handle(req Request) Response {
|
||||
$if debug {
|
||||
eprintln('[$time.now()] $req.method $req.url\n\r$req.header\n\r$req.data - 200 OK')
|
||||
} $else {
|
||||
eprintln('[$time.now()] $req.method $req.url - 200')
|
||||
}
|
||||
mut r := Response{
|
||||
text: req.data
|
||||
header: req.header
|
||||
}
|
||||
r.set_status(.ok)
|
||||
r.set_version(req.version)
|
||||
return r
|
||||
}
|
||||
|
@ -258,12 +258,18 @@ pub fn mark_used(mut table ast.Table, pref &pref.Preferences, ast_files []&ast.F
|
||||
}
|
||||
for itype in interface_info.types {
|
||||
pitype := itype.set_nr_muls(1)
|
||||
mut itypes := [itype]
|
||||
if pitype != itype {
|
||||
itypes << pitype
|
||||
}
|
||||
for method in interface_info.methods {
|
||||
interface_implementation_method_name := '${pitype}.$method.name'
|
||||
$if trace_skip_unused_interface_methods ? {
|
||||
eprintln('>> isym.name: $isym.name | interface_implementation_method_name: $interface_implementation_method_name')
|
||||
for typ in itypes {
|
||||
interface_implementation_method_name := '${int(typ)}.$method.name'
|
||||
$if trace_skip_unused_interface_methods ? {
|
||||
eprintln('>> isym.name: $isym.name | interface_implementation_method_name: $interface_implementation_method_name')
|
||||
}
|
||||
all_fn_root_names << interface_implementation_method_name
|
||||
}
|
||||
all_fn_root_names << interface_implementation_method_name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user