diff --git a/.gitignore b/.gitignore index fe776d0..c85cbe8 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # rspec failure tracking .rspec_status pub_gemini +config.yml diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..340498f --- /dev/null +++ b/config.example.yml @@ -0,0 +1,3 @@ +skeksis_config: + gemini_uri: "gemini://gemini.example.com/" + serve_dir: "/var/gemini" diff --git a/config.ru b/config.ru index 63d9964..9c47a40 100644 --- a/config.ru +++ b/config.ru @@ -1,7 +1,5 @@ require 'skeksis' -SERVE_DIR="/Users/madeline/Code/gemini-bridge-rack/gemini" - class SkeksisApp def initialize @skeksis = Skeksis::GemBridge.new @@ -10,7 +8,7 @@ class SkeksisApp def call(env) status = 200 headers = { "content-type" => "text/html" } - body = @skeksis.query(SERVE_DIR + env['PATH_INFO'], env) + body = @skeksis.query(env['PATH_INFO'], env) [status, headers, body] end diff --git a/config.yml b/config.yml deleted file mode 100644 index 8ad659f..0000000 --- a/config.yml +++ /dev/null @@ -1,2 +0,0 @@ -skeksis_config: - gemini_uri: "gemini://gemini.hackers.town/" diff --git a/lib/skeksis.rb b/lib/skeksis.rb index 38e37e4..b1fb2e1 100644 --- a/lib/skeksis.rb +++ b/lib/skeksis.rb @@ -24,15 +24,25 @@ module Skeksis class GemBridge def initialize - @gemini_uri = YAML.load(File.read("config.yml"))['skeksis_config']['gemini_uri'] + config = YAML.load(File.read("config.yml")) + @gemini_uri = config['skeksis_config']['gemini_uri'] + @serve_dir = config['skeksis_config']['serve_dir'] end def query(path, env) - if Dir.exist?(path) - #return Dir.each_child(path).map {|i| "#{i}\n"} - return create_dir_listing(path, env) - elsif File.exist?(path) - file = File.open(path, 'r') + # Chomps the first / and builds path + full_path = Pathname.new(@serve_dir) + path[1..-1] + puts full_path.to_s + if Dir.exist?(full_path) + index_file = full_path + "index.gmi" + if File.exist?(index_file) + data = File.open(index_file, 'r').readlines + [htmlize(data, env)] + else + create_dir_listing(full_path, env) + end + elsif File.exist?(full_path) + file = File.open(full_path, 'r') data = file.readlines [htmlize(data, env)] else # path is invalid diff --git a/lib/skeksis/htmlize.rb b/lib/skeksis/htmlize.rb index 4a035eb..af69810 100644 --- a/lib/skeksis/htmlize.rb +++ b/lib/skeksis/htmlize.rb @@ -60,13 +60,21 @@ module Skeksis html end + # TODO: Relative and absolute links need to be properly handled, both proxied and non def build_uri(link) uri = URI.parse(link) + if uri.relative? then puts "relative" else puts "absolute" end + + if uri.relative? + path = Pathname.new(@http.path).join(uri.path) + return URI::HTTP.build(host: @http.host, path: path.to_s, port: @http.port) + end + if uri.host == @proxied_uri.host - URI::HTTP.build(host: @http.host, path: uri.path, port: @http.port) + return URI::HTTP.build(host: @http.host, path: uri.path, port: @http.port) else - link + return link end end end