From cc9463401e81cdf9a3cd046bbc65e43a0124c2a5 Mon Sep 17 00:00:00 2001 From: Miccah Date: Sun, 1 Aug 2021 07:50:11 -0500 Subject: [PATCH] http: change server handler from function to interface (#10994) --- vlib/net/http/server.v | 45 +++++++++++++++++++++++--------------- vlib/v/markused/markused.v | 14 ++++++++---- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/vlib/net/http/server.v b/vlib/net/http/server.v index edb2eaf491..3804ae3af8 100644 --- a/vlib/net/http/server.v +++ b/vlib/net/http/server.v @@ -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 +} diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index 77c0957e36..bad2df95a5 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -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 } } }