messua
The root module is for server configuration.
The with_
functions (and the one without_
function) are builder
pattern functions.
import messua
import my_module
fn main() {
messua.default()
|> messua.with_binding("localhost")
|> messua.with_http(8080)
|> messua.start(my_module.my_handler)
}
Types
HTTPS configuration struct to pass to with_https()
.
pub type HttpsConfig {
HttpsConfig(port: Int, cert_path: String, key_path: String)
}
Constructors
-
HttpsConfig(port: Int, cert_path: String, key_path: String)
Arguments
-
port
Port on which to listen for incoming secure requests.
-
cert_path
Path to your SSL certificate file. This will probably be something like
cert.pem
. -
key_path
Path to your SSL private key file. This will probably be something like
privkey.pem
.
-
Functions
pub fn default() -> Config(Nil)
Return a new default server configuration:
- Listens on
0.0.0.0
- Serves HTTP requests on port 80
pub fn default_https(
cert_path: String,
key_path: String,
) -> HttpsConfig
HTTPS configuration serving on the default port (443).
pub fn start(
cfg: Config(a),
handler: fn(MRequest(a)) -> Result(Response(ResponseData), Err),
) -> Nil
Start the server with your given handler function.
Safety
If you have misconfigured your server in a fatal way (such as specifying an invalid or noexistent SSL certificate file, or setting it to listen for neither HTTP or HTTPS requests), Messua will just crash right away. This seems like a better option than limping along while you wonder why nothing is working correctly.
pub fn with_binding(cfg: Config(a), iface: String) -> Config(a)
Bind to the given interface.
pub fn with_http(cfg: Config(a), port: Int) -> Config(a)
Serve HTTP requests on a port other than 80.
pub fn with_https(
cfg: Config(a),
https_config: HttpsConfig,
) -> Config(a)
Serve secure HTTPS requests.
pub fn with_state(cfg: Config(a), state: b) -> Config(b)
The value it inject into each request as “state” so your handlers have access to it.
This might be a database handle or some type of configuration struct.
pub fn without_http(cfg: Config(a)) -> Config(a)
Do not serve HTTP requests.
The default configuration serves HTTP on port 80, so if you don’t want to serve insecure requests, you need to use this.