diff --git a/.travis.yml b/.travis.yml index 69e4282..6427a56 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,9 @@ os: - linux julia: - 1.0 + - 1.1 + - 1.2 + - 1.3 - nightly matrix: fast_finish: true @@ -11,9 +14,5 @@ matrix: - julia: nightly notifications: email: false -#script: -# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi -# - julia -e 'Pkg.clone(pwd()); Pkg.build("ZMQ"); Pkg.build("JuliaWebAPI"); Pkg.test("JuliaWebAPI"; coverage=true)'; after_success: - - julia -e 'cd(Pkg.dir("JuliaWebAPI")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'; -# - julia -e 'cd(Pkg.dir("JuliaWebAPI")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'; + - julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'; diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..073823f --- /dev/null +++ b/Project.toml @@ -0,0 +1,30 @@ +name = "JuliaWebAPI" +uuid = "480116ec-64ea-5dec-baca-db6b11e96e37" +keywords = ["julia", "rest", "api"] +license = "MIT" +desc = "Julia package for deploying APIs" +version = "0.6.1" + +[deps] +Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" +JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" +Sockets = "6462fe0b-24de-5631-8697-dd941f90decc" +ZMQ = "c2297ded-f4af-51ae-bb23-16f91089e4e1" + +[compat] +HTTP = "≥ 0.8.0" +ZMQ = "≥ 0.3.3" +julia = "1" + +[extras] +Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" +Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" +Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +Sockets = "6462fe0b-24de-5631-8697-dd941f90decc" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Base64", "Test", "Random", "Sockets", "Logging", "Distributed"] diff --git a/REQUIRE b/REQUIRE deleted file mode 100644 index fc810d5..0000000 --- a/REQUIRE +++ /dev/null @@ -1,4 +0,0 @@ -julia 1.0 -ZMQ 0.3.3 -JSON -HTTP 0.8.0 diff --git a/src/APIResponder.jl b/src/APIResponder.jl index 1eb732b..da73c41 100644 --- a/src/APIResponder.jl +++ b/src/APIResponder.jl @@ -97,7 +97,7 @@ function call_api(api::APISpec, conn::APIResponder, args, data::Dict{Symbol,Any} result = dynamic_invoke(conn, api.fn, args...; data...) respond(conn, api, :success, result) catch ex - logerr("api_exception: ", ex) + @error("api_exception", exception=(ex, backtrace())) respond(conn, api, :api_exception, string(ex)) end end @@ -163,7 +163,7 @@ function process(conn::APIResponder; async::Bool=false) try call_api(conn.endpoints[command], conn, args(conn.format, msg), data(conn.format, msg)) catch ex - logerr("exception ", ex) + @error("exception in process", exception=(ex, backtrace())) respond(conn, conn.endpoints[command], :invalid_data) end end @@ -190,10 +190,3 @@ function create_responder(apispecs::Array, addr, bind, nid, open=false) end api end - -function logerr(msg, ex) - iob = IOBuffer() - write(iob, msg) - showerror(iob, ex) - @error(String(take!(iob))) -end diff --git a/src/http_rpc_server.jl b/src/http_rpc_server.jl index a420997..af28e6c 100644 --- a/src/http_rpc_server.jl +++ b/src/http_rpc_server.jl @@ -9,7 +9,7 @@ end function isvalidcmd(cmd) isempty(cmd) && return false - Base.is_id_start_char(cmd[1]) || return false + ((cmd[1] == ':') || Base.is_id_start_char(cmd[1])) || return false for c in cmd Base.is_id_char(c) || return false end @@ -198,8 +198,7 @@ function http_handler(apis::Channel{APIInvoker{T,F}}, preproc::Function, req::HT end catch e res = HTTP.Response(500) - Base.showerror(stderr, e, catch_backtrace()) - @error("Exception in handler: ", e) + @error("Exception in handler: ", exception=(e, backtrace())) end @debug("response", res) return res diff --git a/test/clnt.jl b/test/clnt.jl index 5707f56..70a70a5 100644 --- a/test/clnt.jl +++ b/test/clnt.jl @@ -79,6 +79,12 @@ function run_clnt(fmt, tport) @test resp["code"] == 500 @test occursin("testing exception handling", resp["data"]["data"]) + # Test terminate + println("testing server termination...") + resp = apicall(apiclnt, ":terminate") + @test resp["code"] == 200 + @test isempty(resp["data"]) + close(ctx) close(tport) end @@ -92,11 +98,13 @@ function run_httpclnt() resp = HTTP.get("http://localhost:8888/invalidapi"; status_exception=false) @test resp.status == 404 - resp = JSON.parse(String(HTTP.get("http://localhost:8888/testfn1/1/2"; status_exception=false).body)) + respstr = String(HTTP.get("http://localhost:8888/testfn1/1/2"; status_exception=false).body) + resp = JSON.parse(respstr) @test resp["code"] == 0 @test resp["data"] == 5 - resp = JSON.parse(String(HTTP.get("http://localhost:8888/testfn1/1/2"; query=Dict(:narg1=>3,:narg2=>4), status_exception=false).body)) + respstr = String(HTTP.get("http://localhost:8888/testfn1/1/2"; query=Dict(:narg1=>3,:narg2=>4), status_exception=false).body) + resp = JSON.parse(respstr) @test resp["code"] == 0 @test resp["data"] == 11 @@ -104,7 +112,8 @@ function run_httpclnt() filename = "a.txt" postdata = """------WebKitFormBoundaryIabcPsAlNKQmowCx\r\nContent-Disposition: form-data; name="filedata"; filename="a.txt"\r\nContent-Type: text/plain\r\n\r\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\r\n------WebKitFormBoundaryIabcPsAlNKQmowCx\r\nContent-Disposition: form-data; name="filename"\r\n\r\na.txt\r\n------WebKitFormBoundaryIabcPsAlNKQmowCx--\r\n""" headers = Dict("Content-Type"=>"multipart/form-data; boundary=----WebKitFormBoundaryIabcPsAlNKQmowCx") - resp = JSON.parse(String(HTTP.post("http://localhost:8888/testFile"; headers=headers, body=postdata, status_exception=false).body)) + respstr = String(HTTP.post("http://localhost:8888/testFile"; headers=headers, body=postdata, status_exception=false).body) + resp = JSON.parse(respstr) @test resp["code"] == 0 @test resp["data"] == "5,446"