glace

Types

A glace request that’s a wrapper over gleam/http/request

pub type Request(context) =
  glace_types.Request(context)

A glace response that’s a wrapper over gleam/http/response and mist.ResponseData

pub type Response =
  response.Response(mist.ResponseData)

Functions

pub fn configure_logger(
  builder: GlaceBuilder(a),
  info info: fn(String) -> Nil,
  debug debug: fn(String) -> Nil,
  warn warn: fn(String) -> Nil,
  error error: fn(String) -> Nil,
) -> GlaceBuilder(a)

Configures a custom logger and overrides the default logger

Usage:


glace.new()
|> glace.configure_logger(info, debug, warn, error)
|> glace.get("/", fn(req) { handle_request(req) })
|> glace.start()

fn info(message: String) {
  io.println("Info: " <> message) // This can write to any other destination using any logging library
  Nil
}

fn debug(message: String) {
  io.println("Debug: " <> message)
  Nil
}

fn warn(message: String) {
  io.println("Warning: " <> message)
  Nil
}

fn error(message: String) {
  io.println("Error: " <> message)
  Nil
}

fn handle_request(req: Request) -> Response {
  req.logger.info("Received request: " <> req.path)
  c.text("Connected to db", 200)
}
 
pub fn get(
  builder: GlaceBuilder(a),
  path: String,
  handler: fn(Request(a)) -> Response(ResponseData),
) -> GlaceBuilder(a)

Registers a GET request for the specified path

Usage:


glace.new()
|> glace.get("/", fn(_) { glace.text("Hello, World!") })
|> glace.start()

pub fn html(
  body input: String,
  status status: Int,
) -> Response(ResponseData)

Helper function to return html response for a request

Usage:


glace.new()
|> glace.get("/", fn(_) { glace.html("Hello, World!") })
|> glace.start()

pub fn json_string(
  body input: String,
  status status: Int,
) -> Response(ResponseData)

Helper function to return json response for a request

Usage:


glace.new()
|> glace.get("/", fn(_) { glace.json_string("{\"hello\": \"world\"}") })
|> glace.start()

pub fn new() -> GlaceBuilder(a)

Creates a new glace builder that allows chaining subsequent functions

This is the entry point to setting glace

Usage:


glace.new()

pub fn not_found() -> Response(ResponseData)

Helper function to return a 404 response for a request

Usage:


glace.new()
|> glace.get("/", fn(_) { glace.not_found() })
|> glace.start()

pub fn port(
  builder: GlaceBuilder(a),
  port: Int,
) -> GlaceBuilder(a)

Sets the port

By default, glace starts listening on port 4000

Using this function overrides the default port

Usage:

glace.new
|> glace.port(3000)
pub fn post(
  builder: GlaceBuilder(a),
  path: String,
  handler: fn(Request(a)) -> Response(ResponseData),
) -> GlaceBuilder(a)

Registers a POST request for the specified path

Usage:


glace.new()
|> glace.post("/", fn(_) { glace.text("Hello, World!") })
|> glace.start()

pub fn start(builder: GlaceBuilder(Nil)) -> Subject(Message)

Starts the server

This is usually the last step in the pipeline

Usage:


glace.new()
|> glace.get("/", fn(_) { glace.text("Hello, World!") })
|> glace.start()

pub fn start_with_context(
  builder: GlaceBuilder(a),
  context: a,
) -> Subject(Message)

Starts the server with a context that will be forwarded for each request

This is usually the last step in the pipeline (same as start() but with context)

Usage:

let context = Context(db: "app.db")

glace.new()
|> glace.get("/", fn(req) { handle_request(req) })
|> glace.start_with_context(context)

fn handle_request(req: Request) -> Response {
  let db = request.context.db
  c.text("Connected to db", 200)
}
pub fn text(
  body input: String,
  status status: Int,
) -> Response(ResponseData)

Helper function to return text response for a request

Usage:


glace.new()
|> glace.get("/", fn(_) { glace.text("Hello, World!") })
|> glace.start()

Search Document