Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add resty.uuid wrapper #7

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
======
Expand Down
31 changes: 31 additions & 0 deletions lib/resty/uuid.lua
Original file line number Diff line number Diff line change
@@ -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