bliss
Types
Everything is a handler in Bliss A handler is a function that takes a WebRequest and a context and returns a WebResponse A context is your own type, which could hold any data e.g. current_user, db_pool
pub type Handler(ctx) =
fn(WebRequest, ctx) -> WebResponse
A middleware is a function that wraps a handler Middlewares can change the request in the way in and the response in the way out
pub type Middleware(ctx_in, ctx_out) =
fn(Handler(ctx_out)) -> Handler(ctx_in)
Standard response errors Use Unmatched to signal that the current path hasn’t match
pub type ResponseError {
Unmatched
NotFound
Unauthorised
}
Constructors
-
Unmatched
-
NotFound
-
Unauthorised
A Request
pub type WebRequest =
request.Request(BitString)
Expected response by handlers
pub type WebResponse =
Result(Response(BitBuilder), ResponseError)
Constants
pub const header_content_type: String = "Content-Type"
Functions
pub fn if_delete(handler: fn(Request(BitString), a) ->
Result(Response(BitBuilder), ResponseError)) -> fn(
Request(BitString),
a,
) -> Result(Response(BitBuilder), ResponseError)
A middleware that only calls the wrapped handler if the request uses Delete
pub fn if_get(handler: fn(Request(BitString), a) ->
Result(Response(BitBuilder), ResponseError)) -> fn(
Request(BitString),
a,
) -> Result(Response(BitBuilder), ResponseError)
A middleware that only calls the wrapped handler if the request uses Get
pub fn if_methods(wanted_methods: List(Method)) -> fn(
fn(Request(BitString), a) ->
Result(Response(BitBuilder), ResponseError),
) ->
fn(Request(BitString), a) ->
Result(Response(BitBuilder), ResponseError)
Basic Middlewares
A middleware that only calls the wrapped handler if the request uses the allowed methods
["users", id] -> delete_user(id) |> bliss.if_methods([Delete])
pub fn if_post(handler: fn(Request(BitString), a) ->
Result(Response(BitBuilder), ResponseError)) -> fn(
Request(BitString),
a,
) -> Result(Response(BitBuilder), ResponseError)
A middleware that only calls the wrapped handler if the request uses Post
pub fn json_response(data: Json) -> Response(BitBuilder)
Common responses
Create a JSON response
pub fn not_found(_req: Request(BitString), _ctx: a) -> Result(
Response(BitBuilder),
ResponseError,
)
Basic handlers
A handler that always responds with not found
pub fn service(handler: fn(Request(BitString), a) ->
Result(Response(BitBuilder), ResponseError), context context: a) -> fn(
Request(BitString),
) -> Response(BitBuilder)
Create the web service Pass this to a web server
pub fn unmatched(_req: Request(BitString), _ctx: a) -> Result(
Response(BitBuilder),
ResponseError,
)
A handler that returns Unmatched Use the to indicate that the framework should keep trying to match a route
case path {
["users"] -> ...
_ -> bliss.unmatched
}
pub fn use_handler(provide: fn(Request(BitString), a) ->
fn(Request(BitString), a) ->
Result(Response(BitBuilder), ResponseError)) -> fn(
Request(BitString),
a,
) -> Result(Response(BitBuilder), ResponseError)
Take the request and context and return a handler to call Use this for routing
bliss.user_hander(fn(req, ctx) {
case request.path_segments(req) {
["api", ..rest] -> api_routes(rest)
rest -> public_routes(rest)
}
})