API
Mongoose.MgConnection
— TypeMgConnection
A type alias for a pointer to a Mongoose connection. This is used to represent a connection to a client in the Mongoose server.
Mongoose.MgHttpHeader
— Typestruct MgHttpHeader
name::MgStr
val::MgStr
end
A Julia representation of Mongoose's struct mg_http_header
, representing a single HTTP header.
Fields
name::MgStr
: AnMgStr
structure representing the header field name (e.g., "Content-Type").val::MgStr
: AnMgStr
structure representing the header field value (e.g., "application/json").
Mongoose.MgHttpMessage
— Typestruct 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 ofMgHttpHeader
structs representing the HTTP headers.body::MgStr
: The body of the HTTP message.message::MgStr
: The entire raw HTTP message.
Mongoose.MgStr
— Typestruct 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.
Mongoose.mg_body
— Methodmg_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.
Mongoose.mg_headers
— Methodmg_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.
Mongoose.mg_http_reply
— Methodmg_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.
Mongoose.mg_json_reply
— Methodmg_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
.
Mongoose.mg_message
— Methodmg_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.
Mongoose.mg_method
— Methodmg_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").
Mongoose.mg_proto
— Methodmg_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").
Mongoose.mg_query
— Methodmg_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").
Mongoose.mg_register!
— Methodmg_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...)
.
Mongoose.mg_serve!
— Methodmg_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.
Mongoose.mg_shutdown!
— Methodmg_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.
Mongoose.mg_text_reply
— Methodmg_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
.
Mongoose.mg_uri
— Methodmg_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").