Skip to content

Commit

Permalink
add initial project setup (#3)
Browse files Browse the repository at this point in the history
* add basic fastify project with database connection.
* add health & users/:id route.
* add spec files & worfklow file for check.
  • Loading branch information
satyajitnayk authored Dec 23, 2023
1 parent c67c9fc commit 78ae647
Show file tree
Hide file tree
Showing 13 changed files with 8,326 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
POSTGRES_CONNECTION_STRING=postgres://postgres:1234@localhost/postgres
PORT=3000
22 changes: 22 additions & 0 deletions .github/worflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
on:
pull_request:
branches: ['**']
types: [synchronize, opened, reopened, ready_for_review]
push:
branches: [main]

jobs:
lint:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false # run this job only if the PR is not in a draft state
strategy:
matrix:
node: [ '18' ]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- run: npm i
- name: test
run: npm run test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
build
node_modules/
.idea/
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# order-management
A queue service to manage order using Fastify framework

## Update .env file

## run test
```shell
npm test
```

## run server
```shell
npm start
```
22 changes: 22 additions & 0 deletions config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const fastifyPostgres = require('@fastify/postgres');
const dotenv = require('dotenv');

dotenv.config();

const setupDatabase = (fastify) => {
fastify.register(fastifyPostgres, {
connectionString: process.env.POSTGRES_CONNECTION_STRING,
onConnect: async (client) => {
fastify.log.info('Connected to PostgreSQL database');
},
})
.after(() => {
// This block will run after the plugin is registered
})
.addHook('onError', (request, reply, error, done) => {
fastify.log.error(error.message);
done();
});
};

module.exports = {setupDatabase}
23 changes: 23 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Fastify = require('fastify');
const { setupDatabase } = require('./config/db.js');
const { userRoutes } = require('./routes/users.js');

const fastify = Fastify({
logger: true,
});

setupDatabase(fastify);
userRoutes(fastify);

fastify.get('/health', async (request, reply) => {
reply.type('application/json').code(200);
return { status: 'ok', uptime: process.uptime() };
});

fastify.listen({ port: process.env.PORT }, (err, address) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
fastify.log.info(`Server listening on ${address}`);
});
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
// preset: 'ts-jest', // enable if want to use typescript
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.js', '**/?(*.)+(spec|test).js'],
moduleFileExtensions: ['js'],
globals: {
// 'ts-jest': {
// tsconfig: 'path/to/your/tsconfig.json', // Specify your TypeScript configuration file
// },
},
};
Loading

0 comments on commit 78ae647

Please sign in to comment.