Skip to content

Lajule/bfa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bfa

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.

Getting started

First you need OpenResty to be installed, then you can launch nginx with :

openresty -p . -c conf/nginx.conf

Configuration

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.

Docker

A Dockerfile is provided to build a Docker image of the API :

docker build -t bfa .

Note that :

Once the Docker image is built, start the container with the following command :

docker run -it --rm -p 8080:8080 bfa

Docker Compose

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 :

Docker Compose services

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.

OPM

Two libraries are installed during the Docker image building process :

Example routes

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.