Skip to content

Commit

Permalink
- when building for release, embed the static files in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
hkalexling committed Feb 15, 2020
1 parent 0c177c3 commit 21fcde9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
Binary file modified mango
Binary file not shown.
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 1 addition & 9 deletions src/auth_handler.cr
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
6 changes: 6 additions & 0 deletions src/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "./config"
require "./library"
require "./storage"
require "./auth_handler"
require "./static"
require "./util"

class Server
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/static.cr
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions src/util.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 21fcde9

Please sign in to comment.