Last active
July 4, 2025 12:46
-
-
Save jimfoltz/ee791c1bdd30ce137bc23cce826096da 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' | |
BIND_ADDRESS = "127.0.0.1" | |
PORT = 8080 | |
BACKUP_DIR = 'bak' | |
if ARGV.length != 0 | |
root = ARGV.first.gsub('\\', '/') | |
else | |
root = '.' | |
end | |
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.exist? 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 => PORT, :DocumentRoot => root, :BindAddress => BIND_ADDRESS}) | |
# ctrl-c handler | |
trap "INT" do | |
puts "Shutting down..." | |
server.shutdown | |
end | |
puts "Serving on http://#{BIND_ADDRESS}:#{PORT}" | |
server.start |
Backups are heavy on storage, because empty wikis are 2.4MB and they backup after every small edit. With heavy usage you get to a GB in about a month.
Thanks and I agree. There are too many backup strategies to included here. At some point maybe this should be moved to a repo so contributions can be properly considered.
I have an idea to write a server that uses the put saver to save the entire file, but then parses the json store and saves each tiddler individually. That might make sense if users want to use git as a backup and history strategy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Backups are heavy on storage, because empty wikis are 2.4MB and they backup after every small edit. With heavy usage you get to a GB in about a month.
My solutions is to backup randomly every ~5th save. Of course you could use a simple counter, but I trust my luck more.
I will just leave it here for anyone who needs it, no pressure to the maintainer.