Skip to content

Instantly share code, notes, and snippets.

@fukayatsu
Forked from kayakaya/edit-server.rb
Last active December 15, 2015 10:19
Show Gist options
  • Save fukayatsu/5244731 to your computer and use it in GitHub Desktop.
Save fukayatsu/5244731 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'webrick'
require 'tempfile'
Process.daemon
$EDITOR = "/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl"
$OPTIONS = ['-w']
$PORT = 9292
server = WEBrick::HTTPServer.new(:Port => $PORT)
trap('INT') { server.shutdown }
server.mount_proc('/status') do |req, res|
res.status = 200
end
server.mount_proc('/edit') do |req, res|
temp = Tempfile.new(['editwith_', '.md'])
temp << req.body if req.body
temp.close false
system $EDITOR, *$OPTIONS, temp.path
temp.open
res.body = temp.read
temp.close true
res.status = 200
res['Content-Type'] = 'text/plain'
res['Content-Length'] = res.body.bytesize
end
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment