- / and /book/:title

This commit is contained in:
Alex Ling
2020-02-12 17:52:33 +00:00
parent cc031c9aef
commit 9eedac280e
6 changed files with 118 additions and 17 deletions

View File

@@ -14,17 +14,22 @@ macro layout(name)
end
macro send_img(env, img)
send_file env, image.data, image.mime
send_file {{env}}, {{img}}.data, {{img}}.mime
end
get "/" do |env|
image = library.titles[0].get_cover
unless image
"Failed to load image"
titles = library.titles
layout "index"
end
get "/book/:title" do |env|
title = library.get_title env.params.url["title"]
if title.nil?
env.response.status_code = 404
next
end
send_img env, image
layout "title"
end
get "/login" do |env|
@@ -45,6 +50,49 @@ post "/login" do |env|
env.redirect "/"
end
get "/api/page/:title/:entry/:page" do |env|
begin
title = env.params.url["title"]
entry = env.params.url["entry"]
page = env.params.url["page"].to_i
t = library.get_title title
raise "Title `#{title}` not found" if t.nil?
e = t.get_entry entry
raise "Entry `#{entry}` of `#{title}` not found" if e.nil?
img = e.read_page page
raise "Failed to load page #{page} of `#{title}/#{entry}`" if img.nil?
send_img env, img
rescue e
STDERR.puts e
env.response.status_code = 500
e.message
end
end
get "/api/book/:title" do |env|
begin
title = env.params.url["title"]
t = library.get_title title
raise "Title `#{title}` not found" if t.nil?
env.response.content_type = "application/json"
t.to_json
rescue e
STDERR.puts e
env.response.status_code = 500
e.message
end
end
get "/api/book" do |env|
env.response.content_type = "application/json"
library.to_json
end
add_handler AuthHandler.new storage
Kemal.config.port = config.port