Skip to content

Instantly share code, notes, and snippets.

@rtsisyk
Created July 24, 2014 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtsisyk/98a8f57aaa1152a86873 to your computer and use it in GitHub Desktop.
Save rtsisyk/98a8f57aaa1152a86873 to your computer and use it in GitHub Desktop.
Tarantool HTTP Example
#!/usr/bin/env tarantool
box.cfg{}
space = box.space.data
if not space then
space = box.schema.create_space('data')
space:create_index('primary', { parts = {1, 'STR'} })
end
local function handler(self)
local host = self.req.peer.host
local response = {
host = host;
counter = space:inc(host);
}
self:render({ json = response })
end
require('http.server').new('127.0.0.1', 8080)
:route({ path = '/' }, handler)
:start()
@Elzor
Copy link

Elzor commented Nov 30, 2017

Actually there is issue with your code. You forgot 'return'.
Correct handler function looks like:

local function handler(self)
    local host = self.req.peer.host
    local response = {
        host = host;
        counter = space:inc(host);
    }
    return self:render({ json = response })
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment