-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Eomm/to-production
Third post: to production
- Loading branch information
Showing
17 changed files
with
837 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
NODE_ENV=development | ||
BASE_URL=http://localhost:3000 | ||
DISCORD_CLIENT_ID=12345678 | ||
DISCORD_SECRET=XXXXXXXXXXXXXXXXX | ||
DISCORD_SECRET=XXXXXXXXXXXXXXXXX | ||
DB_URI=mongodb+srv://<user>:<password>@playground.xxxxx.mongodb.net/<dbname>?retryWrites=true&w=majority |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,3 +104,4 @@ dist | |
.tern-port | ||
|
||
package-lock.json | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
import schema from './schema.mjs' | ||
import casual from 'casual' | ||
|
||
export default function api (fastify, opts, next) { | ||
fastify.setErrorHandler(function (error, request, reply) { | ||
reply.send(error) | ||
}) | ||
|
||
fastify.put('/users/:userId', { | ||
handler: createUser, | ||
schema: schema.createUser | ||
}) | ||
|
||
fastify.get('/users', { | ||
handler: searchUsers, | ||
schema: schema.searchUsers | ||
}) | ||
|
||
next() | ||
} | ||
|
||
async function createUser (request, reply) { | ||
const { userId } = request.params | ||
|
||
await this.mongo.client.db() | ||
.collection('Users') | ||
.updateOne( | ||
{ id: userId }, | ||
{ | ||
$set: fakeData(request.body), | ||
$push: { visits: new Date() }, | ||
$setOnInsert: { created: new Date() } | ||
}, | ||
{ upsert: true }) | ||
|
||
request.log.debug('Track user %s', userId) | ||
reply.code(201) | ||
return { userId } | ||
} | ||
|
||
async function searchUsers (request, reply) { | ||
const { offset, limit } = request.query | ||
|
||
const query = await this.mongo.client.db().collection('Users') | ||
.find({}, { projection: { _id: 0, visits: { $slice: -1 } } }) | ||
.sort({ 'visits.$0': 1 }) | ||
.skip(offset) | ||
.limit(limit) | ||
|
||
const total = await query.count() | ||
const rows = await query.toArray() | ||
|
||
return { rows, total } | ||
} | ||
|
||
/** | ||
* The GDPR don't let us to store personal information | ||
* without adding burden to this tutorial, so we fake the | ||
* data | ||
*/ | ||
function fakeData (user) { | ||
user.id = casual.integer(0, 401404362695639040) | ||
user.username = casual.username | ||
user.email = casual.email | ||
return user | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
export default { | ||
createUser: { | ||
params: { | ||
userId: { type: 'integer' } | ||
}, | ||
body: { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
id: { type: 'integer' }, | ||
username: { type: 'string', maxLength: 32 }, | ||
avatar: { type: 'string', maxLength: 50 }, | ||
discriminator: { type: 'string', maxLength: 5 }, | ||
email: { type: 'string', format: 'email' }, | ||
verified: { type: 'boolean' }, | ||
locale: { type: 'string', maxLength: 2 } | ||
}, | ||
required: ['id', 'username'] | ||
}, | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
userId: { type: 'integer' } | ||
} | ||
} | ||
} | ||
}, | ||
searchUsers: { | ||
query: { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
offset: { type: 'integer', minimum: 0, default: 0 }, | ||
limit: { type: 'integer', minimum: 1, maximum: 40, default: 10 } | ||
} | ||
}, | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
total: { type: 'integer' }, | ||
rows: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
username: { type: 'string', maxLength: 32 }, | ||
visits: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
format: 'date-time' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
|
||
import Fastify from 'fastify' | ||
import app from './app.mjs' | ||
|
||
const server = Fastify({ | ||
logger: true, | ||
pluginTimeout: 10000 | ||
}) | ||
|
||
server.register(app) | ||
|
||
server.listen(process.env.PORT || 3000, '0.0.0.0', (err) => { | ||
if (err) { | ||
server.log.error(err) | ||
process.exit(1) | ||
} | ||
}) | ||
import configurationLoader from './utils/configuration-loader.mjs' | ||
import appFactory from './app.mjs' | ||
|
||
;(async function () { | ||
const config = await configurationLoader() | ||
const server = appFactory(config) | ||
server.listen(config.PORT || 3000, '0.0.0.0', (err) => { | ||
if (err) { | ||
server.log.error(err) | ||
process.exit(1) | ||
} | ||
}) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
import dotenv from 'dotenv' | ||
|
||
export default async function load () { | ||
const configuration = dotenv.config() | ||
|
||
/** | ||
* This function is async because we could | ||
* load some KEYS from external services (like AWS Secrets Manager) | ||
* in future | ||
*/ | ||
|
||
configuration.fastify = { | ||
logger: configuration.NODE_ENV !== 'production' | ||
} | ||
|
||
return configuration | ||
} |
Oops, something went wrong.