Is there an alternative to "Rucksack" to pack a directory's content into a virtual file system?

1 Like

Problem solved: The Rucksack developers have provided me with the solution (a big thank to them):

require "http/server"
require "rucksack"
require "mime"

server = HTTP::Server.new do |context|
  path = context.request.path
  path = "/index.html" if path == "/"
  path = "./webroot#{path}"

  begin
    # Here we read the requested file from the Rucksack
    # and write it to the HTTP response. By default Rucksack
    # falls back to direct filesystem access in case the
    # executable has no Rucksack attached.
    context.response.content_type = MIME.from_filename(path)
    rucksack(path).read(context.response.output)
  rescue Rucksack::FileNotFound
    context.response.status = HTTP::Status.new(404)
    context.response.print "404 not found :("
  end
end

address = server.bind_tcp 8080
puts "Listening on http://#{address}"
server.listen

# Here we statically reference the files to be included
# once - otherwise Rucksack wouldn't know what to pack.
{% for name in `find ./webroot -type f`.split('\n') %}
  rucksack({{name}})
{% end %}

For the record, a popular alternative is GitHub - schovi/baked_file_system: Virtual File System for Crystal language. Embedding your assets into final binary.
It’s a bit easier to use than rucksack. The architecture of rucksack would certainly be more beneficial for huge files.

1 Like

Thank you very much!