Last active
October 6, 2015 10:58
-
-
Save youpy/2983152 to your computer and use it in GitHub Desktop.
run Sinatra app on demand
This file contains hidden or 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
# usage: | |
# | |
# ruby load.rb ruby /path/to/sinatra_ondemand.rb | |
# | |
# or (with rbenv): | |
# | |
# ruby load.rb /bin/zsh -c "RBENV_VERSION=1.9.3-rc1 ruby /path/to/sinatra_ondemand.rb" | |
require 'launch_agent' | |
agent = LaunchAgent::Base.new(*ARGV) | |
agent['Sockets'] = { | |
'Socket' => { | |
'SockServiceName' => '19000' | |
} | |
} | |
agent['inetdCompatibility'] = { | |
'Wait' => false | |
} | |
agent['ProgramArguments'] = ARGV | |
agent['Label'] = agent.job_id | |
agent.send(agent.loaded? ? :unload : :load) |
This file contains hidden or 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
#!/usr/bin/env ruby | |
# based on http://paste.mifo.sk/snippets/starting-sinatra-through-xinetd | |
require 'thin' | |
require 'socket' | |
require 'sinatra' | |
require 'rack-musicindex' | |
# git clone https://github.com/youpy/vkcom.git | |
# cd vkcom | |
# rake build | |
# gem install pkg/vkcom-x-x-x.gem | |
require 'vkcom' | |
server_thread = Thread.new do | |
# find unused port | |
TCPServer.new('0.0.0.0', 0).tap {|s| @port = s.addr[1] }.close | |
@thin = Thin::Server.new('0.0.0.0', @port, { :"max-persistent-conns" => 0 }) do | |
set :run, false | |
set :logging, false | |
use Rack::MusicIndex, { | |
'/JD' => '/Users/youpy/Downloads/JD', | |
'/Downloads' => '/Users/youpy/Downloads/', | |
'/Dropbox/Downloads' => '/Users/youpy/Dropbox/Downloads/' | |
} | |
get '/vkcom/:name' do | |
content_type 'application/xml;charset=utf-8' | |
Vkcom::Renderer::XML.new(Vkcom::Site.new('http://vk.com/%s' % params[:name])).render | |
end | |
# deprecated | |
%w/id383659 webwave hyperboreavw vprwv badwaves/.each do |name| | |
get '/%s' % name do | |
redirect '/vkcom/%s' % name, 302 | |
end | |
end | |
run Sinatra::Application | |
end | |
@thin.silent = true | |
@thin.maximum_persistent_connections = 0 | |
@thin.start | |
end | |
client_thread = Thread.new do | |
begin | |
socket = TCPSocket.open('0.0.0.0', @port) if @port | |
sleep 0.5 | |
rescue Errno::ECONNREFUSED | |
end until socket | |
buf = '' | |
finished = false | |
while true | |
break if finished | |
rs, ws = IO.select([$stdin, socket], [socket]) | |
rs.each do |r| | |
if r == $stdin | |
data = r.read(1) | |
if data | |
buf += data | |
end | |
else | |
data = r.recv(1024) | |
print data | |
finished = true if data.size == 0 | |
end | |
end | |
ws.each do |w| | |
w.write(buf) | |
buf = '' | |
end | |
end | |
server_thread.kill | |
end | |
[client_thread, server_thread].each do |t| | |
t.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment