Last active
February 15, 2025 17:03
-
-
Save jimfoltz/ee791c1bdd30ce137bc23cce826096da to your computer and use it in GitHub Desktop.
A local server for TiddlyWiki5 that allows saving wiki.
This file contains 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 |
Hello,
I've noticed this error. Any ideas?
Thanks in advance.
[2024-01-11 19:27:50] ERROR NoMethodError: undefined method `exists?' for Dir:Class
tw5-server.rb:22:in `do_PUT'
/usr/lib/ruby/gems/3.2.0/gems/webrick-1.8.1/lib/webrick/httpservlet/abstract.rb:105:in `service'
/usr/lib/ruby/gems/3.2.0/gems/webrick-1.8.1/lib/webrick/httpservlet/filehandler.rb:315:in `exec_handler'
/usr/lib/ruby/gems/3.2.0/gems/webrick-1.8.1/lib/webrick/httpservlet/filehandler.rb:246:in `do_GET'
/usr/lib/ruby/gems/3.2.0/gems/webrick-1.8.1/lib/webrick/httpservlet/abstract.rb:105:in `service'
/usr/lib/ruby/gems/3.2.0/gems/webrick-1.8.1/lib/webrick/httpservlet/filehandler.rb:242:in `service'
/usr/lib/ruby/gems/3.2.0/gems/webrick-1.8.1/lib/webrick/httpserver.rb:140:in `service'
/usr/lib/ruby/gems/3.2.0/gems/webrick-1.8.1/lib/webrick/httpserver.rb:96:in `run'
/usr/lib/ruby/gems/3.2.0/gems/webrick-1.8.1/lib/webrick/server.rb:310:in `block in start_thread'
with
ruby --version
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux-musl]
May I suggest printing the url the user needs to open? It is more convenient, and less confusing for newer users.
BIND_ADDRESS = "127.0.0.1" # localhost
PORT = 8000
server = WEBrick::HTTPServer.new({:Port => PORT, :DocumentRoot => root, :BindAddress => BIND_ADDRESS})
puts "Serving on http://#{BIND_ADDRESS}:#{PORT}"
May I suggest printing the url the user needs to open? It is more convenient, and less confusing for newer users.
BIND_ADDRESS = "127.0.0.1" # localhost PORT = 8000 server = WEBrick::HTTPServer.new({:Port => PORT, :DocumentRoot => root, :BindAddress => BIND_ADDRESS}) puts "Serving on http://#{BIND_ADDRESS}:#{PORT}"
Done - thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dir.exists?
was deprecated in Ruby 2.1.0 and has been removed in the Ruby 3.2.0suggest change: