# Combining baked\_file\_system with a HTTP server (serving files from an archived, virtual, file system)

**URL:** <https://forum.crystal-lang.org/t/combining-baked-file-system-with-a-http-server-serving-files-from-an-archived-virtual-file-system/5416>\
**Category:** News\
**Created:** [February 23, 2023, 10:34am UTC](https://forum.crystal-lang.org/t/combining-baked-file-system-with-a-http-server-serving-files-from-an-archived-virtual-file-system/5416 "2023-02-23T10:34:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [February 23, 2023, 10:34am UTC](https://forum.crystal-lang.org/t/combining-baked-file-system-with-a-http-server-serving-files-from-an-archived-virtual-file-system/5416/1 "2023-02-23T10:34:48Z")

</div>

I put the complete code at :

> **[GitHub - serge-hulne/crystal\_serve\_baked: Server serving files archived with...](https://github.com/serge-hulne/crystal_serve_baked)**
>
> Server serving files archived with 'baked\_file\_system' - GitHub - serge-hulne/crystal\_serve\_baked: Server serving files archived with 'baked\_file\_system'

Here is a solution which works on macOS (but not yet on Windows)

```auto
require "baked_file_system"
require "http/server"

HOST = "127.0.0.1"
PORT = 8080

# URL to archive path mapping
def get_file_from_url?(url : String) : String 
  if url == "/" || url == ""
    url = "/index.html"
  end
  path = url
  file = ""
  begin
    file = FileStorage.get path
    return file.gets_to_end
  rescue BakedFileSystem::NoSuchFileError
    puts "File #{path} is missing"
    return "Path: #{path} not found!"
  end   
end

# Archive
class FileStorage
  extend BakedFileSystem
  bake_folder "../public"
end

# Server using URL to archive path mapping
server = HTTP::Server.new do |context|
  context.response.print get_file_from_url?(context.request.path)
end

# Running server
puts "Listening on http://#{HOST}:#{PORT}"
server.bind_tcp HOST , PORT
server.listen

```
