mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 10:55:30 -04:00
Allow CORS
This commit is contained in:
parent
703e6d076b
commit
2091053221
@ -59,6 +59,10 @@ class AuthHandler < Kemal::Handler
|
|||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
|
# OPTIONS requests do not require authentication
|
||||||
|
if env.request.method === "OPTIONS"
|
||||||
|
return call_next(env)
|
||||||
|
end
|
||||||
# Skip all authentication if requesting /login, /logout, /api/login,
|
# Skip all authentication if requesting /login, /logout, /api/login,
|
||||||
# or a static file
|
# or a static file
|
||||||
if request_path_startswith(env, ["/login", "/logout", "/api/login"]) ||
|
if request_path_startswith(env, ["/login", "/logout", "/api/login"]) ||
|
||||||
|
8
src/handlers/cors_handler.cr
Normal file
8
src/handlers/cors_handler.cr
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
class CORSHandler < Kemal::Handler
|
||||||
|
def call(env)
|
||||||
|
if request_path_startswith env, ["/api"]
|
||||||
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||||
|
end
|
||||||
|
call_next env
|
||||||
|
end
|
||||||
|
end
|
@ -63,6 +63,11 @@ struct APIRouter
|
|||||||
"username" => String,
|
"username" => String,
|
||||||
"password" => String,
|
"password" => String,
|
||||||
}
|
}
|
||||||
|
Koa.response 200, schema: {
|
||||||
|
"success" => Bool,
|
||||||
|
"error" => String?,
|
||||||
|
"token" => String?,
|
||||||
|
}
|
||||||
Koa.tag "users"
|
Koa.tag "users"
|
||||||
post "/api/login" do |env|
|
post "/api/login" do |env|
|
||||||
begin
|
begin
|
||||||
@ -71,11 +76,17 @@ struct APIRouter
|
|||||||
token = Storage.default.verify_user(username, password).not_nil!
|
token = Storage.default.verify_user(username, password).not_nil!
|
||||||
|
|
||||||
env.session.string "token", token
|
env.session.string "token", token
|
||||||
"Authenticated"
|
send_json env, {
|
||||||
|
"success" => true,
|
||||||
|
"token" => token,
|
||||||
|
}.to_json
|
||||||
rescue e
|
rescue e
|
||||||
Logger.error e
|
Logger.error e
|
||||||
env.response.status_code = 403
|
env.response.status_code = 403
|
||||||
e.message
|
send_json env, {
|
||||||
|
"success" => false,
|
||||||
|
"error" => e.message,
|
||||||
|
}.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -114,7 +125,7 @@ struct APIRouter
|
|||||||
rescue e
|
rescue e
|
||||||
Logger.error e
|
Logger.error e
|
||||||
env.response.status_code = 500
|
env.response.status_code = 500
|
||||||
e.message
|
send_text env, e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -151,7 +162,7 @@ struct APIRouter
|
|||||||
rescue e
|
rescue e
|
||||||
Logger.error e
|
Logger.error e
|
||||||
env.response.status_code = 500
|
env.response.status_code = 500
|
||||||
e.message
|
send_text env, e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -191,7 +202,7 @@ struct APIRouter
|
|||||||
rescue e
|
rescue e
|
||||||
Logger.error e
|
Logger.error e
|
||||||
env.response.status_code = 404
|
env.response.status_code = 404
|
||||||
e.message
|
send_text env, e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -250,6 +261,7 @@ struct APIRouter
|
|||||||
spawn do
|
spawn do
|
||||||
Library.default.generate_thumbnails
|
Library.default.generate_thumbnails
|
||||||
end
|
end
|
||||||
|
send_text env, ""
|
||||||
end
|
end
|
||||||
|
|
||||||
Koa.describe "Deletes a user with `username`"
|
Koa.describe "Deletes a user with `username`"
|
||||||
@ -675,7 +687,7 @@ struct APIRouter
|
|||||||
e_tag = "W/#{file_hash}"
|
e_tag = "W/#{file_hash}"
|
||||||
if e_tag == prev_e_tag
|
if e_tag == prev_e_tag
|
||||||
env.response.status_code = 304
|
env.response.status_code = 304
|
||||||
""
|
send_text env, ""
|
||||||
else
|
else
|
||||||
sizes = entry.page_dimensions
|
sizes = entry.page_dimensions
|
||||||
env.response.headers["ETag"] = e_tag
|
env.response.headers["ETag"] = e_tag
|
||||||
@ -709,6 +721,7 @@ struct APIRouter
|
|||||||
rescue e
|
rescue e
|
||||||
Logger.error e
|
Logger.error e
|
||||||
env.response.status_code = 404
|
env.response.status_code = 404
|
||||||
|
send_text env, e.message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,7 +23,11 @@ class Server
|
|||||||
AdminRouter.new
|
AdminRouter.new
|
||||||
ReaderRouter.new
|
ReaderRouter.new
|
||||||
APIRouter.new
|
APIRouter.new
|
||||||
OPDSRouter.new
|
|
||||||
|
options "/api/*" do |env|
|
||||||
|
cors
|
||||||
|
halt env
|
||||||
|
end
|
||||||
|
|
||||||
Kemal.config.logging = false
|
Kemal.config.logging = false
|
||||||
add_handler LogHandler.new
|
add_handler LogHandler.new
|
||||||
|
@ -39,6 +39,7 @@ macro send_error_page(msg)
|
|||||||
end
|
end
|
||||||
|
|
||||||
macro send_img(env, img)
|
macro send_img(env, img)
|
||||||
|
cors
|
||||||
send_file {{env}}, {{img}}.data, {{img}}.mime
|
send_file {{env}}, {{img}}.data, {{img}}.mime
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,12 +58,26 @@ macro get_username(env)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
macro cors
|
||||||
|
env.response.headers["Allow"] = "HEAD,GET,PUT,POST,DELETE,OPTIONS"
|
||||||
|
env.response.headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept, Authorization"
|
||||||
|
env.response.headers["Access-Control-Allow-Origin"] = "*"
|
||||||
|
end
|
||||||
|
|
||||||
def send_json(env, json)
|
def send_json(env, json)
|
||||||
|
cors
|
||||||
env.response.content_type = "application/json"
|
env.response.content_type = "application/json"
|
||||||
env.response.print json
|
env.response.print json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def send_text(env, text)
|
||||||
|
cors
|
||||||
|
env.response.content_type = "text/plain"
|
||||||
|
env.response.print text
|
||||||
|
end
|
||||||
|
|
||||||
def send_attachment(env, path)
|
def send_attachment(env, path)
|
||||||
|
cors
|
||||||
send_file env, path, filename: File.basename(path), disposition: "attachment"
|
send_file env, path, filename: File.basename(path), disposition: "attachment"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user