Skip to content

Commit

Permalink
Merge pull request #75 from JuliaWeb/tan/misc
Browse files Browse the repository at this point in the history
 updates to support HTTP.jl v0.8.0, drop Julia v0.7, fix nightly build
  • Loading branch information
tanmaykm authored Jan 25, 2019
2 parents a947ffb + 44cc880 commit 2787bfb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ os:
- osx
- linux
julia:
- 0.7
- 1.0
- nightly
matrix:
Expand Down
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.7
julia 1.0
ZMQ 0.3.3
JSON
HTTP 0.6.12
HTTP 0.8.0
17 changes: 4 additions & 13 deletions src/APIResponder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ APIResponder(addr::String, ctx::Context=Context(), bound::Bool=true, id=nothing,
APIResponder(ip::IPv4, port::Int, ctx::Context=Context()) = APIResponder("tcp://$ip:$port", ctx)

function Base.show(io::IO, x::APIResponder)
println(io, "JuliaWebAPI.APIResponder with endpoints:")
Base.show_comma_array(io, keys(x.endpoints), "","")
print(io, "JuliaWebAPI.APIResponder with $(length(x.endpoints)) endpoints (", join(keys(x.endpoints), ","), ")")
end

function default_endpoint(f::Function)
Expand All @@ -50,7 +49,7 @@ TODO: validate method belongs to module?
function register(conn::APIResponder, f::Function;
resp_json::Bool=false,
resp_headers::Dict=Dict{String,String}(), endpt=default_endpoint(f))
@debug("registering", endpt)
@info("registering", endpt)
conn.endpoints[endpt] = APISpec(f, resp_json, resp_headers)
return conn # make fluent api possible
end
Expand Down Expand Up @@ -147,11 +146,7 @@ function process(conn::APIResponder; async::Bool=false)
respond(conn, nothing, :terminate, "")
break
else
@static if isdefined(Base, Symbol("@error"))
@error("invalid control command ", command)
else
err("invalid control command ", command)
end
@error("invalid control command ", command)
continue
end
end
Expand Down Expand Up @@ -200,9 +195,5 @@ function logerr(msg, ex)
iob = IOBuffer()
write(iob, msg)
showerror(iob, ex)
@static if isdefined(Base, Symbol("@error"))
@error(String(take!(iob)))
else
err(String(take!(iob)))
end
@error(String(take!(iob)))
end
12 changes: 7 additions & 5 deletions src/http_rpc_server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function http_handler(apis::Channel{APIInvoker{T,F}}, preproc::Function, req::HT
catch e
res = HTTP.Response(500)
Base.showerror(stderr, e, catch_backtrace())
err("Exception in handler: ", e)
@error("Exception in handler: ", e)
end
@debug("response", res)
return res
Expand All @@ -210,7 +210,7 @@ default_preproc(req::HTTP.Request) = nothing
# add a multipart form handler, provide default
struct HttpRpcServer{T,F}
api::Channel{APIInvoker{T,F}}
handler::HTTP.HandlerFunction
handler::HTTP.RequestHandlerFunction
end

HttpRpcServer(api::APIInvoker{T,F}, preproc::Function=default_preproc) where {T,F} = HttpRpcServer([api], preproc)
Expand All @@ -220,13 +220,15 @@ function HttpRpcServer(apis::Vector{APIInvoker{T,F}}, preproc::Function=default_
put!(api, member)
end

HttpRpcServer{T,F}(api, HTTP.HandlerFunction(req->http_handler(api, preproc, req)))
handler_fn = (req)->JuliaWebAPI.http_handler(api, preproc, req)
handler = HTTP.RequestHandlerFunction(handler_fn)
HttpRpcServer{T,F}(api, handler)
end

run_http(api::Union{Vector{APIInvoker{T,F}},APIInvoker{T,F}}, port::Int, preproc::Function=default_preproc; kwargs...) where {T,F} = run_http(HttpRpcServer(api, preproc), port; kwargs...)
function run_http(httprpc::HttpRpcServer{T,F}, port::Int; kwargs...) where {T,F}
@debug("running HTTP RPC server...")
HTTP.listen(ip"0.0.0.0", port; kwargs...) do req::HTTP.Request
@info("running HTTP RPC server...")
HTTP.listen(ip"0.0.0.0", port; kwargs...) do req
HTTP.handle(httprpc.handler, req)
end
end

0 comments on commit 2787bfb

Please sign in to comment.