-
Notifications
You must be signed in to change notification settings - Fork 388
Recipes Web
Roman Tsisyk edited this page Jul 8, 2015
·
1 revision
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.
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.
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
...
#!/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.
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.
tarantool> require('digest').md5_hex('password')
---
- 5f4dcc3b5aa765d61d8327deb882cf99
...
tarantool> require('digest').sha256_hex('password')
---
- 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
...
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
...
- C coding guidelines ↗
- Lua coding guidelines ↗
- Python coding guidelines ↗
- Maintainer's guide
- Debugging
Architecture
- Server architecture
- R tree index quick start and usage
- LuaJIT
- Vinyl
- Vinyl Architecture
- Vinyl Disk Layout
- Vinyl math
- Vinyl Cookbook
- Bullet1
- SQL
- Appserver modules
- Testing
- Performance
- Privileges and Access control
How To ...?
- ... configure build system
- ... add new fuzzers
- ... build RPM or Deb package using packpack
- ... calculate memory size
- ... debug core dump of stripped tarantool
- ... debug core from different OS
- ... debug fuzzer
- ... generate new bootstrap snapshot
- ... use Address Sanitizer
- ... collect a coredump
- ... generate luacov report for builtin module
- ... verify modified lua files via luacheck
- ... verify Lua files in third_party?
- ... rerun failed jobs
- ... update a third party repository
- Fix wrong decimal indexing after upgrade to 2.10.1
- Caveats when upgrading a cluster on Tarantool 1.6
- Fix illegal field type in a space format when upgrading to 2.10.4
Useful links