-
-
Save fhunter/5d765c7956c8922d24ec0c3089ea3937 to your computer and use it in GitHub Desktop.
A local server for TiddlyWiki5 that allows saving wiki.
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
require 'webrick' | |
require 'fileutils' | |
if ARGV.length != 0 | |
root = ARGV.first.gsub('\\', '/') | |
else | |
root = '.' | |
end | |
BACKUP_DIR = 'bak' | |
module WEBrick | |
module HTTPServlet | |
class FileHandler | |
alias do_PUT do_GET | |
end | |
class DefaultFileHandler | |
def do_PUT(req, res) | |
file = "#{@config[:DocumentRoot]}#{req.path}" | |
res.body = '' | |
unless Dir.exists? BACKUP_DIR | |
Dir.mkdir BACKUP_DIR | |
end | |
FileUtils.cp(file, "#{BACKUP_DIR}/#{File.basename(file, '.html')}.#{Time.now.to_i.to_s}.html") | |
File.open(file, "w+") {|f| f.puts(req.body)} | |
end | |
def do_OPTIONS(req, res) | |
res['allow'] = "GET,HEAD,POST,OPTIONS,CONNECT,PUT,DAV,dav" | |
res['x-api-access-type'] = 'file' | |
res['dav'] = 'tw5/put' | |
end | |
end | |
end | |
end | |
server = WEBrick::HTTPServer.new({:Port => 8000, :DocumentRoot => root}) | |
trap "INT" do | |
puts "Shutting down..." | |
server.shutdown | |
end | |
server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment