Bonsai.jl

A minimal HTTP framework
Author onetonfoot
Popularity
0 Stars
Updated Last
4 Months Ago
Started In
October 2019

Bonsai

Build Status Coverage Status

Getting started

Create a instance of the app, add a route then starting the server

app = App()

app("/path"; method = GET) do request
    reponse = "hello"
    return response
end

start(app)

Now vist localhost:8081.

The handler function should take a HTTP.Request and return a HTTP.Response. If the return type isn't a Response then it will be made into one by create_response, you can overload this function for any type from more control.

Routing and Query Parameters

Routes can be defined for multiple methods by passing an array

app(handler, "/path"; method = [GET, POST])

Routes can be parametrized by using the syntax /:x. You can then access the parameters in the handler with.

app("/route/:id", method = [GET, POST]) do request
    params = path_params(request)
    # Dict(:id => value)
end

Specific routes will be matched first, in other words "/route/1" will be matched before "/route/:id". Query parameters can be accessed in a similar way.

query = query_params(request)

Specific routes like /path take priority over variable routes such as /:path.

Serving Files

Files

To serve a single file from "/index.html".

app(p"index.html")

By default the file will be served from the index, but you can specify a route with.

app("/page", p"page.html")

The MIME type will be infered from the extension, the supported MIME types can be found here. If the MIME type is unsuported it can be added to the global MIME_TYPES.

Folders

app("/img", f"images")

Create Response

After your handler is called if it's return type isn't a Response it will be made into one by calling create_response. You can overload create_reponse for your specific type to have more control.

create_response(t::Mytype)::HTTP.Response

This is already defined for AbstractString and AbstractDict, were the Content-Type header is set as text/plain and application/json respectively.

Sessions

When instantiating the app you can pass any data structure you want to use for the session, by default this will be a Dict. The session can then be accessed in the handler.

app("/path") do req
    session = app.session
end

Note Dict isn't thread safe so this will likey change in the future to a differnt data structure.

Examples

For more examples see the examples folder.

TODO

Docker

Middleware

WebSockets

Useful Packages

Some packages that you may find useful to pair wtih Bonsai.

Used By Packages

No packages found.