Created
June 17, 2014 17:50
-
-
Save rrreeeyyy/13286fa53413fa4d594c to your computer and use it in GitHub Desktop.
IPVS Web API using mruby-ipvs and mruby-simplehttpserver.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services = {} | |
def body_parser(body) | |
params = {} | |
body.split('&').each do |x| | |
tokens = x.split('=', 2) | |
if tokens && tokens.size == 2 | |
params[tokens[0]] = HTTP::URL::decode(tokens[1]) | |
end | |
end | |
params | |
end | |
server = SimpleHttpServer.new({ | |
:server_ip => "0.0.0.0", | |
:port => 8000, | |
}) | |
server.http do |r| | |
server.set_response_headers({ | |
"Server" => "mruby-simplehttpserver", | |
"Date" => server.http_date, | |
}) | |
end | |
server.location "/api/v1/service/add" do |r| | |
params = body_parser r.body | |
server.response_body = "OK" | |
service_addr = params['service_addr'] | |
service_port = params['service_port'] | |
service_key = [service_addr, service_port].join(':') | |
begin | |
service = IPVS::Service.new({ | |
'addr' => service_addr, | |
'port' => service_port.to_i | |
}).add_service | |
services[service_key] = {obj: service, dests: {}} | |
rescue => e | |
server.response_body = "ERROR: #{e.message}" | |
end | |
server.create_response | |
end | |
server.location "/api/v1/service/remove" do |r| | |
params = body_parser r.body | |
server.response_body = "OK" | |
service_addr = params['service_addr'] | |
service_port = params['service_port'] | |
service_key = [service_addr, service_port].join(':') | |
service = services[service_key] | |
begin | |
service[:obj].del_service | |
service.delete service_key | |
rescue => e | |
server.response_body = "ERROR: #{e.message}" | |
end | |
server.create_response | |
end | |
server.location "/api/v1/dest/add" do |r| | |
params = body_parser r.body | |
server.response_body = "OK" | |
service_addr = params['service_addr'] | |
service_port = params['service_port'] | |
dest_addr = params['dest_addr'] | |
dest_port = params['dest_port'] | |
service_key = [service_addr, service_port].join(':') | |
dest_key = [dest_addr, dest_port].join(':') | |
service = services[service_key] | |
begin | |
dest = IPVS::Dest.new({ | |
'addr' => dest_addr, | |
'port' => dest_port.to_i | |
}) | |
service[:obj].add_dest dest | |
service[:dests][dest_key] = dest | |
rescue => e | |
server.response_body = "ERROR: #{e.message}" | |
end | |
server.create_response | |
end | |
server.location "/api/v1/dest/remove" do |r| | |
params = body_parser r.body | |
server.response_body = "OK" | |
service_addr = params['service_addr'] | |
service_port = params['service_port'] | |
dest_addr = params['dest_addr'] | |
dest_port = params['dest_port'] | |
service_key = [service_addr, service_port].join(':') | |
dest_key = [dest_addr, dest_port].join(':') | |
service = services[service_key] | |
dest = service[:dests][dest_key] | |
begin | |
service[:obj].del_dest dest | |
service[:dests].delete dest_key | |
rescue => e | |
server.response_body = "ERROR: #{e.message}" | |
end | |
server.create_response | |
end | |
server.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Endpoint:
Add Service
Remove Service
Add Dest
Remove Dest
Usage:
Server
requirements
Client