openresty 使用cuid 类库生成短链接id
cuid 是一个不错的id 生成算发,类似的有shortid 、hashid
演示使用lua 包集成openresty 做测试
使用docker-compose 运行
dockerfile
FROM openresty/openresty:alpine-fat
RUN apk add --no-cache curl perl
RUN /usr/local/openresty/luajit/bin/luarocks install cuid
EXPOSE 80
COPY nginx.conf usr/local/openresty/nginx/conf/
docker-compose file
version: "3"
services:
id:
build: .
image: dalongrong/cuid-openresty
ports:
- "8080:80"
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
location /id {
default_type text/html;
content_by_lua_block {
local cuid = require("cuid")
local id = cuid.generate ( )
local slug = cuid.slug ( )
ngx.say("<div> shortid "..slug.."</div>".."<div>long id"..id.."</div>")
}
}
location /alert {
default_type text/html;
content_by_lua_block{
ngx.say([[<script>alert("error")</script>]])
}
}
location /ip {
default_type text/html;
content_by_lua_block{
ngx.say(ngx.var.remote_addr)
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
构建&&运行
- 构建
docker-compose build
- 运行
docker-compose up -d
- 访问
open http://localhost:8080/id
- 效果

参考资料
https://hub.docker.com/r/openresty/openresty/
https://github.com/rongfengliang/openresty-cuid-demo
https://github.com/marcoonroad/cuid
https://www.npmjs.com/package/cuid
openresty 使用cuid 类库生成短链接id的更多相关文章
- 使用plv8+hashids生成短链接服务
有写过一个集成npm plv8 以及shortid生成短链接id服务,实际上我们可以集成触发器自动生成url对应的短链接地址,hashids也是一个不错的选择. 以下是一个别人写的一个博客实现可以参考 ...
- openresty && hashids&& redis 生成短链接
1. 原理 a. 从redis 获取需要表示的短链接的id( redis incr) b. hashids 编码 id c. openresty conteent_by_lu ...
- Java生成短链接
为什么要生成短链接 微博之所以要是使用短链接,主要是因为微博只允许发140 字,如果链接地址太长的话,那么发送的字数将大大减少. 那么使用短链接的作用有哪些呢?1.字数,2.统计,3.监控,4.过滤 ...
- java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串
java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串 package com.zdz.test; im ...
- 新浪微博API生成短链接
通过新浪微博API,生成短链接,支持一次性转多个长链接 什么是短链接 短链接,通俗来说,就是将长的URL网址,通过程序计算等方式,转换为简短的网址字符串. 短链接服务 国内各大微博都推出了自己的短链接 ...
- PHP生成短链接方法
PHP生成短链接方法方法一:新浪提供了长链接转为短链接的API,可以把长链接转为 t.cn/xxx 这种格式的短链接. API: http://api.t.sina.com.cn/short_url/ ...
- 用PHP和Python生成短链接服务的字符串ID
假设你想做一个像微博短链接那样的短链接服务,短链接服务生成的URL都非常短例如: http://t.cn/E70Piib, 我们应该都能想到链接中的E70Piib对应的就是存储长链接地址的数据记录的I ...
- 生成短链接的URL
假设你想做一个像微博短链接那样的短链接服务,短链接服务生成的URL都非常短例如: http://t.cn/E70Piib, 我们应该都能想到链接中的E70Piib对应的就是存储长链接地址的数据记录的I ...
- plv8 + hashids 生成短连接id
此文章是转载文章的一个学习,稍有改动 环境准备 plv8 环境 version: '3.6' services: postgres: image: dalongrong/plv8:2.3.12 ...
随机推荐
- Asp.Net中的sessionState设置
在web.config中有sessionState的节点配置,sessionState共有4中模式:off,inProc,StateServer,SqlServer. 1. off模式 关闭模式,如果 ...
- Codeforces Round #449 (Div. 1)C - Willem, Chtholly and Seniorious
ODT(主要特征就是推平一段区间) 其实就是用set来维护三元组,因为数据随机所以可以证明复杂度不超过O(NlogN),其他的都是暴力维护 主要操作是split,把区间分成两个,用lowerbound ...
- fzu1901 kmp
For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...
- CoderForce 148D-Bag of mice (概率DP求概率)
题目大意:美女与野兽在玩画鸽子的游戏.鸽子在用黑布遮住的笼子里,白色的有w只,黑色的有b只,每次拿出一只作画,谁先画到白色的鸽子谁就赢.美女首先画,因为野兽太丑,它每次画的时候都会吓跑一只鸽子,所有出 ...
- sql server数据库中char、nchar、varchar、nvarchar的选择
在数据库中,字符型的数据是最多的,可以占到整个数据库的80%以上.为此正确处理字符型的数据,对于提高数据库的性能有很大的作用. 在字符型数据中,用的最多的就是Char与Varchar两种类型.前面的是 ...
- DB2开发系列之二——SQL过程
1.SQL 过程的结构 1)SQL过程的结构 CREATE PROCEDURE proc_name IN, OUT, INOUT parameters optional clauses S ...
- Linux C ftruncate 函数清空文件注意事项(要使用 lseek 重置偏移量)
转载:http://blog.csdn.net/a_ran/article/details/43562429 int truncate(const char *path, off_t length); ...
- 深入了解 Java-Netty高性能高并发理解
https://www.jianshu.com/p/ac7fb5c2640f 一丶 Netty基础入门 Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为 ...
- kill qz _e epi,eu,ex,exo out3
1● epi 在~上,在~周围,在~后面 2● eu 好,优秀 3● ex 出,出去 4● exo 在外面 的,外部的
- Python gui编程pyQt5安装步骤t
Python gui编程pyQt5安装步骤 pip install PyQt5 Pip3 install PyQt5 https://riverbankco ...