Last active
January 11, 2024 19:39
-
-
Save chryoung/732612c46f6c4fd81da286cc593445bd to your computer and use it in GitHub Desktop.
A simple ruby server for TiddlyWiki 5
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 | |
# Install webrick | |
# gem install webrick | |
# Then run this script | |
require 'webrick' | |
require 'fileutils' | |
require 'optparse' | |
options = { | |
address: "127.0.0.1", | |
port: 5000, | |
wiki_file: "index.html", | |
backup: "bak", | |
max_backup: 5, | |
} | |
OptionParser.new do |opts| | |
opts.banner = "Usage server.rb [options]" | |
opts.on("-h", "--help", "Print this help") do | |
puts opts | |
exit | |
end | |
opts.on("-bADDRESS", "--bind=ADDRESS", "Bind to the address. [Default: #{options[:address]}]") do |addr| | |
options[:address] = addr | |
end | |
opts.on("-pPORT", "--port=PORT", "Bind to the port. [Default: #{options[:port]}]") do |port| | |
options[:port] = port | |
end | |
opts.on("-uBACKUP", "--backup=BACKUP", "Backup folder. [Default: #{options[:backup]}]") do |bak| | |
options[:backup] = bak | |
end | |
opts.on("-mMAX_BACKUP", "--max-backup=MAX_BACKUP", "The max number of backup files. [Default: #{options[:max_backup]}]") do |m| | |
options[:max_backup] | |
end | |
end.parse! | |
WIKI_FILE = options[:wiki_file] | |
BACKUP = options[:backup] | |
MAX_BACKUP = options[:max_backup] | |
module WEBrick | |
module HTTPServlet | |
class FileHandler | |
alias do_PUT do_GET | |
end | |
class DefaultFileHandler | |
def do_PUT(req, res) | |
res.body = '' | |
FileUtils.cp(WIKI_FILE, "#{BACKUP}/#{WIKI_FILE}.#{Time.now.to_i.to_s}.html") | |
File.open(WIKI_FILE, "w+") do |f| | |
f.puts(req.body) | |
end | |
# Remain only last 5 backup | |
Dir.glob("#{BACKUP}/*.html").sort.reverse.drop(MAX_BACKUP).each do |backup| | |
File.unlink backup | |
end | |
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: options[:port], DocumentRoot: ".", BindAddress: options[:address] | |
trap "INT" do | |
puts "Shutting down..." | |
server.shutdown | |
end | |
unless Dir.exist? options[:backup] | |
Dir.mkdir options[:backup] | |
end | |
server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much !
Have a good day