You have the mobile application or web-site, and you need to implement user profiling system. So, for that you create users database, and next step is realise users-sesions logic. This one repository implement users-sesions logic.
Up docker-compose:
docker-compose -f docker-compose.yml -f dc.sample.yml up -d
Already done! Just use it like this:
New session: http://localhost/new?user_id=1&ip=192.168.1.1
{
"ip": "192.168.1.1",
"extra": null,
"user_id": 1,
"create": 1464909151,
"activity": 1464909151,
"token": "61494096-9af7-49ba-9fb4-e89c0811e6d3"
}
Get session: http://localhost/get?token=61494096-9af7-49ba-9fb4-e89c0811e6d3
{
"ip": "192.168.1.1",
"extra": null,
"user_id": 1,
"create": 1464909151,
"activity": 1464909999,
"token": "61494096-9af7-49ba-9fb4-e89c0811e6d3"
}
Each response can be one of two variants:
- success:
{ status: 'success', data: array | object }
- error:
{ status: 'error', code: string, message: string }
Method | Params | Description | Success data | Error codes |
---|---|---|---|---|
new* | user_id ip |
Create new session | SessionObject | invalid_user_id invalid_ip |
get* | token ip optional |
Get existing session | SessionObject | token_not_found |
del | token |
Del existing session | SessionObject | token_not_found |
user | id |
Get all sessions for the user | SessionObject[] | invalid_user_id |
ip | ip |
Get all sessions by IP | SessionObject[] | invalid_ip |
new
and get
methods allow inserting extra data for the session. Example for request by PHP:
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL,
'content' => http_build_query(['device'=>'Nexus 5', 'os'=> 'Android'])
]
]);
echo file_get_contents('http://localhost/get?token=61494096-9af7-49ba-9fb4-e89c0811e6d3', FALSE, $context)
response:
"status": "success",
"data": {
"ip": "192.168.1.1",
"extra": {
"os": "Android",
"device": "Nexus 5"
},
"user_id": 1,
"create": 1464909151,
"activity": 1465564426,
"token": "61494096-9af7-49ba-9fb4-e89c0811e6d3"
}
{
"ip": string // IP-address of user,
"extra": object | null, // extra data
"user_id": int,
"create": int, // create session timestamp
"activity": int, // last timestamp of get request for current session
"token": string
}
Sessions removed automatically after 180 days inactivity by get
requests
Creating tarantool snapshot:
docker exec -it sessions_tarantool_1 /bin/bash
tarantool
require('console').connect('localhost:3301')
box.snapshot()
Copying snapshot:
Inside the container in the directory /data
file appeared ...snap
, if more than one - you need a last. Clean /share
and copy:
rm -rf /share/* && cp /data/...snap /share/
Stop container
docker stop sessions_tarantool_1
Remove all from /data, and copy from /share:
docker run --volumes-from sessions_tarantool-data_1 --rm -v $PWD/share:/share busybox sh -c "rm -rf /data/* && cp /share/* /data/"
Run if needed
docker start sessions_tarantool_1