- group config/library/storage into context

This commit is contained in:
Alex Ling 2020-02-16 18:25:28 +00:00
parent d9129ad58f
commit bb936b1fa6
4 changed files with 47 additions and 34 deletions

17
src/context.cr Normal file
View File

@ -0,0 +1,17 @@
require "./config"
require "./library"
require "./storage"
require "logger"
class Context
property config : Config
property library : Library
property storage : Storage
def initialize
@config = Config.load
@library = Library.new @config.library_path, @config.scan_interval
@storage = Storage.new @config.db_path
end
end

View File

@ -1,11 +1,6 @@
require "./server" require "./server"
require "./config" require "./context"
require "./library"
require "./storage"
config = Config.load context = Context.new
library = Library.new config.library_path, config.scan_interval server = Server.new context
storage = Storage.new config.db_path
server = Server.new config, library, storage
server.start server.start

View File

@ -1,17 +1,13 @@
require "kemal" require "kemal"
require "./config" require "./context"
require "./library"
require "./storage"
require "./auth_handler" require "./auth_handler"
require "./static_handler" require "./static_handler"
require "./util" require "./util"
class Server class Server
property config : Config property context : Context
property library : Library
property storage : Storage
def initialize(@config, @library, @storage) def initialize(@context)
error 403 do |env| error 403 do |env|
message = "You are not authorized to visit #{env.request.path}" message = "You are not authorized to visit #{env.request.path}"
@ -20,7 +16,7 @@ class Server
get "/" do |env| get "/" do |env|
begin begin
titles = @library.titles titles = @context.library.titles
username = get_username env username = get_username env
percentage = titles.map &.load_percetage username percentage = titles.map &.load_percetage username
layout "index" layout "index"
@ -31,7 +27,8 @@ class Server
get "/book/:title" do |env| get "/book/:title" do |env|
begin begin
title = (@library.get_title env.params.url["title"]).not_nil! title = (@context.library.get_title env.params.url["title"])
.not_nil!
username = get_username env username = get_username env
percentage = title.entries.map { |e| percentage = title.entries.map { |e|
title.load_percetage username, e.title } title.load_percetage username, e.title }
@ -46,7 +43,7 @@ class Server
end end
get "/admin/user" do |env| get "/admin/user" do |env|
users = @storage.list_users users = @context.storage.list_users
username = get_username env username = get_username env
layout "user" layout "user"
end end
@ -86,7 +83,7 @@ class Server
raise "password should contain ASCII characters only" raise "password should contain ASCII characters only"
end end
@storage.new_user username, password, admin @context.storage.new_user username, password, admin
env.redirect "/admin/user" env.redirect "/admin/user"
rescue e rescue e
@ -125,7 +122,7 @@ class Server
end end
end end
@storage.update_user \ @context.storage.update_user \
original_username, username, password, admin original_username, username, password, admin
env.redirect "/admin/user" env.redirect "/admin/user"
@ -142,7 +139,8 @@ class Server
get "/reader/:title/:entry" do |env| get "/reader/:title/:entry" do |env|
begin begin
title = (@library.get_title env.params.url["title"]).not_nil! title = (@context.library.get_title env.params.url["title"])
.not_nil!
entry = (title.get_entry env.params.url["entry"]).not_nil! entry = (title.get_entry env.params.url["entry"]).not_nil!
# load progress # load progress
@ -161,7 +159,8 @@ class Server
get "/reader/:title/:entry/:page" do |env| get "/reader/:title/:entry/:page" do |env|
begin begin
title = (@library.get_title env.params.url["title"]).not_nil! title = (@context.library.get_title env.params.url["title"])
.not_nil!
entry = (title.get_entry env.params.url["entry"]).not_nil! entry = (title.get_entry env.params.url["entry"]).not_nil!
page = env.params.url["page"].to_i page = env.params.url["page"].to_i
raise "" if page > entry.pages || page <= 0 raise "" if page > entry.pages || page <= 0
@ -197,7 +196,7 @@ class Server
begin begin
cookie = env.request.cookies cookie = env.request.cookies
.find { |c| c.name == "token" }.not_nil! .find { |c| c.name == "token" }.not_nil!
@storage.logout cookie.value @context.storage.logout cookie.value
rescue rescue
ensure ensure
env.redirect "/login" env.redirect "/login"
@ -208,7 +207,8 @@ class Server
begin begin
username = env.params.body["username"] username = env.params.body["username"]
password = env.params.body["password"] password = env.params.body["password"]
token = @storage.verify_user(username, password).not_nil! token = @context.storage.verify_user(username, password)
.not_nil!
cookie = HTTP::Cookie.new "token", token cookie = HTTP::Cookie.new "token", token
env.response.cookies << cookie env.response.cookies << cookie
@ -224,7 +224,7 @@ class Server
entry = env.params.url["entry"] entry = env.params.url["entry"]
page = env.params.url["page"].to_i page = env.params.url["page"].to_i
t = @library.get_title title t = @context.library.get_title title
raise "Title `#{title}` not found" if t.nil? raise "Title `#{title}` not found" if t.nil?
e = t.get_entry entry e = t.get_entry entry
raise "Entry `#{entry}` of `#{title}` not found" if e.nil? raise "Entry `#{entry}` of `#{title}` not found" if e.nil?
@ -244,7 +244,7 @@ class Server
begin begin
title = env.params.url["title"] title = env.params.url["title"]
t = @library.get_title title t = @context.library.get_title title
raise "Title `#{title}` not found" if t.nil? raise "Title `#{title}` not found" if t.nil?
send_json env, t.to_json send_json env, t.to_json
@ -256,23 +256,23 @@ class Server
end end
get "/api/book" do |env| get "/api/book" do |env|
send_json env, @library.to_json send_json env, @context.library.to_json
end end
post "/api/admin/scan" do |env| post "/api/admin/scan" do |env|
start = Time.utc start = Time.utc
@library.scan @context.library.scan
ms = (Time.utc - start).total_milliseconds ms = (Time.utc - start).total_milliseconds
send_json env, { send_json env, {
"milliseconds" => ms, "milliseconds" => ms,
"titles" => @library.titles.size "titles" => @context.library.titles.size
}.to_json }.to_json
end end
post "/api/admin/user/delete/:username" do |env| post "/api/admin/user/delete/:username" do |env|
begin begin
username = env.params.url["username"] username = env.params.url["username"]
@storage.delete_user username @context.storage.delete_user username
rescue e rescue e
send_json env, { send_json env, {
"success" => false, "success" => false,
@ -286,7 +286,8 @@ class Server
post "/api/progress/:title/:entry/:page" do |env| post "/api/progress/:title/:entry/:page" do |env|
begin begin
username = get_username env username = get_username env
title = (@library.get_title env.params.url["title"]).not_nil! title = (@context.library.get_title env.params.url["title"])
.not_nil!
entry = (title.get_entry env.params.url["entry"]).not_nil! entry = (title.get_entry env.params.url["entry"]).not_nil!
page = env.params.url["page"].to_i page = env.params.url["page"].to_i
@ -302,7 +303,7 @@ class Server
end end
end end
add_handler AuthHandler.new @storage add_handler AuthHandler.new @context.storage
{% if flag?(:release) %} {% if flag?(:release) %}
# when building for relase, embed the static files in binary # when building for relase, embed the static files in binary
serve_static false serve_static false
@ -314,7 +315,7 @@ class Server
{% if flag?(:release) %} {% if flag?(:release) %}
Kemal.config.env = "production" Kemal.config.env = "production"
{% end %} {% end %}
Kemal.config.port = @config.port Kemal.config.port = @context.config.port
Kemal.run Kemal.run
end end
end end

View File

@ -12,7 +12,7 @@ macro get_username(env)
# if the request gets here, its has gone through the auth handler, and # if the request gets here, its has gone through the auth handler, and
# we can be sure that a valid token exists, so we can use not_nil! here # we can be sure that a valid token exists, so we can use not_nil! here
cookie = {{env}}.request.cookies.find { |c| c.name == "token" }.not_nil! cookie = {{env}}.request.cookies.find { |c| c.name == "token" }.not_nil!
(storage.verify_token cookie.value).not_nil! (@context.storage.verify_token cookie.value).not_nil!
end end
macro send_json(env, json) macro send_json(env, json)