glemini

Main Glemini module. Contains the server and response functions.

Example usage:

let config =
  new_config()
  |> add_ssl(certfile: "certs/cert.crt", keyfile: "certs/cert.key")
  |> add_handler(fn(req) {
    case req.path {
      "/" -> gemtext_response([gemtext.heading1("Welcome to Glemini!")])
      _ -> not_found_response()
    }
  })
let assert Ok(_) = start(config)

Types

pub opaque type GleminiError
pub opaque type Request
pub type Response {
  SuccessResponse(status: Int, mimetype: String, body: String)
  InputResponse(status: Int, prompt: String)
  RedirectResponse(status: Int, uri: String)
  ErrorResponse(status: Int, message: Option(String))
}

Constructors

  • SuccessResponse(status: Int, mimetype: String, body: String)
  • InputResponse(status: Int, prompt: String)
  • RedirectResponse(status: Int, uri: String)
  • ErrorResponse(status: Int, message: Option(String))

Glemini server configuration.

pub type ServerConfig {
  ServerConfig(
    port: Int,
    certfile: String,
    keyfile: String,
    request_handler: fn(Request) -> Response,
  )
}

Constructors

  • ServerConfig(
      port: Int,
      certfile: String,
      keyfile: String,
      request_handler: fn(Request) -> Response,
    )

Functions

pub fn add_handler(
  config: ServerConfig,
  request_handler: fn(Request) -> Response,
) -> ServerConfig

Adds a request handler to the server configuration. The request handler is your main function where you’ll do routing.

Example handler

fn my_request_handler(req: Request) -> Response {
  case req.path {
    "/hello" -> gemtext_response([gemtext.heading1("Hello, world!")])
    _ -> not_found_response()
  }
}
pub fn add_ssl(
  config: ServerConfig,
  certfile certfile: String,
  keyfile keyfile: String,
) -> ServerConfig

Adds ssl configuration to the server configuration.

pub fn bad_request_response(message: String) -> Response

Creates a bad request response, status 59, with a given message.

pub fn certificate_not_authorized_response(
  message: String,
) -> Response

Creates a certificate not authorized response, status 61, with a given message.

pub fn certificate_not_valid_response(
  message: String,
) -> Response

Creates a certificate not valid response, status 62, with a given message.

pub fn cgi_error_response() -> Response

Creates a CGI error response, status 42.

pub fn client_certificate_required_response(
  message: String,
) -> Response

Creates a client certificate required response, status 60, with a given message.

pub fn gemtext_response(lines: List(Line)) -> Response

Creates a success response, status 20, with a gemtext body created from a list of gemtext lines.

pub fn gone_response() -> Response

Creates a gone response, status 52.

pub fn input_response(prompt: String) -> Response

Creates an input response, status 10, with a given prompt.

pub fn new_config() -> ServerConfig

Creates a new server configuration with missing certificate and empty request handler.

pub fn not_found_response() -> Response

Creates a not found response, status 51.

pub fn permanent_failure_response(message: String) -> Response

Creates a permanent failure response, status 50, with a given message.

pub fn permanent_redirect_response(uri: String) -> Response

Creates a permanent redirect response, status 31, with a given uri.

pub fn proxy_error_response() -> Response

Creates a proxy error response, status 43.

pub fn proxy_request_refused_response() -> Response

Creates a proxy request refused response, status 53.

pub fn sensitive_input_response(prompt: String) -> Response

Creates a sensitive input response, status 11, with a given prompt.

pub fn server_unavailable_response() -> Response

Creates a server unavailable response, status 41.

pub fn slow_down_response() -> Response

Creates a slow down response, status 44.

pub fn start(
  config: ServerConfig,
) -> Result(Subject(Message), StartError)

Starts the gemini server. Expects a ServerConfig

Example

new_config()
|> add_ssl(certfile: "certs/cert.crt", keyfile: "certs/cert.key")
|> add_handler(fn(req) {
  case req.path {
    "/hello" -> gemtext_response([gemtext.heading1("Welcome to Glemini!")])
    _ -> not_found_response()
  }
})
|> start
pub fn success_response(
  mimetype: String,
  body: String,
) -> Response

Creates a success response, status 20, with a given mimetype and body.

pub fn temporary_failure_response(message: String) -> Response

Creates a temporary failure response, status 40, with a given message.

pub fn temporary_redirect_response(uri: String) -> Response

Creates a temporary redirect response, status 30, with a given uri.

Search Document