<commit_before>## A simple chat server

### How to use it

#### Dependencies
- postgresql >=9.3
- redis >=2.8

#### Run it
1. change `db/dbconf.yml` to have the correct credentials.
2. run `goose up` [1] to run all migrations.
3. create a configuration file in `cmd/` (see `development.json` for example).
4. run `godep restore` [2] to install all golang dependencies.
5. run `make local` to run the server.

### APIs

#### `POST /api/register`
- params:
  - first_name: required
  - last_name: required
  - username: required
  - password: <PASSWORD>
  - email: required
  - phone: required

- json:

```json
{
    'status': 'success',
    'data': [
        {
            'email': '<EMAIL>',
            'first_name': 'Bob',
            'last_name': 'Bobson',
            'phone_number': '0123456789',
            'token': {
                'access_key_id': '<KEY>',
                'created_at': '2015-09-03T02:08:08.558583633Z',
                'expires_at': '2015-10-03T02:08:08.558584401Z',
                'is_refreshable': true,
                'modified_at': '2015-09-03T02:08:08.5585851Z',
                'refresh_token': '<KEY>',
                'secret_access_key': '<KEY>'
            },
            'username': 'bob'
        }
    ],
    'error': {
        'code': 200,
        'errors': null,
        'message': ''
    },
    'page': 1,
    'per_page': 10,
    'current_page': 1
}


```

#### `POST /api/auth_token`
- params:
  - username: required
  - password: <PASSWORD>

- json:

```json
{
    'status': 'success',
    'data': [
        {
            'access_key_id': '<KEY>',
            'created_at': '2015-09-03T02:15:31.533436161Z',
            'expires_at': '2015-10-03T02:15:31.533437208Z',
            'is_refreshable': true,
            'modified_at': '2015-09-03T02:15:31.533438047Z',
            'refresh_token': '<KEY>',
            'secret_access_key': '<KEY>'
        }
    ],
    'error': {
        'code': 200,
        'errors': null,
        'message': ''
    },
    'page': 1,
    'per_page': 10,
    'current_page': 1
}
```

#### `DELETE /api/auth_token`
- authentication is required

#### `GET /api/inbox`
- authentication is required

- json

```json

{
    'status': 'success',
    'data': [
        {
            'author_username': 'bob',
            'created_at': '2015-09-02T15:49:02.996617Z',
            'latest_message': 'hello'
        }
    ],
    'error': {
        'code': 200,
        'errors': None,
        'message': ''
    },
    'page': 1,
    'per_page': 10,
    'current_page': 1
}
```

#### `GET /api/thread/[username]`
- authentication is required

- json

```json
{
    'status': 'success'
    'data': [
        {
            'author': 'bob',
            'created_at': '2015-09-02T15:49:02.994339Z',
            'message': 'hello',
            'message_type': 0,
            'recipient': 'alice'
        },
        {
            'author': 'alice',
            'created_at': '2015-09-02T15:40:02.994339Z',
            'message': 'Hi bob',
            'message_type': 0,
            'recipient': 'bob'
        },
    ],
    'error': {
        'code': 200,
        'errors': null,
        'message': ''
    },
    'page': 1,
    'per_page': 10,
    'current_page': 2
}
```

#### websocket
- endpoint: `/api/ws`
- authentication is required

### Errors

```json
{
    'status': 'fail',
    'data': [],
    'error': {
        'code': 400,<commit_msg>Add ws packet protocol to README<commit_after>## A simple chat server

### How to use it

#### Dependencies
- postgresql >=9.3
- redis >=2.8

#### Run it
1. change `db/dbconf.yml` to have the correct credentials.
2. run `goose up` [1] to run all migrations.
3. create a configuration file in `cmd/` (see `development.json` for example).
4. run `godep restore` [2] to install all golang dependencies.
5. run `make local` to run the server.

### APIs

#### `POST /api/register`
- params:
  - first_name: required
  - last_name: required
  - username: required
  - password: <PASSWORD>
  - email: required
  - phone: required

- json:

```json
{
    'status': 'success',
    'data': [
        {
            'email': '<EMAIL>',
            'first_name': 'Bob',
            'last_name': 'Bobson',
            'phone_number': '0123456789',
            'token': {
                'access_key_id': '<KEY>',
                'created_at': '2015-09-03T02:08:08.558583633Z',
                'expires_at': '2015-10-03T02:08:08.558584401Z',
                'is_refreshable': true,
                'modified_at': '2015-09-03T02:08:08.5585851Z',
                'refresh_token': 'FAIMPUSXVQJAMRGBSSUJHIRRZC3ROLLOLPYT5VQOR5K6OJKFY62Q',
                'secret_access_key': '<KEY>'
            },
            'username': 'bob'
        }
    ],
    'error': {
        'code': 200,
        'errors': null,
        'message': ''
    },
    'page': 1,
    'per_page': 10,
    'current_page': 1
}


```

#### `POST /api/auth_token`
- params:
  - username: required
  - password: <PASSWORD>

- json:

```json
{
    'status': 'success',
    'data': [
        {
            'access_key_id': '<KEY>',
            'created_at': '2015-09-03T02:15:31.533436161Z',
            'expires_at': '2015-10-03T02:15:31.533437208Z',
            'is_refreshable': true,
            'modified_at': '2015-09-03T02:15:31.533438047Z',
            'refresh_token': 'N547NO<PASSWORD>VF<PASSWORD>BEZKE5FSLOB4<PASSWORD>VTO<PASSWORD>HT<PASSWORD>',
            'secret_access_key': '<KEY>'
        }
    ],
    'error': {
        'code': 200,
        'errors': null,
        'message': ''
    },
    'page': 1,
    'per_page': 10,
    'current_page': 1
}
```

#### `DELETE /api/auth_token`
- authentication is required

#### `GET /api/inbox`
- authentication is required

- json

```json

{
    'status': 'success',
    'data': [
        {
            'author_username': 'bob',
            'created_at': '2015-09-02T15:49:02.996617Z',
            'latest_message': 'hello'
        }
    ],
    'error': {
        'code': 200,
        'errors': None,
        'message': ''
    },
    'page': 1,
    'per_page': 10,
    'current_page': 1
}
```

#### `GET /api/thread/[username]`
- authentication is required

- json

```json
{
    'status': 'success'
    'data': [
        {
            'author': 'bob',
            'created_at': '2015-09-02T15:49:02.994339Z',
            'message': 'hello',
            'message_type': 0,
            'recipient': 'alice'
        },
        {
            'author': 'alice',
            'created_at': '2015-09-02T15:40:02.994339Z',
            'message': 'Hi bob',
            'message_type': 0,
            'recipient': 'bob'
        },
    ],
    'error': {
        'code': 200,
        'errors': null,
        'message': ''
    },
    'page': 1,
    'per_page': 10,
    'current_page': 2
}
```

#### websocket
- endpoint: `/api/ws`
- authentication is required

- send a packet:

```json
{
    'author': 'bob',
    'recipient': 'alice',
    'message': 'hello'
}

```

- receive a packet

```json
{
    'author': 'bob',
    'recipient': 'alice',
    'message': 'hello',
    'message_type': 0,
    'created_at':'2015-09-03T03:13:12.817650473Z'
}

```

### Errors

```json
{
    'status': 'fail',
    'data': [],
    'error': {
        'code': 400,
{'fraction_non_alphanumeric': 0.18352681039248203, 'fraction_numerical': 0.07393587617468214, 'mean_word_length': 1.9134460547504026, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6730594', 'n_tokens_mistral': 2939, 'n_tokens_neox': 2571, 'n_words': 523}