API

Mongoose.MgConnectionType
MgConnection

A type alias for a pointer to a Mongoose connection. This is used to represent a connection to a client in the Mongoose server.

source
Mongoose.MgHttpHeaderType
struct MgHttpHeader
    name::MgStr
    val::MgStr
end

A Julia representation of Mongoose's struct mg_http_header, representing a single HTTP header.

Fields

  • name::MgStr: An MgStr structure representing the header field name (e.g., "Content-Type").
  • val::MgStr: An MgStr structure representing the header field value (e.g., "application/json").
source
Mongoose.MgHttpMessageType
struct MgHttpMessage
    method::MgStr
    uri::MgStr
    query::MgStr
    proto::MgStr
    headers::NTuple{MG_MAX_HTTP_HEADERS, MgHttpHeader}
    body::MgStr
    message::MgStr
end

A Julia representation of Mongoose's struct mg_http_message, containing parsed information about an HTTP request or response.

Fields

  • method::MgStr: The HTTP method (e.g., "GET", "POST").
  • uri::MgStr: The request URI (e.g., "/api/data").
  • query::MgStr: The query string part of the URI (e.g., "id=123").
  • proto::MgStr: The protocol string (e.g., "HTTP/1.1").
  • headers::NTuple{MG_MAX_HTTP_HEADERS, MgHttpHeader}: A tuple of MgHttpHeader structs representing the HTTP headers.
  • body::MgStr: The body of the HTTP message.
  • message::MgStr: The entire raw HTTP message.
source
Mongoose.MgStrType
struct MgStr
    ptr::Cstring
    len::Csize_t
end

A Julia representation of Mongoose's struct mg_str which is a view into a string buffer. It's used to represent strings returned by Mongoose.

Fields

  • ptr::Cstring: A pointer to the beginning of the string data in memory.
  • len::Csize_t: The length of the string in bytes.
source
Mongoose.mg_bodyMethod
mg_body(message::MgHttpMessage) -> String

Extracts the body of the HTTP message from an MgHttpMessage as a Julia String.

Arguments

  • message::MgHttpMessage: The HTTP message object.

Returns

String: The body content of the HTTP message.

source
Mongoose.mg_headersMethod
mg_headers(message::MgHttpMessage) -> NamedTuple

Extracts all HTTP headers from an MgHttpMessage into a Julia Named Tuple.

Arguments

  • message::MgHttpMessage: The HTTP message object.

Returns

NamedTuple: A dictionary where keys are header names and values are header values.

source
Mongoose.mg_http_replyMethod
mg_http_reply(conn::MgConnection, status::Integer, headers::AbstractString, body::AbstractString)::Cvoid

Sends an HTTP reply to a connected client. It constructs and sends an HTTP response including the status code, headers, and body.

Arguments

  • conn::MgConnection: A pointer to the Mongoose connection to which the reply should be sent.
  • status::Integer: The HTTP status code (e.g., 200 for OK, 404 for Not Found).
  • headers::AbstractString: A string containing HTTP headers, separated by \r\n. For example: "Content-Type: text/plain\r\nCustom-Header: value\r\n".
  • body::AbstractString: The body of the HTTP response.
source
Mongoose.mg_json_replyMethod
mg_json_reply(conn::MgConnection, status::Integer, body::AbstractString)

This is a convenience function that calls mg_http_reply with the Content-Type header set to application/json.

source
Mongoose.mg_messageMethod
mg_message(message::MgHttpMessage) -> String

Extracts the entire raw HTTP message from an MgHttpMessage as a Julia String.

Arguments

  • message::MgHttpMessage: The HTTP message object.

Returns

String: The complete raw HTTP message string.

source
Mongoose.mg_methodMethod
mg_method(message::MgHttpMessage) -> String

Extracts the HTTP method from an MgHttpMessage as a Julia String.

Arguments

  • message::MgHttpMessage: The HTTP message object.

Returns

String: The HTTP method (e.g., "GET", "POST").

source
Mongoose.mg_protoMethod
mg_proto(message::MgHttpMessage) -> String

Extracts the protocol string from an MgHttpMessage as a Julia String.

Arguments

  • message::MgHttpMessage: The HTTP message object.

Returns

String: The protocol string (e.g., "HTTP/1.1").

source
Mongoose.mg_queryMethod
mg_query(message::MgHttpMessage) -> String

Extracts the query string from an MgHttpMessage as a Julia String.

Arguments

  • message::MgHttpMessage: The HTTP message object.

Returns

String: The query string (e.g., "param=value&id=1").

source
Mongoose.mg_register!Method
mg_register!(method::Symbol, uri::AbstractString, handler::Function)

Registers an HTTP request handler for a specific method and URI.

Arguments

  • method::AbstractString: The HTTP method (e.g., GET, POST, PUT, PATCH, DELETE).
  • uri::AbstractString: The URI path to register the handler for (e.g., "/api/users").
  • handler::Function: The Julia function to be called when a matching request arrives.

This function should accept two arguments: (conn::MgConnection, message::MgHttpMessage; kwargs...).

source
Mongoose.mg_serve!Method
mg_serve!(host::AbstractString="127.0.0.1", port::Integer=8080)::Nothing

Starts the Mongoose HTTP server. Initialize the Mongoose manager, binds an HTTP listener, and starts a background Task to poll the Mongoose event loop.

Arguments

  • host::AbstractString="127.0.0.1": The IP address or hostname to listen on. Defaults to "127.0.0.1" (localhost).
  • port::Integer=8080: The port number to listen on. Defaults to 8080.
source
Mongoose.mg_shutdown!Method
mg_shutdown!()::Nothing

Stops the running Mongoose HTTP server. Sets a flag to stop the background event loop task, and then frees the Mongoose associated resources.

source
Mongoose.mg_text_replyMethod
mg_text_reply(conn::MgConnection, status::Integer, body::AbstractString)

This is a convenience function that calls mg_http_reply with the Content-Type header set to text/plain.

source
Mongoose.mg_uriMethod
mg_uri(message::MgHttpMessage) -> String

Extracts the URI from an MgHttpMessage as a Julia String.

Arguments

  • message::MgHttpMessage: The HTTP message object.

Returns

String: The request URI (e.g., "/api/users").

source