Bonito: serving an HTML page

In Bonito.jl, how should I serve an HTML file? I would have expected

    server = Server("0.0.0.0", 8080)
    route!(server, "/" => title_page)
    route!(server, "/documentation" => HTMLPage("/local/path/to/my/page.html")

but looking at the docs I couldn’t find anything. I guess I can create an App which opens the file and returns its content inside an HTML() but that’s very roundabout.

2 Likes

Hm, that hasn’t been a use case so far, so I guess your workaround is the best option.
I guess one could implement route!(server, "/" => Asset("../test.html")), by overloading:

function HTTPServer.apply_handler(app::Asset, context)
   server = context.application
   request =  context.request
  ...
end
1 Like

Thank you for the suggestion! I tried my hand at HTML(read(my_file, String)), but it’s turning out more complicated than I thought. I’m trying to serve Documenter.jl documentation output. I can serve the .html file I care about fine, but then its styling is all wrong, because I’m not also serving the Documenter.jl-generated files in /docs/build/assets. Maybe I could serve those as Asset? It’s a bit painful, but it looks like the simplest solution.

Otherwise, I guess I could set up an Apache Server to serve the /docs/build repository on a different port, and link to those pages. Or maybe go with Oxygen.jl? I don’t have any experience there, so any advice is appreciated.

LiveServer.jl is kinda neat and solves my problem in the short term, with a localhost link. Not sure it’ll work out once I’m on a server, but it’ll be worth a try!

Otherwise, I guess I could set up an Apache Server to serve the /docs/build repository

You should be able to also serve a whole folder with Bonito.
Here is a prototype:

using Bonito

struct Folder
    path::String
end

function Bonito.HTTPServer.apply_handler(folder::Folder, context)
    target = context.request.target[2:end] # first should be '/' which we want to remove for a path
    # Bonito.html is just a simple helper to return a HTTP html response
    return Bonito.html(read(joinpath(folder.path, target)))
end

server = Server("0.0.0.0", 8080)
route!(server, r"/.*" => Folder(pwd()))

If you get it to work, a PR would be nice, since I guess others may want to have this functionality :wink:

1 Like