mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-02 10:55:30 -04:00
- when building for release, embed the static files in binary
This commit is contained in:
parent
0c177c3d31
commit
21fcde944d
@ -15,7 +15,7 @@ license: MIT
|
||||
dependencies:
|
||||
kemal:
|
||||
github: kemalcr/kemal
|
||||
kemal-basic-auth:
|
||||
github: kemalcr/kemal-basic-auth
|
||||
sqlite3:
|
||||
github: crystal-lang/crystal-sqlite3
|
||||
baked_file_system:
|
||||
github: schovi/baked_file_system
|
||||
|
@ -1,14 +1,6 @@
|
||||
require "kemal"
|
||||
require "./storage"
|
||||
|
||||
def request_path_startswith(env, ary)
|
||||
ary.each do |prefix|
|
||||
if env.request.path.starts_with? prefix
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
require "./util"
|
||||
|
||||
class AuthHandler < Kemal::Handler
|
||||
exclude ["/login"]
|
||||
|
@ -3,6 +3,7 @@ require "./config"
|
||||
require "./library"
|
||||
require "./storage"
|
||||
require "./auth_handler"
|
||||
require "./static"
|
||||
require "./util"
|
||||
|
||||
class Server
|
||||
@ -276,6 +277,11 @@ class Server
|
||||
end
|
||||
|
||||
add_handler AuthHandler.new @storage
|
||||
{% if flag?(:release) %}
|
||||
# when building for relase, embed the static files in binary
|
||||
serve_static false
|
||||
add_handler StaticHandler.new
|
||||
{% end %}
|
||||
end
|
||||
|
||||
def start
|
||||
|
29
src/static.cr
Normal file
29
src/static.cr
Normal file
@ -0,0 +1,29 @@
|
||||
require "baked_file_system"
|
||||
require "kemal"
|
||||
require "gzip"
|
||||
require "./util"
|
||||
|
||||
class FS
|
||||
extend BakedFileSystem
|
||||
bake_folder "../public"
|
||||
end
|
||||
|
||||
class StaticHandler < Kemal::Handler
|
||||
property dirs : Array(String)
|
||||
|
||||
def initialize
|
||||
@dirs = ["/css", "/js"]
|
||||
end
|
||||
|
||||
def call(env)
|
||||
if request_path_startswith env, @dirs
|
||||
file = FS.get? env.request.path
|
||||
return call_next env if file.nil?
|
||||
|
||||
slice = Bytes.new file.size
|
||||
file.read slice
|
||||
return send_file env, slice, file.mime_type
|
||||
end
|
||||
call_next env
|
||||
end
|
||||
end
|
@ -23,3 +23,12 @@ end
|
||||
def hash_to_query(hash)
|
||||
hash.map { |k, v| "#{k}=#{v}" }.join("&")
|
||||
end
|
||||
|
||||
def request_path_startswith(env, ary)
|
||||
ary.each do |prefix|
|
||||
if env.request.path.starts_with? prefix
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user