1
- @compat abstract type AbstractAPIResponder end
1
+ abstract type AbstractAPIResponder end
2
2
3
- immutable APISpec
3
+ struct APISpec
4
4
fn:: Function
5
5
resp_json:: Bool
6
6
resp_headers:: Dict{String,String}
@@ -11,15 +11,15 @@ const EndPts = Dict{String,APISpec}
11
11
"""
12
12
APIResponder holds the transport and format used for data exchange and the endpoint specifications.
13
13
"""
14
- immutable APIResponder{T<: AbstractTransport ,F<: AbstractMsgFormat }
14
+ struct APIResponder{T<: AbstractTransport ,F<: AbstractMsgFormat }
15
15
transport:: T
16
16
format:: F
17
- id:: Union{Void ,String} # optional responder id to be sent back
17
+ id:: Union{Nothing ,String} # optional responder id to be sent back
18
18
open:: Bool # whether the responder will process all functions, or only registered ones
19
19
endpoints:: EndPts
20
20
end
21
21
22
- APIResponder {T<:AbstractTransport,F<:AbstractMsgFormat} (transport:: T , format:: F , id:: Union{Void ,String} = nothing , open:: Bool = false ) = APIResponder (transport, format, id, open, EndPts ())
22
+ APIResponder (transport:: T , format:: F , id:: Union{Nothing ,String} = nothing , open:: Bool = false ) where {T <: AbstractTransport ,F <: AbstractMsgFormat } = APIResponder (transport, format, id, open, EndPts ())
23
23
24
24
"""
25
25
APIResponder holds the transport and format used for data exchange and the endpoint specifications.
@@ -31,7 +31,7 @@ APIResponder(ip::IPv4, port::Int, ctx::Context=Context()) = APIResponder("tcp://
31
31
32
32
function Base. show (io:: IO , x:: APIResponder )
33
33
println (io, " JuliaWebAPI.APIResponder with endpoints:" )
34
- Base. show_comma_array (STDOUT , keys (x. endpoints), " " ," " )
34
+ Base. show_comma_array (io , keys (x. endpoints), " " ," " )
35
35
end
36
36
37
37
function default_endpoint (f:: Function )
@@ -50,7 +50,11 @@ TODO: validate method belongs to module?
50
50
function register (conn:: APIResponder , f:: Function ;
51
51
resp_json:: Bool = false ,
52
52
resp_headers:: Dict = Dict {String,String} (), endpt= default_endpoint (f))
53
- Logging. debug (" registering endpoint [$endpt ]" )
53
+ @static if isdefined (Base, Symbol (" @debug" ))
54
+ @debug (" registering" , endpt)
55
+ else
56
+ Logging. debug (" registering endpoint [$endpt ]" )
57
+ end
54
58
conn. endpoints[endpt] = APISpec (f, resp_json, resp_headers)
55
59
return conn # make fluent api possible
56
60
end
@@ -61,17 +65,18 @@ function respond(conn::APIResponder, code::Int, headers::Dict, resp)
61
65
sendresp (conn. transport, resp)
62
66
end
63
67
64
- respond (conn:: APIResponder , api:: Nullable{ APISpec} , status:: Symbol , resp= nothing ) =
68
+ respond (conn:: APIResponder , api:: Union{Nothing, APISpec} , status:: Symbol , resp= nothing ) =
65
69
respond (conn, ERR_CODES[status][1 ], get_hdrs (api), get_resp (api, status, resp))
66
70
67
- get_hdrs (api:: Nullable{APISpec} ) = ! isnull (api) ? get (api). resp_headers : Dict {String,String} ()
71
+ get_hdrs (api:: Nothing ) = Dict {String,String} ()
72
+ get_hdrs (api:: APISpec ) = api. resp_headers
68
73
69
- function get_resp (api:: Nullable{ APISpec} , status:: Symbol , resp= nothing )
74
+ function get_resp (api:: Union{Nothing, APISpec} , status:: Symbol , resp= nothing )
70
75
st = ERR_CODES[status]
71
76
stcode = st[2 ]
72
- stresp = ((stcode != 0 ) && (resp === nothing )) ? " $ (st[3 ]) : $( st[2 ])" : resp
77
+ stresp = ((stcode != 0 ) && (resp === nothing )) ? string (st[3 ], " : " , st[2 ]) : resp
73
78
74
- if ! isnull (api) && get ( api) . resp_json
79
+ if (api != = nothing ) && api. resp_json
75
80
return Dict {String, Any} (" code" => stcode, " data" => stresp)
76
81
else
77
82
return stresp
@@ -95,10 +100,10 @@ function call_api(api::APISpec, conn::APIResponder, args, data::Dict{Symbol,Any}
95
100
narrow_args! (args)
96
101
end
97
102
result = dynamic_invoke (conn, api. fn, args... ; data... )
98
- respond (conn, Nullable ( api) , :success , result)
103
+ respond (conn, api, :success , result)
99
104
catch ex
100
105
logerr (" api_exception: " , ex)
101
- respond (conn, Nullable ( api) , :api_exception , string (ex))
106
+ respond (conn, api, :api_exception , string (ex))
102
107
end
103
108
end
104
109
@@ -130,30 +135,46 @@ end
130
135
""" start processing as a server"""
131
136
function process (conn:: APIResponder ; async:: Bool = false )
132
137
if async
133
- Logging. debug (" processing async..." )
138
+ @static if isdefined (Base, Symbol (" @debug" ))
139
+ @debug (" processing async..." )
140
+ else
141
+ Logging. debug (" processing async..." )
142
+ end
134
143
@async process (conn)
135
144
else
136
- Logging. debug (" processing..." )
145
+ @static if isdefined (Base, Symbol (" @debug" ))
146
+ @debug (" processing..." )
147
+ else
148
+ Logging. debug (" processing..." )
149
+ end
137
150
while true
138
151
msg = juliaformat (conn. format, recvreq (conn. transport))
139
152
140
153
command = cmd (conn. format, msg)
141
- Logging. info (" received request: " , command)
154
+ @static if isdefined (Base, Symbol (" @info" ))
155
+ @info (" received" , command)
156
+ else
157
+ Logging. info (" received request: " , command)
158
+ end
142
159
143
160
if startswith (command, ' :' ) # is a control command
144
161
ctrlcmd = Symbol (command[2 : end ])
145
162
if ctrlcmd === :terminate
146
- respond (conn, Nullable {APISpec} () , :terminate , " " )
163
+ respond (conn, nothing , :terminate , " " )
147
164
break
148
165
else
149
- err (" invalid control command " , command)
166
+ @static if isdefined (Base, Symbol (" @error" ))
167
+ @error (" invalid control command " , command)
168
+ else
169
+ err (" invalid control command " , command)
170
+ end
150
171
continue
151
172
end
152
173
end
153
174
154
175
if ! haskey (conn. endpoints, command)
155
176
if ! conn. open || ! isdefined (Main, Symbol (command))
156
- respond (conn, Nullable {APISpec} () , :invalid_api )
177
+ respond (conn, nothing , :invalid_api )
157
178
continue
158
179
else
159
180
_add_spec (getfield (Main, Symbol (command)), conn)
@@ -164,35 +185,19 @@ function process(conn::APIResponder; async::Bool=false)
164
185
call_api (conn. endpoints[command], conn, args (conn. format, msg), data (conn. format, msg))
165
186
catch ex
166
187
logerr (" exception " , ex)
167
- respond (conn, Nullable ( conn. endpoints[command]) , :invalid_data )
188
+ respond (conn, conn. endpoints[command], :invalid_data )
168
189
end
169
190
end
170
191
close (conn. transport)
171
- Logging. info (" stopped processing." )
192
+ @static if isdefined (Base, Symbol (" @info" ))
193
+ @info (" stopped processing." )
194
+ else
195
+ Logging. info (" stopped processing." )
196
+ end
172
197
end
173
198
conn
174
199
end
175
200
176
- function setup_logging (;log_level= INFO, nid:: String = get (ENV ," JBAPI_CID" ," " ))
177
- api_name = get (ENV ," JBAPI_NAME" , " noname" )
178
- logfile = " apisrvr_$(api_name) _$(nid) .log"
179
- Logging. configure (level= log_level, filename= logfile)
180
- end
181
-
182
- function process_async (apispecs:: Array , addr:: String = get (ENV ," JBAPI_QUEUE" ," " ); log_level= INFO, bind:: Bool = false , nid:: String = get (ENV ," JBAPI_CID" ," " ), open:: Bool = false )
183
- Base. depwarn (" processs_async is deprecated, use process(conn::APIResponder; async::Bool=true) instead" , :process )
184
- process (apispecs, addr; log_level= log_level, bind= bind, nid= nid, open= open, async= true )
185
- end
186
-
187
- function process (apispecs:: Array , addr:: String = get (ENV ," JBAPI_QUEUE" ," " ); log_level= Logging. LogLevel (get (ENV , " JBAPI_LOGLEVEL" , " INFO" )),
188
- bind:: Bool = false , nid:: String = get (ENV ," JBAPI_CID" ," " ), open:: Bool = false , async:: Bool = false )
189
- Base. depwarn (" processs(apispecs::Array,...) is deprecated, use process(conn::APIResponder; async::Bool=false) instead" , :process )
190
- setup_logging (;log_level= log_level)
191
- Logging. debug (" queue is at $addr " )
192
- api = create_responder (apispecs, addr, bind, nid, open)
193
- process (api; async= async)
194
- end
195
-
196
201
_add_spec (fn:: Function , api:: APIResponder ) = register (api, fn, resp_json= false , resp_headers= Dict {String,String} ())
197
202
198
203
function _add_spec (spec:: Tuple , api:: APIResponder )
@@ -211,26 +216,13 @@ function create_responder(apispecs::Array, addr, bind, nid, open=false)
211
216
api
212
217
end
213
218
214
- function process ()
215
- Base. depwarn (" process() is deprecated, use process(conn::APIResponder; async::Bool=false) instead" , :process )
216
- log_level = Logging. LogLevel (get (ENV , " JBAPI_LOGLEVEL" , " INFO" ))
217
- setup_logging (;log_level= log_level)
218
-
219
- Logging. info (" Reading api server configuration from environment..." )
220
- Logging. info (" JBAPI_NAME=" * get (ENV ," JBAPI_NAME" ," " ))
221
- Logging. info (" JBAPI_QUEUE=" * get (ENV ," JBAPI_QUEUE" ," " ))
222
- Logging. info (" JBAPI_CMD=" * get (ENV ," JBAPI_CMD" ," " ))
223
- Logging. info (" JBAPI_CID=" * get (ENV ," JBAPI_CID" ," " ))
224
- Logging. info (" JBAPI_LOGLEVEL=" * get (ENV ," JBAPI_LOGLEVEL" ," " ) * " as " * string (log_level))
225
-
226
- cmd = get (ENV ," JBAPI_CMD" ," " )
227
- eval (parse (cmd))
228
- nothing
229
- end
230
-
231
219
function logerr (msg, ex)
232
220
iob = IOBuffer ()
233
221
write (iob, msg)
234
222
showerror (iob, ex)
235
- err (String (take! (iob)))
223
+ @static if isdefined (Base, Symbol (" @error" ))
224
+ @error (String (take! (iob)))
225
+ else
226
+ err (String (take! (iob)))
227
+ end
236
228
end
0 commit comments