Skip to content

Instantly share code, notes, and snippets.

@xmonader

xmonader/a.cr Secret

Created April 24, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmonader/e791491b3e67d9fbd62e383cc2bed282 to your computer and use it in GitHub Desktop.
Save xmonader/e791491b3e67d9fbd62e383cc2bed282 to your computer and use it in GitHub Desktop.
# Matches /hello/kemal
require "kemal"
module TfWiki
module WikiServer
awalker = TfWiki::Walker.new
awikipath = ""
def self.walker
# p awalker
awalker
end
def self.wikipath
awikipath
end
def self.send_from_dirsinfo(env, filename)
if self.walker.dirsfilesinfo.has_key?(filename)
firstpath = self.walker.dirsfilesinfo[filename].paths[0] # in decent repo it will be only 1 in this array.
send_file env, firstpath
env.response.status_code = 404
env.response.print "file #{filename} doesn't exist in scanned info."
env.response.close
end
end
get "/wiki/:filename" do |env|
puts "Got request.."
puts env.request.resource
filename = env.params.url["filename"]
unless filename.ends_with?(".md")
filename = filename + ".md"
end
self.send_from_dirsinfo(env, filename)
end
get "/:filepath" do |env|
puts "/:file path request "
puts env.request.resource
filepath = env.params.url["filepath"]
filename = File.basename(filepath)
unless File.exists?(filepath)
env.response.status_code = 404
env.response.print "file #{filepath} doesn't exist."
env.response.close
end
self.send_from_dirsinfo(env, filename)
end
def self.setup(wikipath : String, walker : TfWiki::Walker)
awikipath = wikipath
awalker = walker
puts "created server.."
end
def self.serve
puts "Starting kemal server"
Kemal.run
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment