Skip to content

Commit

Permalink
Merge pull request #70 from JuliaWeb/tan/misc
Browse files Browse the repository at this point in the history
drop 0.6 support
  • Loading branch information
tanmaykm authored Sep 5, 2018
2 parents 550f4b1 + ceb8572 commit 9811686
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 174 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.6
- 0.7
- 1.0
- nightly
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Create a file `srvr.jl` with the following code
```julia
# Load required packages
using JuliaWebAPI
using Compat

# Define functions testfn1 and testfn2 that we shall expose
function testfn1(arg1, arg2; narg1="1", narg2="2")
Expand All @@ -51,8 +50,6 @@ julia srvr.jl &
Then, on a Julia REPL, run the following code
```julia
using JuliaWebAPI #Load package
using Logging
Logging.configure(level=INFO);

#Create the ZMQ client that talks to the ZMQ listener above
const apiclnt = APIInvoker("tcp://127.0.0.1:9999");
Expand Down
4 changes: 1 addition & 3 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
julia 0.6
julia 0.7
ZMQ 0.3.3
JSON
Logging 0.3.1
Compat 0.70.0
HTTP 0.6.12
7 changes: 2 additions & 5 deletions examples/filedownload/httpsrvr.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using JuliaWebAPI
using Logging
using Base.Test

Logging.configure(level=INFO)
using Test

const apiclnt = APIInvoker("tcp://127.0.0.1:9999")

Logging.info("Open http://localhost:12000/listfiles with a web browser to download files")
@info("Open http://localhost:12000/listfiles with a web browser to download files")
run_http(apiclnt, 12000)
4 changes: 1 addition & 3 deletions examples/filedownload/srvr.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using JuliaWebAPI
using Compat
using Compat.Base64
using Logging
using Base64
using GZip

const CONTENT_DISPOSITION_TEMPLATE = "attachment; filename="
Expand Down
30 changes: 5 additions & 25 deletions src/APIResponder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,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))
@static if isdefined(Base, Symbol("@debug"))
@debug("registering", endpt)
else
Logging.debug("registering endpoint [$endpt]")
end
@debug("registering", endpt)
conn.endpoints[endpt] = APISpec(f, resp_json, resp_headers)
return conn # make fluent api possible
end
Expand Down Expand Up @@ -135,27 +131,15 @@ end
"""start processing as a server"""
function process(conn::APIResponder; async::Bool=false)
if async
@static if isdefined(Base, Symbol("@debug"))
@debug("processing async...")
else
Logging.debug("processing async...")
end
@debug("processing async...")
@async process(conn)
else
@static if isdefined(Base, Symbol("@debug"))
@debug("processing...")
else
Logging.debug("processing...")
end
@debug("processing...")
while true
msg = juliaformat(conn.format, recvreq(conn.transport))

command = cmd(conn.format, msg)
@static if isdefined(Base, Symbol("@info"))
@info("received", command)
else
Logging.info("received request: ", command)
end
@info("received", command)

if startswith(command, ':') # is a control command
ctrlcmd = Symbol(command[2:end])
Expand Down Expand Up @@ -189,11 +173,7 @@ function process(conn::APIResponder; async::Bool=false)
end
end
close(conn.transport)
@static if isdefined(Base, Symbol("@info"))
@info("stopped processing.")
else
Logging.info("stopped processing.")
end
@info("stopped processing.")
end
conn
end
Expand Down
10 changes: 3 additions & 7 deletions src/JuliaWebAPI.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
__precompile__()

module JuliaWebAPI

using ZMQ
using JSON
using Logging
using HTTP
using Compat
using Compat.Base64
using Compat.Sockets
using Compat.Serialization
using Base64
using Sockets
using Serialization

import Base: close

Expand Down
44 changes: 10 additions & 34 deletions src/http_rpc_server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,17 @@ function get_multipart_form_boundary(req::HTTP.Request)
end

function http_handler(apis::Channel{APIInvoker{T,F}}, preproc::Function, req::HTTP.Request) where {T,F}
@static if isdefined(Base, Symbol("@info"))
@info("processing", target=getfield(req, :target))
else
Logging.info("processing request ", req)
end
@info("processing", target=getfield(req, :target))
res = HTTP.Response(500)

try
comps = Compat.split(getfield(req, :target), '?', limit=2, keepempty=false)
comps = split(getfield(req, :target), '?', limit=2, keepempty=false)
if isempty(comps)
res = HTTP.Response(404)
else
res = preproc(req)
if res === nothing
comps = Compat.split(getfield(req, :target), '?', limit=2, keepempty=false)
comps = split(getfield(req, :target), '?', limit=2, keepempty=false)
path = popfirst!(comps)
data_dict = isempty(comps) ? Dict{String,String}() : HTTP.queryparams(comps[1])
multipart_boundary = get_multipart_form_boundary(req)
Expand All @@ -177,33 +173,21 @@ function http_handler(apis::Channel{APIInvoker{T,F}}, preproc::Function, req::HT
else
data_dict = parsepostdata(req, data_dict, multipart_boundary)
end
args = map(String, Compat.split(path, '/', keepempty=false))
args = map(String, split(path, '/', keepempty=false))

if isempty(args) || !isvalidcmd(args[1])
res = HTTP.Response(404)
else
cmd = popfirst!(args)
@static if isdefined(Base, Symbol("@info"))
@info("waiting for a handler")
else
Logging.info("waiting for a handler")
end
@info("waiting for a handler")
api = take!(apis)
try
if isempty(data_dict)
@static if isdefined(Base, Symbol("@debug"))
@debug("calling", cmd, args)
else
Logging.debug("calling cmd ", cmd, ", with args ", args)
end
@debug("calling", cmd, args)
res = httpresponse(api.format, apicall(api, cmd, args...))
else
vargs = make_vargs(data_dict)
@static if isdefined(Base, Symbol("@debug"))
@debug("calling", cmd, args, vargs)
else
Logging.debug("calling cmd ", cmd, ", with args ", args, ", vargs ", vargs)
end
@debug("calling", cmd, args, vargs)
res = httpresponse(api.format, apicall(api, cmd, args...; vargs...))
end
finally
Expand All @@ -214,14 +198,10 @@ function http_handler(apis::Channel{APIInvoker{T,F}}, preproc::Function, req::HT
end
catch e
res = HTTP.Response(500)
Base.showerror(Compat.stderr, e, catch_backtrace())
Base.showerror(stderr, e, catch_backtrace())
err("Exception in handler: ", e)
end
@static if isdefined(Base, Symbol("@debug"))
@debug("response", res)
else
Logging.debug("\tresponse ", res)
end
@debug("response", res)
return res
end

Expand All @@ -245,11 +225,7 @@ 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}
@static if isdefined(Base, Symbol("@debug"))
@debug("running HTTP RPC server...")
else
Logging.debug("running HTTP RPC server...")
end
@debug("running HTTP RPC server...")
HTTP.listen(ip"0.0.0.0", port; kwargs...) do req::HTTP.Request
HTTP.handle(httprpc.handler, req)
end
Expand Down
6 changes: 1 addition & 5 deletions src/msgformat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ function _dict_fmt(code::Int, headers::Dict{String,String}, resp, id=nothing)

if !isempty(headers)
msg["hdrs"] = headers
@static if isdefined(Base, Symbol("@debug"))
@debug("sending headers", headers)
else
Logging.debug("sending headers: ", headers)
end
@debug("sending headers", headers)
end

msg["code"] = code
Expand Down
48 changes: 8 additions & 40 deletions src/transport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,21 @@ struct ZMQTransport <: AbstractTransport
end

function sendrecv(conn::ZMQTransport, msgstr)
@static if isdefined(Base, Symbol("@debug"))
@debug("sending request", msgstr)
else
Logging.debug("sending request: ", msgstr)
end
@debug("sending request", msgstr)
ZMQ.send(conn.sock, ZMQ.Message(msgstr))
respstr = unsafe_string(ZMQ.recv(conn.sock))
@static if isdefined(Base, Symbol("@debug"))
@debug("received response", respstr)
else
Logging.debug("received response: ", respstr)
end
@debug("received response", respstr)
respstr
end

function sendresp(conn::ZMQTransport, msgstr)
@static if isdefined(Base, Symbol("@debug"))
@debug("sending response", msgstr)
else
Logging.debug("sending response: ", msgstr)
end
@debug("sending response", msgstr)
ZMQ.send(conn.sock, ZMQ.Message(msgstr))
end

function recvreq(conn::ZMQTransport)
reqstr = unsafe_string(ZMQ.recv(conn.sock))
@static if isdefined(Base, Symbol("@debug"))
@debug("received request", reqstr)
else
Logging.debug("received request: ", reqstr)
end
@debug("received request", reqstr)
reqstr
end

Expand All @@ -78,40 +62,24 @@ const InProcChannels = Dict{Symbol,Tuple{Channel{Any},Channel{Any}}}()
function sendrecv(conn::InProcTransport, msg)
clntq,srvrq = InProcChannels[conn.name]

@static if isdefined(Base, Symbol("@debug"))
@debug("sending request", msg)
else
Logging.debug("sending request: ", msg)
end
@debug("sending request", msg)
put!(srvrq, msg)
resp = take!(clntq)
@static if isdefined(Base, Symbol("@debug"))
@debug("received response", resp)
else
Logging.debug("received response: ", resp)
end
@debug("received response", resp)
resp
end

function sendresp(conn::InProcTransport, msg)
clntq,srvrq = InProcChannels[conn.name]
@static if isdefined(Base, Symbol("@debug"))
@debug("sending response", msg)
else
Logging.debug("sending response: ", msg)
end
@debug("sending response", msg)
put!(clntq, msg)
nothing
end

function recvreq(conn::InProcTransport)
clntq,srvrq = InProcChannels[conn.name]
req = take!(srvrq)
@static if isdefined(Base, Symbol("@debug"))
@debug("received request", req)
else
Logging.debug("received request: ", req)
end
@debug("received request", req)
req
end

Expand Down
15 changes: 5 additions & 10 deletions test/clnt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
using JuliaWebAPI
using Logging
using ZMQ
using Compat
using Compat.Test
using Compat.Random
using Test
using Random
using HTTP
using JSON

@static if isdefined(Base, Symbol("@info"))
global_logger(ConsoleLogger(stderr, Logging.Info))
else
Logging.configure(level=INFO)
end
global_logger(ConsoleLogger(stderr, Logging.Info))

const NCALLS = 100
const APIARGS = randperm(NCALLS*4)
Expand Down Expand Up @@ -76,13 +71,13 @@ function run_clnt(fmt, tport)
@test resp["code"] == 404
resp = apicall(apiclnt, "testArray", "no such argument")
@test resp["code"] == 500
@test Compat.occursin("MethodError", resp["data"])
@test occursin("MethodError", resp["data"])

# Test exceptions
println("testing server method exception handling...")
resp = apicall(apiclnt, "testException")
@test resp["code"] == 500
@test Compat.occursin("testing exception handling", resp["data"]["data"])
@test occursin("testing exception handling", resp["data"]["data"])

close(ctx)
close(tport)
Expand Down
6 changes: 2 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using JuliaWebAPI
using Logging
using Compat
using Compat.Test
using Test

const opts = Base.JLOptions()
const inline_flag = opts.can_inline == 1 ? `` : `--inline=no`
Expand All @@ -11,7 +9,7 @@ const cov_flag = (opts.code_coverage == 1) ? `--code-coverage=user` :

function run_test(script, flags)
srvrscript = joinpath(dirname(@__FILE__), script)
srvrcmd = `$(joinpath(Compat.Sys.BINDIR, "julia")) $cov_flag $inline_flag $script $flags`
srvrcmd = `$(joinpath(Sys.BINDIR, "julia")) $cov_flag $inline_flag $script $flags`
println("Running tests from ", script, "\n", "="^60)
ret = run(srvrcmd)
println("Finished ", script, "\n", "="^60)
Expand Down
Loading

0 comments on commit 9811686

Please sign in to comment.