Class: LS4::HTTPGatewayService
- Inherits:
-
Service
- Object
- EventBus::Singleton
- Service
- LS4::HTTPGatewayService
- Defined in:
- lib/ls4/service/gw_http.rb
Defined Under Namespace
Classes: Responder
Constant Summary collapse
- DEFAULT_FORMAT =
'json'
- DEFAULT_TEMPLATE =
<<EOF <html> <head> <title><%= code %> <%= title %></title> </head> <body> <center><h1><%= code %> <%= title %></h1></center> <hr> <center>LS4 <%= LS4::VERSION %></center> </body> </html> EOF
Class Method Summary collapse
Instance Method Summary collapse
-
#call_attrs(env) ⇒ Object
attrs/<key> get(data/key) => get_attrs vtime=i => gett_attrs vname=s => getv_attrs format=json/msgpack/tsv post(attrs/key, json) => update_attrs put(attrs/key, json) => update_attrs TODO delete(data/key).
-
#call_data(env) ⇒ Object
data/<key> get(data/key) => get_data vtime=i => gett_data vname=s => getv_data post(data/key, data) => add_data vname=s => addv_data attrs=formated => add/addv format=json/msgpack/tsv put(data/key, data) => add_data vname=s => addv_data attrs=formated => add/addv format=json/msgpack/tsv TODO delete(data/key).
-
#call_direct(env) ⇒ Object
direct/<rsid>/<vtime>/<key> => get_direct.
-
#call_redirect(env) ⇒ Object
redirect/<key> get(data/key) => url vtime=i => urlt vname=s => urlv.
-
#call_rpc(env) ⇒ Object
api/<cmd> get(api/cmd?k=v) post(api/cmd?k=v) cmd: get_data key= [vtime=] [vname=] get_attrs key= [vtime=] [vname=] [format=] add key= [vname=] data= [attrs= [format=]] add_data (alias of add) update_attrs key= attrs= [format=] delete key= [vtimie=] [vname=] remove key= url key= [vtime=] [vname=].
- #http_attrs_get(env) ⇒ Object
- #http_attrs_post(env) ⇒ Object
- #http_attrs_put(env) ⇒ Object
- #http_data_get(env) ⇒ Object
- #http_data_post(env) ⇒ Object
- #http_data_put(env) ⇒ Object
- #http_direct_get(env) ⇒ Object
- #http_redirect_get(env) ⇒ Object
- #http_rpc_add(env) ⇒ Object
- #http_rpc_delete(env) ⇒ Object
- #http_rpc_get_attrs(env) ⇒ Object
- #http_rpc_get_data(env) ⇒ Object
- #http_rpc_remove(env) ⇒ Object
- #http_rpc_update_attrs(env) ⇒ Object
- #http_rpc_url(env) ⇒ Object
- #init(addr, tmplfile) ⇒ Object
-
#initialize ⇒ HTTPGatewayService
constructor
A new instance of HTTPGatewayService.
Methods inherited from Service
Methods included from EventBus::SingletonMixin
#ebus_bind!, #ebus_connect, extended
Methods included from EventBus::BusMixin
#ebus_all_slots, #ebus_disconnect!
Methods included from EventBus::DeclarerBase::Methods
#connect, #ebus_all_slots, #ebus_call_log, #ebus_call_slots, #ebus_signal_error, #ebus_signal_log, #ebus_signal_slots
Methods included from EventBus::DeclarerBase
Constructor Details
#initialize ⇒ HTTPGatewayService
Returns a new instance of HTTPGatewayService.
46 47 48 49 |
# File 'lib/ls4/service/gw_http.rb', line 46 def initialize @thread = nil @server = nil end |
Class Method Details
Instance Method Details
#call_attrs(env) ⇒ Object
attrs/<key>
get(data/key) => get_attrs
vtime=i => gett_attrs
vname=s => getv_attrs
format=json/msgpack/tsv
post(attrs/key, json) => update_attrs
put(attrs/key, json) => update_attrs
TODO delete(data/key)
225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/ls4/service/gw_http.rb', line 225 def call_attrs(env) case env['REQUEST_METHOD'] when 'GET' http_attrs_get(env) when 'POST' http_attrs_post(env) when 'PUT' http_attrs_put(env) else return html_response(405, 'Method Not Allowed') end end |
#call_data(env) ⇒ Object
data/<key>
get(data/key) => get_data
vtime=i => gett_data
vname=s => getv_data
post(data/key, data) => add_data
vname=s => addv_data
attrs=formated => add/addv
format=json/msgpack/tsv
put(data/key, data) => add_data
vname=s => addv_data
attrs=formated => add/addv
format=json/msgpack/tsv
TODO delete(data/key)
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ls4/service/gw_http.rb', line 93 def call_data(env) case env['REQUEST_METHOD'] when 'GET' http_data_get(env) when 'POST' http_data_post(env) when 'PUT' http_data_put(env) else return html_response(405, 'Method Not Allowed') end end |
#call_direct(env) ⇒ Object
direct/<rsid>/<vtime>/<key> => get_direct
618 619 620 621 622 623 624 625 |
# File 'lib/ls4/service/gw_http.rb', line 618 def call_direct(env) case env['REQUEST_METHOD'] when 'GET' http_direct_get(env) else return html_response(405, 'Method Not Allowed') end end |
#call_redirect(env) ⇒ Object
redirect/<key>
get(data/key) => url
vtime=i => urlt
vname=s => urlv
330 331 332 333 334 335 336 337 |
# File 'lib/ls4/service/gw_http.rb', line 330 def call_redirect(env) case env['REQUEST_METHOD'] when 'GET' http_redirect_get(env) else return html_response(405, 'Method Not Allowed') end end |
#call_rpc(env) ⇒ Object
api/<cmd>
get(api/cmd?k=v)
post(api/cmd?k=v)
cmd:
get_data key= [vtime=] [vname=]
get_attrs key= [vtime=] [vname=] [format=]
add key= [vname=] data= [attrs= [format=]]
add_data (alias of add)
update_attrs key= attrs= [format=]
delete key= [vtimie=] [vname=]
remove key=
url key= [vtime=] [vname=]
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/ls4/service/gw_http.rb', line 386 def call_rpc(env) m = env['REQUEST_METHOD'] if m != 'GET' && m != 'POST' return html_response(405, 'Method Not Allowed') end case get_cmd_path_info(env) when 'get_data' http_rpc_get_data(env) when 'get_attrs' http_rpc_get_attrs(env) when 'add' http_rpc_add(env) when 'add_data' http_rpc_add(env) when 'update_attrs' http_rpc_update_attrs(env) when 'delete' http_rpc_delete(env) when 'remove' http_rpc_remove(env) when 'url' http_rpc_url(env) else return html_response(406, 'Not Acceptable Method') end end |
#http_attrs_get(env) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/ls4/service/gw_http.rb', line 238 def http_attrs_get(env) request = ::Rack::Request.new(env) key = get_key_path_info(env) format = optional_str(request, 'format') format ||= DEFAULT_FORMAT vtime = optional_int(request, 'vtime') if vtime check_request(request, %w[format vtime]) else vname = optional_str(request, 'vname') check_request(request, %w[format vname]) end if vtime attrs = submit(GWRPCBus, :gett_attrs, vtime, key) elsif vname attrs = submit(GWRPCBus, :getv_attrs, vname, key) else attrs = submit(GWRPCBus, :get_attrs, key) end if attrs attrs, ct = format_attrs(attrs, format) unless attrs return html_response(400, 'Bad Request', "unknown format `#{format}'") end body = [attrs] return [200, {'Content-Type'=>ct}, body] else if vtime return html_response(404, 'Not Found', "key=`#{key}' vtime=#{vtime}") elsif vname return html_response(404, 'Not Found', "key=`#{key}' vname=#{vname}") else return html_response(404, 'Not Found', "key=`#{key}'") end end end |
#http_attrs_post(env) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/ls4/service/gw_http.rb', line 277 def http_attrs_post(env) request = ::Rack::Request.new(env) key = get_key_path_info(env) attrs = require_str(request, 'attrs') format = optional_str(request, 'format') format ||= DEFAULT_FORMAT check_request(request, %w[attrs format]) attrs = parse_attrs(attrs, format) unless attrs return html_response(400, 'Bad Request', "unknown format `#{format}'") end okey = submit(GWRPCBus, :update_attrs, key, attrs) if okey return html_response(200, 'OK') else return html_response(404, 'Not Found', "key=`#{key}'") end end |
#http_attrs_put(env) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/ls4/service/gw_http.rb', line 299 def http_attrs_put(env) if env['HTTP_EXPECT'] == '100-continue' return html_response(417, 'Exception Failed') end request = ::Rack::Request.new(env) key = get_key_path_info(env) format = optional_str(request, 'format') format ||= DEFAULT_FORMAT check_request(request, %w[format]) attrs = env['rack.input'].read attrs = parse_attrs(attrs, format) unless attrs return html_response(400, 'Bad Request', "unknown format `#{format}'") end okey = submit(GWRPCBus, :update_attrs, key, attrs) if okey return html_response(202, 'Accepted') else return html_response(404, 'Not Found', "key=`#{key}'") end end |
#http_data_get(env) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ls4/service/gw_http.rb', line 106 def http_data_get(env) request = ::Rack::Request.new(env) key = get_key_path_info(env) vtime = optional_int(request, 'vtime') if vtime check_request(request, %w[vtime]) else vname = optional_str(request, 'vname') check_request(request, %w[vname]) end if vtime data = submit(GWRPCBus, :gett_data, vtime, key) elsif vname data = submit(GWRPCBus, :getv_data, vname, key) else data = submit(GWRPCBus, :get_data, key) end if data body = [data] return [200, {'Content-Type'=>'application/octet-stream'}, body] else if vtime return html_response(404, 'Not Found', "key=`#{key}' vtime=#{vtime}") elsif vname return html_response(404, 'Not Found', "key=`#{key}' vname=#{vname}") else return html_response(404, 'Not Found', "key=`#{key}'") end end end |
#http_data_post(env) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ls4/service/gw_http.rb', line 139 def http_data_post(env) request = ::Rack::Request.new(env) key = get_key_path_info(env) data = require_str(request, 'data') vname = optional_str(request, 'vname') attrs = optional_str(request, 'attrs') if attrs format = optional_str(request, 'format') format ||= DEFAULT_FORMAT check_request(request, %w[data vname attrs format]) else check_request(request, %w[data vname]) end if attrs attrs = parse_attrs(attrs, format) unless attrs return html_response(400, 'Bad Request', "unknown format `#{format}'") end if vname okey = submit(GWRPCBus, :addv, vname, key, data, attrs) else okey = submit(GWRPCBus, :add, key, data, attrs) end else if vname okey = submit(GWRPCBus, :addv_data, vname, key, data) else okey = submit(GWRPCBus, :add_data, key, data) end end # FIXME return okey? return html_response(200, 'OK') end |
#http_data_put(env) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/ls4/service/gw_http.rb', line 175 def http_data_put(env) if env['HTTP_EXPECT'] == '100-continue' return html_response(417, 'Exception Failed') end request = ::Rack::Request.new(env) key = get_key_path_info(env) vname = optional_str(request, 'vname') attrs = optional_str(request, 'attrs') if attrs format = optional_str(request, 'format') format ||= DEFAULT_FORMAT check_request(request, %w[vname attrs format]) else check_request(request, %w[vname]) end data = env['rack.input'].read if attrs attrs = parse_attrs(attrs, format) unless attrs return html_response(400, 'Bad Request', "unknown format `#{format}'") end if vname okey = submit(GWRPCBus, :addv, vname, key, data, attrs) else okey = submit(GWRPCBus, :add, key, data, attrs) end else if vname okey = submit(GWRPCBus, :addv_data, vname, key, data) else okey = submit(GWRPCBus, :add_data, key, data) end end # FIXME return okey? return html_response(202, 'Accepted') end |
#http_direct_get(env) ⇒ Object
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/ls4/service/gw_http.rb', line 627 def http_direct_get(env) request = ::Rack::Request.new(env) path = get_key_path_info(env) rsid, vtime, key = path.split('/', 3) rsid = rsid.to_i vtime = vtime.to_i check_request(request, %w[]) okey = ObjectKey.new(key, vtime, rsid) data = submit(DSRPCBus, :get_direct, okey) if data body = [data] return [200, {'Content-Type'=>'application/octet-stream'}, body] else return html_response(404, 'Not Found', "rsid=#{rsid} key=`#{key}' vtime=#{vtime}") end end |
#http_redirect_get(env) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/ls4/service/gw_http.rb', line 339 def http_redirect_get(env) request = ::Rack::Request.new(env) key = get_key_path_info(env) vtime = optional_int(request, 'vtime') if vtime check_request(request, %w[vtime]) else vname = optional_str(request, 'vname') check_request(request, %w[vname]) end if vtime url = submit(GWRPCBus, :urlt, vtime, key) elsif vname url = submit(GWRPCBus, :urlv, vname, key) else url = submit(GWRPCBus, :url, key) end if url body = [url] return [302, {'Content-Type'=>'text/plain', 'Location'=>url}, body] else if vtime return html_response(404, 'Not Found', "key=`#{key}' vtime=#{vtime}") elsif vname return html_response(404, 'Not Found', "key=`#{key}' vname=#{vname}") else return html_response(404, 'Not Found', "key=`#{key}'") end end end |
#http_rpc_add(env) ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/ls4/service/gw_http.rb', line 485 def http_rpc_add(env) request = ::Rack::Request.new(env) key = require_str(request, 'key') data = require_str(request, 'data') vname = optional_str(request, 'vname') attrs = optional_str(request, 'attrs') if attrs format = optional_str(request, 'format') format ||= DEFAULT_FORMAT check_request(request, %w[key vname data attrs format]) else check_request(request, %w[key vname data]) end if attrs attrs = parse_attrs(attrs, format) unless attrs return html_response(400, 'Bad Request', "unknown format `#{format}'") end if vname okey = submit(GWRPCBus, :addv, vname, key, data, attrs) else okey = submit(GWRPCBus, :add, key, data, attrs) end else if vname okey = submit(GWRPCBus, :addv_data, vname, key, data) else okey = submit(GWRPCBus, :add_data, key, data) end end # FIXME return okey? return html_response(200, 'OK') end |
#http_rpc_delete(env) ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/ls4/service/gw_http.rb', line 543 def http_rpc_delete(env) request = ::Rack::Request.new(env) key = require_str(request, 'key') vtime = optional_int(request, 'vtime') if vtime check_request(request, %w[key vtime]) else vname = optional_str(request, 'vname') check_request(request, %w[key vname]) end if vtime deleted = submit(GWRPCBus, :deletet, vtime, key) elsif vname deleted = submit(GWRPCBus, :deletev, vname, key) else deleted = submit(GWRPCBus, :delete, key) end if deleted return html_response(200, 'OK') else return html_response(404, 'Not Found', "key=`#{key}'") end end |
#http_rpc_get_attrs(env) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/ls4/service/gw_http.rb', line 446 def http_rpc_get_attrs(env) request = ::Rack::Request.new(env) key = require_str(request, 'key') format = optional_str(request, 'format') format ||= DEFAULT_FORMAT vtime = optional_int(request, 'vtime') if vtime check_request(request, %w[format vtime]) else vname = optional_str(request, 'vname') check_request(request, %w[format vname]) end if vtime attrs = submit(GWRPCBus, :gett_attrs, vtime, key) elsif vname attrs = submit(GWRPCBus, :getv_attrs, vname, key) else attrs = submit(GWRPCBus, :get_attrs, key) end if attrs attrs, ct = format_attrs(attrs, format) unless attrs return html_response(400, 'Bad Request', "unknown format `#{format}'") end body = [attrs] return [200, {'Content-Type'=>ct}, body] else if vtime return html_response(404, 'Not Found', "key=`#{key}' vtime=#{vtime}") elsif vname return html_response(404, 'Not Found', "key=`#{key}' vname=#{vname}") else return html_response(404, 'Not Found', "key=`#{key}'") end end end |
#http_rpc_get_data(env) ⇒ Object
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/ls4/service/gw_http.rb', line 413 def http_rpc_get_data(env) request = ::Rack::Request.new(env) key = require_str(request, 'key') vtime = optional_int(request, 'vtime') if vtime check_request(request, %w[key vtime]) else vname = optional_str(request, 'vname') check_request(request, %w[key vname]) end if vtime data = submit(GWRPCBus, :gett_data, vtime, key) elsif vname data = submit(GWRPCBus, :getv_data, vname, key) else data = submit(GWRPCBus, :get_data, key) end if data body = [data] return [200, {'Content-Type'=>'application/octet-stream'}, body] else if vtime return html_response(404, 'Not Found', "key=`#{key}' vtime=#{vtime}") elsif vname return html_response(404, 'Not Found', "key=`#{key}' vname=#{vname}") else return html_response(404, 'Not Found', "key=`#{key}'") end end end |
#http_rpc_remove(env) ⇒ Object
569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/ls4/service/gw_http.rb', line 569 def http_rpc_remove(env) request = ::Rack::Request.new(env) key = require_str(request, 'key') check_request(request, %w[key]) removed = submit(GWRPCBus, :remove, key) if removed return html_response(200, 'OK') else return html_response(404, 'Not Found', "key=`#{key}'") end end |
#http_rpc_update_attrs(env) ⇒ Object
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/ls4/service/gw_http.rb', line 521 def http_rpc_update_attrs(env) request = ::Rack::Request.new(env) key = require_str(request, 'key') attrs = require_str(request, 'attrs') format = optional_str(request, 'format') format ||= DEFAULT_FORMAT check_request(request, %w[attrs format]) attrs = parse_attrs(attrs, format) unless attrs return html_response(400, 'Bad Request', "unknown format `#{format}'") end okey = submit(GWRPCBus, :update_attrs, key, attrs) if okey return html_response(200, 'OK') else return html_response(404, 'Not Found', "key=`#{key}'") end end |
#http_rpc_url(env) ⇒ Object
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/ls4/service/gw_http.rb', line 583 def http_rpc_url(env) request = ::Rack::Request.new(env) key = require_str(request, 'key') vtime = optional_int(request, 'vtime') if vtime check_request(request, %w[key vtime]) else vname = optional_str(request, 'vname') check_request(request, %w[key vname]) end if vtime url = submit(GWRPCBus, :urlt, vtime, key) elsif vname url = submit(GWRPCBus, :urlv, vname, key) else url = submit(GWRPCBus, :url, key) end if url body = [url] return [200, {'Content-Type'=>'text/plain'}, body] else if vtime return html_response(404, 'Not Found', "key=`#{key}' vtime=#{vtime}") elsif vname return html_response(404, 'Not Found', "key=`#{key}' vname=#{vname}") else return html_response(404, 'Not Found', "key=`#{key}'") end end end |
#init(addr, tmplfile) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ls4/service/gw_http.rb', line 51 def init(addr, tmplfile) if tmplfile erb = File.read(tmplfile) else erb = DEFAULT_TEMPLATE end @erb = ERB.new(erb) opt = { :BindAddress => addr.host, :Port => addr.port, } @server = ::WEBrick::HTTPServer.new(opt) me = self app = ::Rack::URLMap.new({ '/data' => Proc.new {|env| me.call_data(env) }, '/attrs' => Proc.new {|env| me.call_attrs(env) }, '/api' => Proc.new {|env| me.call_rpc(env) }, '/direct' => Proc.new {|env| me.call_direct(env) }, '/redirect' => Proc.new {|env| me.call_redirect(env) }, }) @server.mount("/", ::Rack::Handler::WEBrick, app) @thread = Thread.new do @server.start end end |