Recipes Web - AnaNek/tarantool GitHub Wiki

Web

How to get data by HTTP

http_client = require('http.client')
json = require('json')
r = http_client.get('http://api.openweathermap.org/data/2.5/weather?q=Oakland,us')
if r.status ~= 200 then
    print('Failed to get weather forecast ', r.reason)
    return
end
data = json.decode(r.body)
print('Oakland wind speed: ', data.wind.speed)

This snipplet requires http rock.

How to send data by HTTP

http_client = require('http.client')
json = require('json')
data = json.encode({ Key = 'Value'})
headers = { Token = 'xxxx', ['X-Secret-Value'] = 42 }
r = http_client.post('http://localhost:8081', data, { headers = headers})
if r.status == 200 then
    print 'Success'
end

This snipplet requires http rock.

How to split URI into components

Use built-in :func:`uri.decode`:

tarantool> uri = require('uri')
tarantool> uri.parse("scheme://login:password@host:service:/path1/path2/path3?q1=v1&q2=v2#fragment")
---
- password: password
  path: /path1/path2/path3
  scheme: scheme
  login: login
  query: q1=v1&q2=v2
  service: service
  fragment: fragment
  host: host
  ...

How to turn Tarantol into a web-server

#!/usr/bin/env tarantool

local function handler(self)
    return self:render{ json = { ['Your-IP-Is'] = self.peer.host } }
end

local server = require('http.server').new(nil, 8080) -- listen *:8080
server:route({ path = '/' }, handler)
server:start()
-- connect to localhost:8080 and see json

This snipplet requires http rock. See also our nginx-tarantool-upstream module.

How to generate HTML pages with data

A http rock has pretty simple template engine which allow to execute regular Lua code inside text blocks (like PHP). You don't need to learn extra languages to write templates.

Hashbang script:

#!/usr/bin/env tarantool

local function handler(self)
local fruits = { 'Apple', 'Orange', 'Grapefruit', 'Banana'}
    return self:render{ fruits = fruits }
end

local server = require('http.server').new(nil, 8080) -- nil means '*'
server:route({ path = '/', file = 'index.html.lua' }, handler)
server:start()

A templates/index.html.lua file:

<html>
<body>
    <table border="1">
        % for i,v in pairs(fruits) do
        <tr>
            <td><%= i %></td>
            <td><%= v %></td>
        </tr>
        % end
    </table>
</body>
</html>

Produced result:

1 Apple
2 Orange
3 Grapefruit
4 Banana

Please consult http documentation for more details.

How to calculate md5/sha1/etc.

tarantool> require('digest').md5_hex('password')
---
- 5f4dcc3b5aa765d61d8327deb882cf99
...
tarantool> require('digest').sha256_hex('password')
---
- 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
...

How to generate unique session identifier

Use :func:`digest.urandom()` to get random bytes.

tarantool> digest.base64_encode(digest.urandom(16))
---
- pnZJK8QPupOAtAEA/1UmiA==
...

As an alternative it is possible to use UUID:

 tarantool> require('uuid').str()
 ---
 - fb871f8a-cbf0-4fd7-8575-b3a500137b71
...
⚠️ **GitHub.com Fallback** ⚠️