mirror of
https://github.com/hkalexling/Mango.git
synced 2026-04-03 00:03:14 -04:00
- implement login and load images from zip
This commit is contained in:
42
src/mango.cr
42
src/mango.cr
@@ -5,21 +5,47 @@ require "./storage"
|
||||
require "./auth_handler"
|
||||
|
||||
config = Config.load
|
||||
|
||||
library = Library.new config.library_path
|
||||
|
||||
storage = Storage.new config.db_path
|
||||
|
||||
get "/" do
|
||||
"Hello World!"
|
||||
|
||||
macro layout(name)
|
||||
render "src/views/#{{{name}}}.ecr", "src/views/layout.ecr"
|
||||
end
|
||||
|
||||
# APIs
|
||||
get "/api/test" do |env|
|
||||
"Hello!"
|
||||
macro send_img(env, img)
|
||||
send_file env, image.data, image.mime
|
||||
end
|
||||
|
||||
add_handler AuthHandler.new
|
||||
|
||||
get "/" do |env|
|
||||
image = library.titles[0].get_cover
|
||||
unless image
|
||||
"Failed to load image"
|
||||
next
|
||||
end
|
||||
send_img env, image
|
||||
end
|
||||
|
||||
get "/login" do |env|
|
||||
render "src/views/login.ecr"
|
||||
end
|
||||
|
||||
post "/login" do |env|
|
||||
username = env.params.body["username"]
|
||||
password = env.params.body["password"]
|
||||
token = storage.verify_user username, password
|
||||
if token.nil?
|
||||
env.redirect "/login"
|
||||
next
|
||||
end
|
||||
|
||||
cookie = HTTP::Cookie.new "token", token
|
||||
env.response.cookies << cookie
|
||||
env.redirect "/"
|
||||
end
|
||||
|
||||
add_handler AuthHandler.new storage
|
||||
|
||||
Kemal.config.port = config.port
|
||||
Kemal.run
|
||||
|
||||
Reference in New Issue
Block a user