Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Julia 0.4 compat. Qualify logging api calls. #9

Merged
merged 1 commit into from
May 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ before_install:
- sudo apt-get install libpcre3-dev julia -y
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
script:
- julia -e 'versioninfo(); Pkg.init(); Pkg.clone(pwd()); Pkg.test("JuliaBox")'
- julia -e 'versioninfo(); Pkg.init(); Pkg.clone(pwd()); Pkg.test("JuliaWebAPI")'
12 changes: 6 additions & 6 deletions src/APIInvoker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type APIInvoker
ctx::Context
sock::Socket

function APIInvoker(addr::String, ctx::Context=Context())
function APIInvoker(addr::AbstractString, ctx::Context=Context())
a = new()
a.ctx = ctx
a.sock = Socket(ctx, REQ)
Expand All @@ -22,25 +22,25 @@ function data_dict(data::Array)
end

# call a remote api
function apicall(conn::APIInvoker, cmd::String, args...; data...)
req = Dict{String,Any}()
function apicall(conn::APIInvoker, cmd::AbstractString, args...; data...)
req = Dict{AbstractString,Any}()

req["cmd"] = cmd
isempty(args) || (req["args"] = args)
isempty(data) || (req["vargs"] = data_dict(data))

msgstr = JSON.json(req)
debug("sending request: $msgstr")
Logging.debug("sending request: $msgstr")
ZMQ.send(conn.sock, Message(JSON.json(req)))

respstr = bytestring(ZMQ.recv(conn.sock))
debug("received response $respstr")
Logging.debug("received response $respstr")
JSON.parse(respstr)
end

# construct an HTTP Response object from the API response
function httpresponse(resp::Dict)
hdrs = merge(HttpCommon.headers(), get(resp, "hdrs", Dict{String,String}()))
hdrs = merge(HttpCommon.headers(), get(resp, "hdrs", Dict{AbstractString,AbstractString}()))
data = get(resp, "data", "")
if isa(data, Array)
Response(resp["code"], hdrs, convert(Array{Uint8}, data))
Expand Down
40 changes: 20 additions & 20 deletions src/APIResponder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ end
type APIResponder
ctx::Context
sock::Socket
endpoints::Dict{String, APISpec}
endpoints::Dict{AbstractString, APISpec}

function APIResponder(addr::String, ctx::Context=Context(), bind::Bool=true)
function APIResponder(addr::AbstractString, ctx::Context=Context(), bind::Bool=true)
a = new()
a.ctx = ctx
a.sock = Socket(ctx, REP)
a.endpoints = Dict{String, APISpec}()
a.endpoints = Dict{AbstractString, APISpec}()
if bind
ZMQ.bind(a.sock, addr)
else
Expand All @@ -28,19 +28,19 @@ end
# TODO: validate method belongs to module?
function register(conn::APIResponder, f::Function;
resp_json::Bool=false,
resp_headers::Dict=Dict{String,String}())
resp_headers::Dict=Dict{AbstractString,AbstractString}())
endpt = string(f)
debug("registering endpoint [$endpt]")
Logging.debug("registering endpoint [$endpt]")
conn.endpoints[endpt] = APISpec(f, resp_json, resp_headers)
end

function respond(conn::APIResponder, code::Int, headers::Dict, resp::Any)
msg = Dict{String,Any}()
msg = Dict{AbstractString,Any}()
msg["code"] = code
(length(headers) > 0) && (msg["hdrs"] = headers)
msg["data"] = resp
msgstr = JSON.json(msg)
debug("sending response [$msgstr]")
Logging.debug("sending response [$msgstr]")
ZMQ.send(conn.sock, Message(msgstr))
end

Expand All @@ -62,25 +62,25 @@ function get_resp(api::Nullable{APISpec}, status::Symbol, resp::Any=nothing)
stresp = ((stcode != 0) && (resp === nothing)) ? "$(st[3]) : $(st[2])" : resp

if !isnull(api) && get(api).resp_json
return @compat Dict{String, Any}("code" => stcode, "data" => stresp)
return @compat Dict{AbstractString, Any}("code" => stcode, "data" => stresp)
else
return stresp
end
end

get_hdrs(api::Nullable{APISpec}) = !isnull(api) ? get(api).resp_headers : Dict{String,String}()
get_hdrs(api::Nullable{APISpec}) = !isnull(api) ? get(api).resp_headers : Dict{AbstractString,AbstractString}()

args(msg::Dict) = get(msg, "args", [])
data(msg::Dict) = convert(Dict{Symbol,Any}, get(msg, "vargs", Dict{Symbol,Any}()))

# start processing as a server
function process(conn::APIResponder)
debug("processing...")
Logging.debug("processing...")
while true
msg = JSON.parse(bytestring(ZMQ.recv(conn.sock)))

cmd = get(msg, "cmd", "")
debug("received request [$cmd]")
Logging.debug("received request [$cmd]")

if startswith(cmd, ':') # is a control command
ctrlcmd = symbol(cmd[2:end])
Expand All @@ -104,33 +104,33 @@ function process(conn::APIResponder)
respond(conn, Nullable(conn.endpoints[cmd]), :invalid_data)
end
end
info("stopped processing.")
Logging.info("stopped processing.")
end

function process(apispecs::Array, addr::String=get(ENV,"JBAPI_QUEUE",""); log_level=INFO, bind::Bool=false)
function process(apispecs::Array, addr::AbstractString=get(ENV,"JBAPI_QUEUE",""); log_level=INFO, bind::Bool=false)
api_name = get(ENV,"JBAPI_NAME", "noname")
logfile = "apisrvr_$(api_name).log"
Logging.configure(level=log_level, filename=logfile)
debug("queue is at $addr")
Logging.debug("queue is at $addr")
api = APIResponder(addr, Context(), bind)

for spec in apispecs
fn = spec[1]
resp_json = (length(spec) > 1) ? spec[2] : false
resp_headers = (length(spec) > 2) ? spec[3] : Dict{String,String}()
resp_headers = (length(spec) > 2) ? spec[3] : Dict{AbstractString,AbstractString}()
register(api, fn, resp_json=resp_json, resp_headers=resp_headers)
end

debug("processing...")
Logging.debug("processing...")
process(api)
end

function process()
Logging.configure(level=INFO, filename="apisrvr.log")
info("Reading api server configuration from environment...")
info("JBAPI_NAME=" * get(ENV,"JBAPI_NAME",""))
info("JBAPI_QUEUE=" * get(ENV,"JBAPI_QUEUE",""))
info("JBAPI_CMD=" * get(ENV,"JBAPI_CMD",""))
Logging.info("Reading api server configuration from environment...")
Logging.info("JBAPI_NAME=" * get(ENV,"JBAPI_NAME",""))
Logging.info("JBAPI_QUEUE=" * get(ENV,"JBAPI_QUEUE",""))
Logging.info("JBAPI_CMD=" * get(ENV,"JBAPI_CMD",""))

cmd = get(ENV,"JBAPI_CMD","")
eval(parse(cmd))
Expand Down
4 changes: 0 additions & 4 deletions src/JuliaWebAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ const ERR_CODES = @compat Dict{Symbol, Array}(:success => [200, 0, "
)

const CONTROL_CMDS = @compat Dict{Symbol, Array}(:terminate => ["terminate"])
if isless(Base.VERSION, v"0.4.0-")
include("nullable.jl")
end

include("APIResponder.jl")
include("APIInvoker.jl")
include("RESTServer.jl")


end # module
16 changes: 8 additions & 8 deletions src/RESTServer.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

function make_vargs(vargs::Dict{String,String})
function make_vargs(vargs::Dict{AbstractString,AbstractString})
arr = Tuple[]
for (n,v) in vargs
push!(arr, (symbol(n),v))
Expand All @@ -8,15 +8,15 @@ function make_vargs(vargs::Dict{String,String})
end

function rest_handler(api::APIInvoker, req::Request, res::Response)
debug("processing request $req")
Logging.debug("processing request $req")

try
comps = @compat split(req.resource, '?', limit=2, keep=false)
if isempty(comps)
res = Response(404)
else
path = shift!(comps)
query = isempty(comps) ? Dict{String,String}() : parsequerystring(comps[1])
query = isempty(comps) ? Dict{AbstractString,AbstractString}() : parsequerystring(comps[1])
args = @compat split(path, '/', keep=false)
data_dict = isempty(req.data) ? query : merge(query, parsequerystring(req.data))

Expand All @@ -25,11 +25,11 @@ function rest_handler(api::APIInvoker, req::Request, res::Response)
else
cmd = shift!(args)
if isempty(data_dict)
debug("calling cmd $cmd with args $args")
Logging.debug("calling cmd $cmd with args $args")
res = httpresponse(apicall(api, cmd, args...))
else
vargs = make_vargs(data_dict)
debug("calling cmd $cmd with args $args, vargs $vargs")
Logging.debug("calling cmd $cmd with args $args, vargs $vargs")
res = httpresponse(apicall(api, cmd, args...; vargs...))
end
end
Expand All @@ -39,12 +39,12 @@ function rest_handler(api::APIInvoker, req::Request, res::Response)
Base.error_show(STDERR, e, catch_backtrace())
err("Exception in handler: $e")
end
debug("\tresponse $res")
Logging.debug("\tresponse $res")
return res
end

on_error(client, err) = err("HTTP error: $err")
on_listen(port) = info("listening on port $(port)...")
on_listen(port) = Logging.info("listening on port $(port)...")

type RESTServer
api::APIInvoker
Expand All @@ -68,7 +68,7 @@ type RESTServer
end

function run_rest(api::APIInvoker, port::Int)
debug("running rest server...")
Logging.debug("running rest server...")
rest = RESTServer(api)
run(rest.server, port)
end
Expand Down
55 changes: 0 additions & 55 deletions src/nullable.jl

This file was deleted.