From db9e6688bd4c6aabc3415127536b967a4be64e45 Mon Sep 17 00:00:00 2001 From: smallfish Date: Sun, 21 Oct 2012 17:29:41 +0800 Subject: [PATCH 1/3] add resty.uuid module, use LuaJIT ffi wrapper libuuid. --- lib/resty/uuid.lua | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/resty/uuid.lua diff --git a/lib/resty/uuid.lua b/lib/resty/uuid.lua new file mode 100644 index 0000000..c357654 --- /dev/null +++ b/lib/resty/uuid.lua @@ -0,0 +1,32 @@ +module("resty.uuid", package.seeall) + +_VERSION = '0.01' + +local ffi = require "ffi" +local ffi_new = ffi.new +local ffi_str = ffi.string +local C = ffi.C + +ffi.cdef[[ + typedef unsigned char uuid_t[16]; + void uuid_generate(uuid_t out); + void uuid_unparse(const uuid_t uu, char *out); +]] + +local libuuid = ffi.load("libuuid") + +function generate() + if libuuid then + local uuid = ffi_new("uuid_t") + local result = ffi_new("char[36]") + libuuid.uuid_generate(uuid) + libuuid.uuid_unparse(uuid, result) + return ffi_str(result) + end +end + +-- to prevent use of casual module global variables +getmetatable(resty.uuid).__newindex = function (table, key, val) + error('attempt to write to undeclared variable "' .. key .. '": ' + .. debug.traceback()) +end From 5767001e6bfdf6d15401cf63ee90db65ca753602 Mon Sep 17 00:00:00 2001 From: smallfish Date: Sun, 21 Oct 2012 17:31:26 +0800 Subject: [PATCH 2/3] update README with uuid --- README.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.markdown b/README.markdown index 6f7e6f6..9d3c281 100644 --- a/README.markdown +++ b/README.markdown @@ -148,6 +148,10 @@ Synopsis aes_256_cbc_sha512x5:decrypt(encrypted)) + -- uuid test + local resty_uuid = require "resty.uuid" + local uuid = resty_uuid:generate() + ngx.say("UUID: ", uuid) Author ====== From 119b8a932ff67805c59954fe9767f050dc8c7e46 Mon Sep 17 00:00:00 2001 From: smallfish Date: Sun, 21 Oct 2012 17:40:31 +0800 Subject: [PATCH 3/3] remove uuid nouse vars --- lib/resty/uuid.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/resty/uuid.lua b/lib/resty/uuid.lua index c357654..a3de92b 100644 --- a/lib/resty/uuid.lua +++ b/lib/resty/uuid.lua @@ -5,7 +5,6 @@ _VERSION = '0.01' local ffi = require "ffi" local ffi_new = ffi.new local ffi_str = ffi.string -local C = ffi.C ffi.cdef[[ typedef unsigned char uuid_t[16];