Skip to content

Commit

Permalink
移植完毕
Browse files Browse the repository at this point in the history
新增 url 规格化(与原项目不同,不会自动添加协议)
新增特殊域名支持(如 '工业和信息化部.中国')
新增安装脚本(然而部署并没有变简单多少)
  • Loading branch information
xiaooloong committed Sep 10, 2016
1 parent c274c98 commit fbb10dc
Show file tree
Hide file tree
Showing 8 changed files with 901 additions and 35 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@
[Ourls][1] 是由 [takashiki][2] 实现的一个基于发号和 hashid 的短网址服务。
受这个项目的启发,将此工程移植,使用 [OpenResty][3] 实现。

### 待移植的功能:

- url 规格化
- 前置代理支持

### 待增加的特性:

- 工程目录优化,以 OpenResty 目录结构为准
- Cache 支持

### 安装方法:

- 安装 openresty rpm 包(或手动编译,建议使用 --prefix=/usr/local/openresty)
- 安装 libidn-devel 库(yum install libidn-devel)
- 将原 openresty/nginx/conf 目录备份
- 将本工程解压到 openresty/nginx/conf 目录
- 进入 openresty/nginx/conf/ourl 目录,复制 config.sample.lua 为 config.lua
- 修改 config.lua 中的数据库等配置
- 将本工程解压到 openresty/nginx/conf 目录,执行 `install.sh`
- 修改 ourl/config.lua 中的数据库等配置
- 恢复 urls.sql 至 mysql/mariadb 数据库
- 进入 openresty/nginx/conf/vhosts 目录,修改 ourl.conf 中的 server_name
- 进入 openresty/nginx/conf/lib/hashids 目录,执行 make 命令
- 进入 vhosts 目录,修改 ourl.conf 中的 server_name 为你自己的域名
- 启动 openresty

### 使用到的其他项目

- [leihog/hashids.lua][4]
- [APItools/router.lua][5]
- [golgote/neturl][6]
- [mah0x211/lua-idna][7]
- [hamishforbes/lua-resty-iputils][8]

[1]: https://github.com/takashiki/Ourls
[2]: https://github.com/takashiki
[3]: http://openresty.org/
[4]: https://github.com/leihog/hashids.lua
[5]: https://github.com/APItools/router.lua
[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
7 changes: 7 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#/bin/sh
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}
63 changes: 63 additions & 0 deletions lib/idna/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This Makefile is based on lua-zlib's Makefile. Thanks to the lua-zlib developers.
# Inform the location to intall the modules
PREFIX ?= /usr/local/openresty
LUAPATH ?= $(PREFIX)/lualib
LUACPATH ?= $(PREFIX)/lualib
INCDIR ?= -I$(PREFIX)/luajit/include/luajit-2.1
LIBDIR ?= -L$(PREFIX)/luajit/lib

# For Mac OS X: set the system version
MACOSX_VERSION = 10.4

CMOD = idna.so
OBJS = idna.o

LIBS = -lidn -lluajit-5.1
WARN = -Wall -pedantic

BSD_CFLAGS = -O2 -fPIC $(WARN) $(INCDIR) $(DEFS)
BSD_LDFLAGS = -O -shared -fPIC $(LIBDIR)

LNX_CFLAGS = -O2 -fPIC $(WARN) $(INCDIR) $(DEFS)
LNX_LDFLAGS = -O -shared -fPIC $(LIBDIR)

MAC_ENV = env MACOSX_DEPLOYMENT_TARGET='$(MACVER)'
MAC_CFLAGS = -O2 -fPIC -fno-common $(WARN) $(INCDIR) $(DEFS)
MAC_LDFLAGS = -bundle -undefined dynamic_lookup -fPIC $(LIBDIR)

CC = gcc
LD = $(MYENV) gcc
CFLAGS = $(MYCFLAGS)
LDFLAGS = $(MYLDFLAGS)

.PHONY: all clean install none linux bsd macosx

all:
@echo "Usage: $(MAKE) <platform>"
@echo " * linux"
@echo " * bsd"
@echo " * macosx"

install: $(CMOD)
cp $(CMOD) $(LUACPATH)

uninstall:
rm $(LUACPATH)/$(CMOD)

linux:
@$(MAKE) $(CMOD) MYCFLAGS="$(LNX_CFLAGS)" MYLDFLAGS="$(LNX_LDFLAGS)" INCDIR="$(INCDIR)" LIBDIR="$(LIBDIR)" DEFS="$(DEFS)"

bsd:
@$(MAKE) $(CMOD) MYCFLAGS="$(BSD_CFLAGS)" MYLDFLAGS="$(BSD_LDFLAGS)" INCDIR="$(INCDIR)" LIBDIR="$(LIBDIR)" DEFS="$(DEFS)"

macosx:
@$(MAKE) $(CMOD) MYCFLAGS="$(MAC_CFLAGS)" MYLDFLAGS="$(MAC_LDFLAGS)" MYENV="$(MAC_ENV)" INCDIR="$(INCDIR)" LIBDIR="$(LIBDIR)" DEFS="$(DEFS)"

clean:
rm -f $(OBJS) $(CMOD)

.c.o:
$(CC) -c $(CFLAGS) $(DEFS) $(INCDIR) -o $@ $<

$(CMOD): $(OBJS)
$(LD) $(LDFLAGS) $(LIBDIR) $(OBJS) $(LIBS) -o $@
92 changes: 92 additions & 0 deletions lib/idna/idna.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2014 Masatoshi Teruya. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* idna.c
* lua-idna
*
* Created by Masatoshi Teruya on 14/12/06.
*
*/

#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <stringprep.h>
#include <idna.h>

#define lstate_fn2tbl(L,k,v) do{ \
lua_pushstring(L,k); \
lua_pushcfunction(L,v); \
lua_rawset(L,-3); \
}while(0)

#define pdealloc(p) free((void*)p)

static int encode_lua( lua_State *L )
{
const char *src = luaL_checkstring( L, 1 );
char *dest = NULL;
int rc = idna_to_ascii_8z( src, &dest, 0 );

if( rc == IDNA_SUCCESS ){
lua_pushstring( L, dest );
pdealloc( dest );
return 1;
}

// got error
lua_pushnil( L );
lua_pushstring( L, idna_strerror( rc ) );

return 2;
}


static int decode_lua( lua_State *L )
{
const char *src = luaL_checkstring( L, 1 );
char *dest = NULL;
int rc = idna_to_unicode_8z8z( src, &dest, 0 );

if( rc == IDNA_SUCCESS ){
lua_pushstring( L, dest );
pdealloc( dest );
return 1;
}

// got error
lua_pushnil( L );
lua_pushstring( L, idna_strerror( rc ) );

return 2;
}


LUALIB_API int luaopen_idna( lua_State *L )
{
lua_createtable( L, 0, 2 );
lstate_fn2tbl( L, "encode", encode_lua );
lstate_fn2tbl( L, "decode", decode_lua );

return 1;
}


Loading

0 comments on commit fbb10dc

Please sign in to comment.