diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b29140 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.so +*.o +lualib/ourl/config.lua diff --git a/README.md b/README.md index dbfa4f4..690315e 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,21 @@ # Ourls-Resty [Ourls][1] 是由 [takashiki][2] 实现的一个基于发号和 hashid 的短网址服务。 -受这个项目的启发,将此工程移植,使用 [OpenResty][3] 实现。 +受这个项目的启发,将此项目移植到 [OpenResty][3] 。 ### 待增加的特性: - - 工程目录优化,以 OpenResty 目录结构为准 - Cache 支持 ### 安装方法: - - 安装 openresty rpm 包(或手动编译,建议使用 --prefix=/usr/local/openresty) - - 安装 libidn-devel 库(yum install libidn-devel) - - 将原 openresty/nginx/conf 目录备份 - - 将本工程解压到 openresty/nginx/conf 目录,执行 `install.sh` - - 修改 ourl/config.lua 中的数据库等配置 - - 恢复 urls.sql 至 mysql/mariadb 数据库 - - 进入 vhosts 目录,修改 ourl.conf 中的 server_name 为你自己的域名 - - 启动 openresty + - 安装 openresty [预编译包][9] ([手动编译教程][10]) + - 安装 gcc、make、libidn、libidn-devel (yum gcc make install libidn-devel) + - 将本工程解压到 openresty 目录,执行 `install.sh` (bash install.sh) + - 修改 lualib/ourl/config.lua 中的数据库配置、hashids 参数、可信代理的 cidr + - 恢复 urls.sql 至 mysql 或 mariadb 数据库 + - 进入 nginx/conf 目录,根据自己的实际情况修改 (合并配置,修改 server_name …) + - 启动 openresty (service openresty start) ### 使用到的其他项目 @@ -34,4 +32,6 @@ [5]: https://github.com/APItools/router.lua [6]: https://github.com/golgote/neturl [7]: https://github.com/mah0x211/lua-idna - [8]: https://github.com/hamishforbes/lua-resty-iputils \ No newline at end of file + [8]: https://github.com/hamishforbes/lua-resty-iputils + [9]: http://openresty.org/cn/rpm-packages.html + [10]: https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/install_on_centos.html \ No newline at end of file diff --git a/lib/idna/Makefile b/idna/Makefile similarity index 100% rename from lib/idna/Makefile rename to idna/Makefile diff --git a/lib/idna/idna.c b/idna/idna.c similarity index 100% rename from lib/idna/idna.c rename to idna/idna.c diff --git a/install.sh b/install.sh index 5d5a039..0425e02 100755 --- a/install.sh +++ b/install.sh @@ -1,7 +1,11 @@ -#/bin/sh +#/usr/bin/env bash BASE=$(dirname $(readlink -f ${0})) -cp "${BASE}/ourl/config.sample.lua" "${BASE}/ourl/config.lua" -cd "${BASE}/lib/hashids" && make -cd "${BASE}/lib/idna" && make linux -mv "${BASE}/lib/idna/idna.so" "${BASE}" -cd ${BASE} \ No newline at end of file +PLATFORM=$(uname -s) +case ${PLATFORM} in + Darwin) PLATFORM='macosx' ;; + FreeBSD) PLATFORM='bsd' ;; + *) PLATFORM='linux' ;; +esac +cp "${BASE}/lualib/ourl/config.sample.lua" "${BASE}/lualib/ourl/config.lua" +cd "${BASE}/lualib/hashids" && make +cd "${BASE}/idna" && make ${PLATFORM} && mv "${BASE}/idna/idna.so" "${BASE}/lualib" && cd ${BASE} \ No newline at end of file diff --git a/lib/hashids/Makefile b/lualib/hashids/Makefile similarity index 100% rename from lib/hashids/Makefile rename to lualib/hashids/Makefile diff --git a/lib/hashids/clib.c b/lualib/hashids/clib.c similarity index 100% rename from lib/hashids/clib.c rename to lualib/hashids/clib.c diff --git a/lib/hashids/init.lua b/lualib/hashids/init.lua similarity index 99% rename from lib/hashids/init.lua rename to lualib/hashids/init.lua index d032885..f10925a 100644 --- a/lib/hashids/init.lua +++ b/lualib/hashids/init.lua @@ -8,7 +8,7 @@ local strcat = table.concat local push = table.insert local str_switch_pos -local ok, lib = pcall(require, "lib.hashids.clib"); +local ok, lib = pcall(require, "hashids.clib"); if ok then str_switch_pos = lib.str_switch_pos; else diff --git a/lib/net/url.lua b/lualib/net/url.lua similarity index 100% rename from lib/net/url.lua rename to lualib/net/url.lua diff --git a/ourl/config.sample.lua b/lualib/ourl/config.sample.lua similarity index 100% rename from ourl/config.sample.lua rename to lualib/ourl/config.sample.lua diff --git a/ourl/init.lua b/lualib/ourl/init.lua similarity index 97% rename from ourl/init.lua rename to lualib/ourl/init.lua index 2325c3a..5e5bf9f 100644 --- a/ourl/init.lua +++ b/lualib/ourl/init.lua @@ -1,10 +1,9 @@ local config = require 'ourl.config' -local router = require 'lib.router' -local hashid = require 'lib.hashids.init' -local neturl = require 'lib.net.url' --- idna.so 必须在 lua_package_cpath 根目录下才能使用 +local router = require 'router' +local hashid = require 'hashids' +local neturl = require 'net.url' local idna = require 'idna' -local iputil = require 'lib.resty.iputils' +local iputil = require 'resty.iputils' local json = require 'cjson' local mysql = require 'resty.mysql' local r_sha1 = require 'resty.sha1' diff --git a/lib/resty/iputils.lua b/lualib/resty/iputils.lua similarity index 100% rename from lib/resty/iputils.lua rename to lualib/resty/iputils.lua diff --git a/lib/router.lua b/lualib/router.lua similarity index 100% rename from lib/router.lua rename to lualib/router.lua diff --git a/mime.types b/nginx/conf/mime.types similarity index 100% rename from mime.types rename to nginx/conf/mime.types diff --git a/nginx.conf b/nginx/conf/nginx.conf similarity index 64% rename from nginx.conf rename to nginx/conf/nginx.conf index 1ee25ee..48190b7 100644 --- a/nginx.conf +++ b/nginx/conf/nginx.conf @@ -8,8 +8,6 @@ http { default_type application/octet-stream; sendfile on; keepalive_timeout 65; - - lua_package_path '/usr/local/openresty/nginx/conf/?.lua;;'; - lua_package_cpath '/usr/local/openresty/nginx/conf/?.so;;'; + include vhosts/*.conf; } diff --git a/vhosts/ourl.conf b/nginx/conf/vhosts/ourl.conf similarity index 80% rename from vhosts/ourl.conf rename to nginx/conf/vhosts/ourl.conf index 60cf77c..ce67216 100644 --- a/vhosts/ourl.conf +++ b/nginx/conf/vhosts/ourl.conf @@ -1,7 +1,7 @@ server { listen 80; server_name t.sicaudm.com; - root conf/ourl/public; + root html/ourl; index index.html; location / { @@ -11,7 +11,7 @@ server { internal; default_type text/html; content_by_lua_block { - local t = require 'ourl.init' + local t = require 'ourl' t.run() } } diff --git a/ourl/public/css/app.css b/nginx/html/ourl/css/app.css similarity index 100% rename from ourl/public/css/app.css rename to nginx/html/ourl/css/app.css diff --git a/ourl/public/favicon.ico b/nginx/html/ourl/favicon.ico similarity index 100% rename from ourl/public/favicon.ico rename to nginx/html/ourl/favicon.ico diff --git a/ourl/public/index.html b/nginx/html/ourl/index.html similarity index 100% rename from ourl/public/index.html rename to nginx/html/ourl/index.html diff --git a/ourl/public/js/index.js b/nginx/html/ourl/js/index.js similarity index 100% rename from ourl/public/js/index.js rename to nginx/html/ourl/js/index.js diff --git a/ourl/urls.sql b/urls.sql similarity index 100% rename from ourl/urls.sql rename to urls.sql