This project is a tiny wrapper around lua-http
, all it does it implement a more friendly
default http server setup for small services.
This project was created to move part of my lacord project code into its own repository.
The module you require exports one function: new()
.
This function will take configuration and give you back a lua-http
server object.
Constructs a new lua-http server configured according to options
.
-
table
options.routes
options.routes
must be a table which can be indexed with a uri path to produce a callback to handle the request. This callback will receive aresponse
object. There is no fancy matching except the following: if no function is found the key'*'
will be used to lookup a callback. As an alternativeoptions.routes
may itself be a function, in which case it will be called for every request. -
luaossl openssl context?
options.ctx
An optional openssl context object may be provided instead of providingcrtfile
andkeyfile
. If this option is not provided and neither arecrtfile
andkeyfile
then TLS will not be enabled. -
string
options.server
An optional string to set as the value of the server header. This should be a name identifying your application, by default this will be"shs-http-server"
. -
anything
options.data
An optional value to be attached to response objects in their.data
field. -
table
options.response_mt
An optional table to override the default response object metatable. The default metatable is exported asshs.response_mt
. -
string (file path)
crtfile
An optional file path which should be the location of your TLS certificate chain in PEM format. -
string (file path)
keyfile
An optional file path which should be the location of your certificate's associated private key in PEM format.
Note that options.ctx
and crtfile
/ keyfile
are mutually exclusive. Please ensure when compiling luaossl you use a modern and up-to-date version of openssl.
I would recommend using something like Nginx as a reverse proxy and having shs servers on local ports.
The callbacks in routes
will all receive a response
object as their argument. Its methods and properties are described below:
The request's associated headers.
The underlying stream from client to server.
The address of the peer of the connection.
The request's path with the query and fragment removed.
A table containing the query portion of the request's path as key-value pairs (all strings).
The request's HTTP method.
Data attached to the response from the new option data
.
The headers to send back with the response.
Retrieves the request's body, and will decompress it if necessary.
Sets the body to send back in the response.
- string
body
Sets the status code to 500
and sets an appropriate default body.
Sets the status code to 503
and sets an appropriate default body.
Sets the status code to 401
and sets an appropriate default body.
- string
msg
An alternative error message to be sent astext/plain
.
Sets the status code to 204
.
Sets the status code to 200
and sets the body and content type.
- string
body
- string (content type)
content_type
Sets the status code and sets the body and content type.
- integer (HTTP status code)
code
- string
body
- string (content type)
content_type
Sets the status code to 302
and sets the location header.
- string (uri)
location
Sets the status code and sets the location header.
- integer (HTTP status code in [300, 400) )
code
This value will become302
if it is outside the valid 3XX range. - string (uri)
location