Skip to content

Instantly share code, notes, and snippets.

@jimfoltz
Last active February 26, 2024 02:18
Show Gist options
  • Save jimfoltz/ee791c1bdd30ce137bc23cce826096da to your computer and use it in GitHub Desktop.
Save jimfoltz/ee791c1bdd30ce137bc23cce826096da to your computer and use it in GitHub Desktop.
A local server for TiddlyWiki5 that allows saving wiki.
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,OPTIONS,PUT"
res['dav'] = 'anything' # TW looks for a 'dav' header, and ignores any value
end
end
end
end
server = WEBrick::HTTPServer.new({:Port => 8000, :DocumentRoot => root, :BindAddress => "127.0.0.1"})
trap "INT" do
puts "Shutting down..."
server.shutdown
end
server.start
@jimfoltz
Copy link
Author

jimfoltz commented Jan 12, 2024

@ClimaxUke

Use Dir.exist? instead.

@huataihuang - thank you for the suggestion and fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment