Build some Blazing Fast APIs, An OpenResty starter kit to write pure Lua APIs.
No framework ! Forget about your favorite framework, forget about Flask, Express or even Sinatra, here the HTTP requests are handled directly in nginx by executing Lua scripts.
First you need OpenResty to be installed, then you can launch nginx with :
openresty -p . -c conf/nginx.conf
Everything is contained into the nginx configuration file :
worker_processes 1;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {
charset utf-8;
default_type application/json;
# Docker DNS
resolver 127.0.0.11;
lua_package_path '${prefix}lua/?.lua;;';
lua_package_cpath '${prefix}lua/?.so;;';
server {
listen 8080;
# Docker Compose services
set $redis_host redis;
set $redis_port 6379;
set $postgres_host postgres;
set $postgres_port 5432;
set $postgres_database bfa;
set $postgres_user bfa;
set $postgres_password bfa;
location / {
content_by_lua_file lua/bootstrap.lua;
}
}
}
If you work outside Docker Compose, pay attention to
resolver
directive and "Docker Compose services" variables, you'll probably have to modify these values according to your environment.
A Dockerfile is provided to build a Docker image of the API :
docker build -t bfa .
Note that :
- The Docker image is based on the OpenResty Official Docker Image
- OPM dependencies are installed during the building process (See below)
Once the Docker image is built, start the container with the following command :
docker run -it --rm -p 8080:8080 bfa
A docker-compose.yml file is also provided to orchestrate three containers :
- The API (See above)
- A PostgreSQL database
- A Redis server
Take a look at the following component diagram :
To start the environment, simply run :
docker-compose up -d --build
The Docker image of the API is built before start due to
--build
option.
Two libraries are installed during the Docker image building process :
- lua-resty-http : An HTTP Client
- pgmoon : A PostgreSQL driver
This stater kit comes with some example routes to demonstrate the usage of libraries :
Route | Description | Script |
---|---|---|
/ip | Call an external service | ip.lua |
/kv | Get or set a value in Redis | kv.lua |
/articles | CRUD operations on PostgreSQL resource | articles.lua |
Routes are defined in routes.lua script.