Skip to content

Commit

Permalink
Structure refactor.
Browse files Browse the repository at this point in the history
- Made it a luarocks
- Builtin make luarocks
- Better project structure
  • Loading branch information
thibaultcha committed Dec 3, 2014
1 parent 3390710 commit 284ae92
Show file tree
Hide file tree
Showing 23 changed files with 141 additions and 165 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nginx_tmp
tmp/
108 changes: 34 additions & 74 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,75 +1,35 @@
PREFIX ?= /usr/local
OPENRESTY_PREFIX ?= $(PREFIX)/openresty
INSTALL ?= @install
LUA_INCLUDE_DIR ?= $(PREFIX)/include
LUA_LIB_DIR ?= $(PREFIX)/lib/lua/$(LUA_VERSION)
CURRENT_FOLDER ?= `pwd`

# Env variables
DEV_DAEMON=off
PROD_DAEMON=on

DEV_LUA_CODE_CACHE=off
PROD_LUA_CODE_CACHE=on

DEV_LUA_PATH ?= $(CURRENT_FOLDER)/lib/?.lua;;
PROD_LUA_PATH ?= $(LUA_LIB_DIR)/?.lua;;

PROD_CONF_DIR ?= /etc/apenode

DEV_CONF_PATH ?= $(CURRENT_FOLDER)/etc/conf.yaml
PROD_CONF_PATH ?= $(PROD_CONF_DIR)/conf.yaml

.PHONY: all test install

all: ;

install: all

##############################
# Install the base structure #
##############################

$(INSTALL) -d $(LUA_LIB_DIR)/resty/apenode
@cp -R lib/resty/apenode/ $(LUA_LIB_DIR)/resty/apenode/

#############################
# Install the configuration #
#############################

# - nginx.conf
@sed \
-e "s/{DAEMON}/$(PROD_DAEMON)/g" \
-e "s/{LUA_CODE_CACHE}/$(PROD_LUA_CODE_CACHE)/g" \
-e "s@{LUA_PATH}@$(PROD_LUA_PATH)@g" \
-e "s@{CONF_PATH}@$(PROD_CONF_PATH)@g" nginx.conf > $(OPENRESTY_PREFIX)/nginx/conf/nginx.conf;
@if [ -a $(OPENRESTY_PREFIX)/nginx/conf/nginx.conf ]; then \
cp $(OPENRESTY_PREFIX)/nginx/conf/nginx.conf $(OPENRESTY_PREFIX)/nginx/conf/nginx.conf.old; \
fi;

# - conf.yaml
$(INSTALL) -d $(PROD_CONF_DIR)
$(INSTALL) etc/conf.yaml $(PROD_CONF_DIR)

uninstall: all

@rm -rf $(LUA_LIB_DIR)/resty/apenode
@rm -rf $(PROD_CONF_DIR)
@mv $(OPENRESTY_PREFIX)/nginx/conf/nginx.conf.old $(OPENRESTY_PREFIX)/nginx/conf/nginx.conf

run-dev: all
@rm -rf nginx_tmp
@mkdir -p nginx_tmp/logs
@echo "" > nginx_tmp/logs/error.log
@echo "" > nginx_tmp/logs/access.log

PWD = `pwd`
# Dev environment variables
DEV_DAEMON ?= off
DEV_LUA_LIB ?= lua_package_path \"$(PWD)/src/?.lua\;\;\"\;
DEV_LUA_CODE_CACHE ?= off
DEV_APENODE_CONF ?= $(PWD)/tmp/apenode.dev.yaml
DEV_APENODE_PORT ?= 8000
DEV_APENODE_WEB_PORT ?= 8001

.PHONY: test local global

test:
@echo "Tests with busted"

local:
@luarocks make apenode-0.0-1.rockspec --local

global:
@sudo luarocks make apenode-0.0-1.rockspec

run:
@mkdir -p tmp/nginx/logs
@cp templates/apenode.yaml $(DEV_APENODE_CONF)
@echo "" > tmp/nginx/logs/error.log
@echo "" > tmp/nginx/logs/access.log
@sed \
-e "s/{DAEMON}/$(DEV_DAEMON)/g" \
-e "s/{LUA_CODE_CACHE}/$(DEV_LUA_CODE_CACHE)/g" \
-e "s@{LUA_PATH}@$(DEV_LUA_PATH)@g" \
-e "s@{CONF_PATH}@$(DEV_CONF_PATH)@g" nginx.conf > nginx_tmp/nginx_dev.conf;

@nginx -p ./nginx_tmp -c nginx_dev.conf

test: all
PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t
-e "s/{{DAEMON}}/$(DEV_DAEMON)/g" \
-e "s@{{LUA_LIB_PATH}}@$(DEV_LUA_LIB)@g" \
-e "s/{{LUA_CODE_CACHE}}/$(DEV_LUA_CODE_CACHE)/g" \
-e "s/{{PORT}}/$(DEV_APENODE_PORT)/g" \
-e "s/{{WEB_PORT}}/$(DEV_APENODE_WEB_PORT)/g" \
-e "s@{{APENODE_CONF}}@$(DEV_APENODE_CONF)@g" \
templates/nginx.conf > tmp/nginx/nginx.conf;

@nginx -p ./tmp/nginx -c nginx.conf
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# Apenode
## Apenode

lua-resty-apenode - Lua Apenode core dependencies

[![The beginning of a new era](http://img.youtube.com/vi/U2iiPpcwfCA/0.jpg)](http://www.youtube.com/watch?v=U2iiPpcwfCA)

# Installation
### Requirements
- Lua `5.1`
- Luarocks for Lua `5.1`
- Openrestify ([Download](http://openresty.com/#Download) the latest version of OpenResty and install it.)

* [Download](http://openresty.com/#Download) the latest version of OpenResty and install it.
* Execute `make install` to install the Apenode
### Installation
- `make global` or `make local`

# Running

You can run the Apenode in two different modes:

* In background, with: `sudo nginx`
* Or in foreground, with: `sudo nginx -g "daemon off;"`
### Running for development
- `make run`

By default it will be running on port `8000`, so navigate to [http://localhost:8000/](http://localhost:8000/) after starting it.
48 changes: 48 additions & 0 deletions apenode-0.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package = "apenode"
version = "0.0-1"
source = {
url = "git://github.com/Mashape/lua-resty-apenode",
branch = "master"
}
description = {
summary = "Apenode, the fastest and most installed API layer in the universe",
detailed = [[
The Apenode is the most popular API layer in the world
that provides API management and analytics for any kind
of API.
]],
homepage = "http://apenode.com",
license = "MIT"
}
dependencies = {
"lua ~> 5.1",

"luasec ~> 0.5-2",
"uuid ~> 0.2-1",
"yaml ~> 1.1.1-1",
"lapis ~> 1.0.6-1",
"inspect ~> 3.0-1",
"luaxml ~> 101012-1",
"busted ~> 2.0.rc3-0"
}
build = {
type = "builtin",
modules = {
["apenode"] = "src/main.lua",
["apenode.core.access"] = "src/core/access.lua",
["apenode.core.handler"] = "src/core/handler.lua",
["apenode.core.handler_filter"] = "src/core/header_filter.lua",
["apenode.core.log"] = "src/core/log.lua",
["apenode.core.utils"] = "src/core/utils.lua",

["apenode.web.app"] = "src/web/app.lua",

["apenode.dao.memory"] = "src/dao/memory/factory.lua",
["apenode.dao.memory.api"] = "src/dao/memory/api.lua",
["apenode.dao.memory.application"] = "src/dao/memory/application.lua",

["apenode.plugins.transformations.handler"] = "src/plugins/transformations/handler.lua",
["apenode.plugins.transformations.header_filter"] = "src/plugins/transformations/header_filter.lua",
["apenode.plugins.transformations.body_filter"] = "src/plugins/transformations/body_filter.lua"
}
}
28 changes: 0 additions & 28 deletions apenode-0.1-1.rockspec

This file was deleted.

17 changes: 0 additions & 17 deletions lib/resty/apenode/dao/memory/factory.lua

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

local cjson = require "cjson"
local inspect = require "inspect"
local utils = require "resty.apenode.utils"
local utils = require "apenode.core.utils"

local _M = { _VERSION = '0.1' }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- Copyright (C) Mashape, Inc.

local access = require "resty.apenode.plugins.core.access"
local header_filter = require "resty.apenode.plugins.core.header_filter"
local log = require "resty.apenode.plugins.core.log"
local access = require "apenode.core.access"
local header_filter = require "apenode.core.header_filter"
local log = require "apenode.core.log"


local _M = { _VERSION = '0.1' }
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


local cjson = require "cjson"
local utils = require "resty.apenode.utils"
local utils = require "apenode.core.utils"


local _M = { _VERSION = '0.1' }
Expand Down
1 change: 0 additions & 1 deletion lib/resty/apenode/utils.lua → src/apenode/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ function _M.create_timer(func, data)
ngx.log(ngx.ERR, "failed to create timer: ", err)
return
end

end


Expand Down
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions src/apenode/dao/memory/factory.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Copyright (C) Mashape, Inc.


local application_dao = require "apenode.dao.memory.application"
local api_dao = require "apenode.dao.memory.api"


local _M = {

_VERSION = '0.1',
api = api_dao,
application = application_dao

}


return _M
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- Copyright (C) Mashape, Inc.

local header_filter = require "resty.apenode.plugins.transformations.header_filter"
local body_filter = require "resty.apenode.plugins.transformations.body_filter"
local header_filter = require "apenode.plugins.transformations.header_filter"
local body_filter = require "apenode.plugins.transformations.body_filter"

local _M = { _VERSION = '0.1' }

Expand Down
4 changes: 2 additions & 2 deletions lib/resty/apenode/web/app.lua → src/apenode/web/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
local lapis = require("lapis")
local app_helpers = require("lapis.application")
local validate = require("lapis.validate")
local utils = require "resty.apenode.utils"
local utils = require "apenode.core.utils"
local capture_errors, yield_error = app_helpers.capture_errors, app_helpers.yield_error


Expand All @@ -23,7 +23,7 @@ end)

app:get("/apis/:id", function(self)
local api = dao.api.get_by_id(self.params.id)
if api then
if api then
return utils.show_response(200, dao.api.get_by_id(self.params.id))
else
return utils.show_error(404, "Not found")
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/resty/apenode/main.lua → src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function _M.init(configuration_path)

-- Requiring the plugins
for i, plugin_name in ipairs(configuration.plugins) do
table.insert(plugins, require("resty.apenode.plugins." .. plugin_name .. ".handler"))
table.insert(plugins, require("apenode.plugins." .. plugin_name .. ".handler"))
end
end

Expand Down
1 change: 0 additions & 1 deletion t/logger.t

This file was deleted.

5 changes: 2 additions & 3 deletions etc/conf.yaml → templates/apenode.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
version: 0.1

# Specify the DAO to use
dao_factory: resty.apenode.dao.memory.factory
dao_factory: apenode.dao.memory

# Plugins
plugins:
- core
- transformations

...
---
Loading

0 comments on commit 284ae92

Please sign in to comment.