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 ====== diff --git a/lib/resty/uuid.lua b/lib/resty/uuid.lua new file mode 100644 index 0000000..a3de92b --- /dev/null +++ b/lib/resty/uuid.lua @@ -0,0 +1,31 @@ +module("resty.uuid", package.seeall) + +_VERSION = '0.01' + +local ffi = require "ffi" +local ffi_new = ffi.new +local ffi_str = ffi.string + +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