Code Monkey home page Code Monkey logo

sessions's Introduction

Example use case

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.

Quick start

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"
}

API documentation

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
* Support optional extra params:

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"
}

SessionObject

{
  "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
}

Expiration

Sessions removed automatically after 180 days inactivity by get requests

Tarantool backup / restore

Backup

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/

Restore

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

sessions's People

Contributors

0x1991 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

demonoid81

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.