Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create src/folder.jl to handle serving of Folder() #280

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Bonito.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ include("deno.jl")
include("types.jl")
include("HTTPServer/HTTPServer.jl")
include("app.jl")
include("folder.jl")

function HTTPServer.route!(server::HTTPServer.Server, routes::Routes)
for (key, app) in routes.routes
Expand Down
24 changes: 24 additions & 0 deletions src/folder.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function HTTPServer.apply_handler(folder::Folder, context)
target = context.request.target[2:end] # first should be '/' which we want to remove for a path
path = abspath(joinpath(folder.path, target))
if isfile(path)
_, ext = splitext(path)
body = read(path)
# TODO: use HTTPServer.file_mimetype
if ext == ".html"
# Bonito.html is just a simple helper to return a HTTP html response
return Bonito.html(body)
elseif ext == ".css"
return HTTP.Response(200, ["Content-Type" => "text/css", "charset" => "utf-8"], body=body)
elseif ext == ".js"
return HTTP.Response(200, ["Content-Type" => "text/javascript", "charset" => "utf-8"],
body=body)
else
@warn "Cannot deal with $ext"
return Bonito.HTTPServer.response_404() # not appropriate! I don't know what to do here.
end
else
@warn "Did not find $target ($path), returning 404"
return Bonito.HTTPServer.response_404()
end
end
4 changes: 4 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ struct Asset <: AbstractAsset
end


struct Folder
path::String
end

struct Link <: AbstractAsset
target::String
end
Expand Down