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的更多相关文章

  1. 使用plv8+hashids生成短链接服务

    有写过一个集成npm plv8 以及shortid生成短链接id服务,实际上我们可以集成触发器自动生成url对应的短链接地址,hashids也是一个不错的选择. 以下是一个别人写的一个博客实现可以参考 ...

  2. openresty && hashids&& redis 生成短链接

    1. 原理     a. 从redis 获取需要表示的短链接的id( redis incr)     b. hashids 编码 id     c. openresty  conteent_by_lu ...

  3. Java生成短链接

    为什么要生成短链接 微博之所以要是使用短链接,主要是因为微博只允许发140 字,如果链接地址太长的话,那么发送的字数将大大减少. 那么使用短链接的作用有哪些呢?1.字数,2.统计,3.监控,4.过滤 ...

  4. java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串

    java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串 package com.zdz.test; im ...

  5. 新浪微博API生成短链接

    通过新浪微博API,生成短链接,支持一次性转多个长链接 什么是短链接 短链接,通俗来说,就是将长的URL网址,通过程序计算等方式,转换为简短的网址字符串. 短链接服务 国内各大微博都推出了自己的短链接 ...

  6. PHP生成短链接方法

    PHP生成短链接方法方法一:新浪提供了长链接转为短链接的API,可以把长链接转为 t.cn/xxx 这种格式的短链接. API: http://api.t.sina.com.cn/short_url/ ...

  7. 用PHP和Python生成短链接服务的字符串ID

    假设你想做一个像微博短链接那样的短链接服务,短链接服务生成的URL都非常短例如: http://t.cn/E70Piib, 我们应该都能想到链接中的E70Piib对应的就是存储长链接地址的数据记录的I ...

  8. 生成短链接的URL

    假设你想做一个像微博短链接那样的短链接服务,短链接服务生成的URL都非常短例如: http://t.cn/E70Piib, 我们应该都能想到链接中的E70Piib对应的就是存储长链接地址的数据记录的I ...

  9. plv8 + hashids 生成短连接id

    此文章是转载文章的一个学习,稍有改动 环境准备 plv8 环境 version: '3.6' services:  postgres:    image: dalongrong/plv8:2.3.12 ...

随机推荐

  1. tomcat8w.exe 运行 提示 指定的服务未安装 unable to open the service 'tomcat8'

    新下载的Tomcat8 解压版,解压缩完成后,双击tomcat8.exe出现个DOS样子的窗口一闪而过消失了,tomcat也没有启动成功.双击tomcat8w.exe 弹出个错误对话框,说“指定的服务 ...

  2. How to create own operator with python in mxnet?

    继承CustomOp 定义操作符,重写前向后向方法,此时可以通过_init__ 方法传递需要用到的参数 class LossLayer(mxnet.operator.CustomOp): def __ ...

  3. thinkphp %s %d %f

    %s -- 表示字段串%d -- 表示整形数字%f -- 表示浮点数 例子:$order_number = $m_ebay_order_numbers->where("ebay_ord ...

  4. HttpServletResponse输出的中文乱码

    HttpServletResponse输出有两种格式,一种是字符流,一种是字节流. 1.字符流 // 这句话的意思,是让浏览器用utf8来解析返回的数据,即设置客户端解析的编码 response.se ...

  5. nginx支持返回相对路径

    一.问题:http://192.168.72.4/bm-crm/  局域网可以访问[实际指向了192.168.80.1:8081/brm-crm] http://59.41.111.24:8280/b ...

  6. node.js express 4.x 安装指南 (找了很久呀,痛苦之路)

    问题出现这,版本的问题,还有express4.0以上的版本启动node app.js指令的变更,蛋疼.参考这个

  7. POJ 2057 The Lost Home 树形dp 难度:2

    The Lost House Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 2203   Accepted: 906 Des ...

  8. ReentrantReadWriteLock——写写互斥(二)

    "读写" ."写读"."写写"都是同步的.互斥的 1.Service.java package ReentrantReadWriteLock ...

  9. 亚马逊Kindle正式进入中国

    6月7日下午消息,亚马逊Kindle今天下午4点正式发售.其中,Kindle电子阅读器和Kindle Fire平板电脑同步销售.Paperwhite售价最低849元,Kindle Fire HD售价最 ...

  10. webbench源码学习-->命令行选项解析函数getopt和getopt_long函数

    对于webbench这个网站压力测试工具网上介绍的很多,有深度详解剖析的,对于背景就不在提了, 听说最多可以模拟3万个并发连接去测试网站的负载能力,这里主要是学习了一下它的源码,做点 笔记. 官方介绍 ...