Skip to content

Commit

Permalink
embed assets into binary
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Oct 31, 2023
1 parent 1e9701d commit 0397df7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ shards:
git: https://github.com/sija/backtracer.cr.git
version: 1.2.2

baked_file_system:
git: https://github.com/schovi/baked_file_system.git
version: 0.10.0

crystal_mpd:
git: https://github.com/mamantoha/crystal_mpd.git
version: 0.15.1+git.commit.390d23b4d5535389c872fdc827a5a8c09f3f050a
Expand Down
2 changes: 2 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ dependencies:
crystal_mpd:
github: mamantoha/crystal_mpd
branch: master
baked_file_system:
github: schovi/baked_file_system

targets:
crympd:
Expand Down
12 changes: 10 additions & 2 deletions src/crympd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require "json"
require "kemal"
require "crystal_mpd"
require "./mpd_client"
require "./filesystem"

macro assets_version
{{ `git rev-parse --short HEAD || echo -n "unknown"`.chomp.stringify }}
Expand All @@ -15,6 +16,12 @@ before_get ["/current_song", "/status", "/stats", "/playlist"] do |env|
env.response.content_type = "application/json"
end

Filesystem.files.each do |file|
get(file.path) do |env|
Filesystem.serve(file, env)
end
end

get "/" do
render "views/index.ecr"
end
Expand Down Expand Up @@ -83,10 +90,11 @@ get "/albumart" do |env|
send_file env, binary.to_slice, data["type"]
end
else
send_file env, "./public/images/record_placeholder.jpg", "image/jpeg"

send_file env, Filesystem.get("images/record_placeholder.jpg").gets_to_end.to_slice, "image/jpeg"
end
rescue
send_file env, "./public/images/record_placeholder.jpg", "image/jpeg"
send_file env, Filesystem.get("images/record_placeholder.jpg").gets_to_end.to_slice, "image/jpeg"
end

ws "/mpd" do |socket|
Expand Down
21 changes: 21 additions & 0 deletions src/filesystem.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "baked_file_system"

module Filesystem
BakedFileSystem.load("../public", __DIR__)

def self.serve(file, ctx)
req = ctx.request
resp = ctx.response
resp.status_code = 200
resp.content_type = MIME.from_filename(file.path)

if req.headers["Accept-Encoding"]? =~ /gzip/
resp.headers["Content-Encoding"] = "gzip"
resp.content_length = file.compressed_size
file.write_to_io(resp, compressed: true)
else
resp.content_length = file.size
file.write_to_io(resp, compressed: false)
end
end
end

0 comments on commit 0397df7

Please sign in to comment.