-
Notifications
You must be signed in to change notification settings - Fork 3
/
tw5-server.rb
61 lines (51 loc) · 1.54 KB
/
tw5-server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# TW5 SERVER
# Allows editing and saving of TiddlyWiki in a browser.
#
# USAGE
# Download TiddlyWiki from https://tiddlywiki.com/empty.html and
# save it in its own subfolder as an .html file but NOT index.html.
#
# From the command line (e.g. Terminal on Mac):
# /usr/bin/wget https://tiddlywiki.com/empty.html -P folder/
# /usr/bin/ruby tw5-server.rb folder/empty.html
#
# Originally from:
# https://gist.github.com/jimfoltz/ee791c1bdd30ce137bc23cce826096da
# https://github.com/brianemery/tw5_server
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}".sub(/[\/]$/,'')
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, :BindAddress => "127.0.0.1"})
trap "INT" do
puts "Shutting down..."
server.shutdown
end
server.start