http: change server handler from function to interface (#10994)
parent
836ac54d12
commit
cc9463401e
|
@ -7,31 +7,21 @@ import io
|
||||||
import net
|
import net
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
interface Handler {
|
||||||
|
handle(Request) Response
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Server {
|
pub struct Server {
|
||||||
pub mut:
|
pub mut:
|
||||||
port int = 8080
|
port int = 8080
|
||||||
handler fn (Request) Response
|
handler Handler = DebugHandler{}
|
||||||
read_timeout time.Duration = 30 * time.second
|
read_timeout time.Duration = 30 * time.second
|
||||||
write_timeout time.Duration = 30 * time.second
|
write_timeout time.Duration = 30 * time.second
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut s Server) listen_and_serve() ? {
|
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')
|
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') ?
|
mut l := net.listen_tcp(.ip6, ':$s.port') ?
|
||||||
eprintln('Listening on :$s.port')
|
eprintln('Listening on :$s.port')
|
||||||
|
@ -63,9 +53,28 @@ fn (mut s Server) parse_and_respond(mut conn net.TcpConn) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
mut resp := s.handler(req)
|
mut resp := s.handler.handle(req)
|
||||||
if resp.version() == .unknown {
|
if resp.version() == .unknown {
|
||||||
resp.set_version(req.version)
|
resp.set_version(req.version)
|
||||||
}
|
}
|
||||||
conn.write(resp.bytes()) or { eprintln('error sending response: $err') }
|
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 {
|
for itype in interface_info.types {
|
||||||
pitype := itype.set_nr_muls(1)
|
pitype := itype.set_nr_muls(1)
|
||||||
|
mut itypes := [itype]
|
||||||
|
if pitype != itype {
|
||||||
|
itypes << pitype
|
||||||
|
}
|
||||||
for method in interface_info.methods {
|
for method in interface_info.methods {
|
||||||
interface_implementation_method_name := '${pitype}.$method.name'
|
for typ in itypes {
|
||||||
$if trace_skip_unused_interface_methods ? {
|
interface_implementation_method_name := '${int(typ)}.$method.name'
|
||||||
eprintln('>> isym.name: $isym.name | interface_implementation_method_name: $interface_implementation_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