Minirps
Mini reverse proxy server written in rust
Install / Use
/learn @marcodpt/MinirpsREADME
Mini RPS
Mini reverse proxy server written in rust
❤️ Features
- Static file server.
- HTTPS
- CORS
- The optional configuration file can be written in JSON or TOML.
- minijinja templates with custom
functions:
- read, write and remove files from the filesystem.
- Send http requests in the template.
- Execute commands in the template.
- Reverse proxy.
- Modify the response headers and status in the template.
- Parse and format to:
- Safe rust and good code organization.
- No panics after startup (every panic is a bug).
- Extensively tested with hurl.
- Good debugging experience with the server displaying requests in the terminal and error messages in templates for humans.
- Designed following the principles of UNIX philosophy.
💻 Install
cargo install minirps
Alternatively you can use one of the precompiled binaries available with each release (currently generic Linux only).
🎮 Usage
Help
minirps -h
Simple static file server
minirps path/to/static/folder
Serve hidden files
minirps -a path/to/static/folder
Ignore markdown files in root folder
minirps -i "/*.md" path/to/static/folder
Ignore any markdown files
minirps -i "/**/*.md" path/to/static/folder
Running on port 4000 instead of 3000
minirps -p 4000 path/to/static/folder
Using https instead of http
minirps path/to/static/folder -c path/to/cert.pem -k path/to/key.pem
Allow CORS from all origins
minirps -o path/to/static/folder
Start the server with a config file
The supported formats are JSON and TOML.
minirps -f path/to/config/file
Send HTML template response instead of API response
Here it is assumed that there are
minijinja templates users.html
and edit_user.html
config.toml
templates = "path/to/templates/folder"
assets = "path/to/static/folder"
port = 4000
cert = "path/to/cert.pem"
key = "path/to/key.pem"
cors = []
[[routes]]
method = "GET"
path = "/api/users"
template = "users.html"
[[routes]]
method = "GET"
path = "/api/users/:id"
template = "edit_user.html"
[[routes]]
method = "POST"
path = "/api/users/:id"
template = "edit_user.html"
Alternatively you can use a JSON file
config.json
{
"templates": "path/to/templates/folder",
"assets": "path/to/static/folder",
"port": 4000,
"cert": "path/to/cert.pem",
"key": "path/to/key.pem",
"cors": [],
"routes": [
{
"method": "GET",
"path": "/api/users",
"template": "users.html"
}, {
"method": "GET",
"path": "/api/users/:id",
"template": "edit_user.html"
}, {
"method": "POST",
"path": "/api/users/:id",
"template": "edit_user.html"
}
]
}
💯 Examples
Demo
minirps -f examples/demo/config.toml
alternatively
minirps -f examples/demo/config.json
Here it was implemented:
- Command Line: use of the command line through a minijinja custom function.
- Periodic Table: A periodic table web interface was built from a JSON file.
- Star Wars API: Web interface for swapi Star Wars API.
- Note taking app: An example using the file system to save and read data.
- Form Data: Sending and reading examples.
- CORS: A working demo of a CORS request, needs both servers running.
Test
In this example, a static server and some routes are built to test the use of reverse proxy and templates automatically using hurl.
minirps -f examples/tests/config.toml
hurl --test examples/tests/test.hurl
📢 Motivation
The objective of this project is to deliver an http server in a single self-contained binary.
Where the basics should be obtained without any configuration file:
- static file server.
- HTTPS
- CORS
And where other reverse proxy functionalities are obtained with simple configurations.
Templates have the ability to send requests, read and write files and execute commands.
This way they can interact with resources such as databases without the need for a complete scripting language such as php, python, ruby...
A small, highly extensible server, without having to manage operating system versions, dependencies and packages.
It simply works!
📖 Docs
config
Command line arguments take priority over config file if both are present.
Command line argument paths are relative to the current working directory.
config paths are relative to your own directory.
Currently, any changes to config, the server must be restarted for them
to be applied.
port: integer?
Optional integer port number to run the server on, default: 3000
all: bool
Whether to display hidden files.
In case of confirmation via the command line or config file they will be
displayed.
ignore: [string]?
List of files to ignore using glob expressions.
If the -i option is passed on the command line it will be appended to the list.
The routes must be considered in relation to the assets folder and not the working directory.
For a complete reference of glob expressions and possible bugs check this library.
cors: [string]?
Optional array of strings representing allowed origins for CORS requests.
An empty array allows all origins.
If this variable is not defined,CORS will be disabled.
cert: string?
Optional string with the public key file path for the https server.
Only if the cert and key are available will the server run over https.
key: string?
Optional string with the private key file path for the https server.
Only if the cert and key are available will the server run over https.
assets: string?
Optional string with the static files folder path.
templates: string?
Optional string with the path to the minijinja templates folder.
data: string?
Optional string with the path where templates can read, write and remove
files. If not passed, these functions will be unavailable to templates.
routes: [{method, path, template}]
Optional array of objects that define routes:
methodstring: one of the http methods:- GET
- POST
- DELETE
- PUT
- PATCH
- HEAD
- OPTIONS
- TRACE
- CONNECT
pathstring: the path associated with the route,:varis acceptable for setting path variables (ex: /api/user/:id).templatestring: the template path associated with this route within thetemplatesfolder.
Template variables
method: string
The method associated with this route. It is useful when the same template
is used in many routes.
url: string
It is the junction of the path and the route query.
http://localhost:3000/api/users?name=john#me => /api/users?name=john
route: string
It is the route as declared in the config file.
/api/user/:id
path: string
The associated path passed by the client in the request.
http://localhost:3000/api/users?name=john => /api/users
query: string?
The associated query string passed by the client in the request.
http://localhost:3000/api/users?name=john => name=john
params: {name: value}
The associated object of the path params associated with the client
request on a given route.
namestring: The name of the parameter as declared in theroute.valuestring: The value of the parameter passed in thepath.
/api/user/:id => http://localhost:3000/api/user/25 => {"id": "25"}
vars: {name: value}
The associated object of the query params associated with the client request.
namestring: The name of the parameter passed in thequery.valuestring: The value of the parameter passed in thequery.
http://localhost:3000/api/users?name=john => {"name": "john"}
headers: {name: value}
The associated object of the headers passed by the client in the request.
Note that all header keys are in lowercase.
namestring: The name of the header passed in the request.valuestring: The value of the header passed in the request.
Content-Type: text/plain => {"content-type": "text/plain"}
body: binary
The body passed by the client in the request.
Template return state
Variables that, if defined, modify the behavior of the server response.
It only works if they are declared outside the blocks to be returned in the template's global state.
modify {status, headers: {name: value}}
The response body is always the result of the template, and this variable allows you to modify the status code and headers.
status(integer?): The new response status code, if not passed, will use 200 by default.headers({name: value}?): The headers that should be changed in the response.
An example of
