GithubHelp home page GithubHelp logo

jherax / hapi-mongo-swagger Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 460 KB

Hapi + Swagger, GraphQL + JWT, MongoDB + Winston Logs

JavaScript 10.91% TypeScript 87.03% Shell 0.79% Dockerfile 0.52% HTML 0.75%

hapi-mongo-swagger's Introduction

Hapi + Swagger, GraphQL + JWT, MongoDB + Winston

Hapi Node server with Swagger, MongoDB database, and GraphQL API with Winston logs.

This backend application consists of 2 APIs.

  1. GraphQL API: JWT + Mongoose (Schemas: Painting, User)
  2. REST API: Express + Mongoose + Swagger (Controllers: Painting, HealthCheck)

Environment

Make sure to set the env variables. For local environment you can create a .env file with the following environment variables:

APP_HOST=localhost
APP_PORT=4000
# dev | prod | qa
NODE_ENV=dev

# https://www.npmjs.com/package/jsonwebtoken
JWT_PRIVATE_KEY=your_own_secret_key
JWT_EXPIRY_TIME=24h

Also you may change the log level by setting the env variable LOG_LEVEL to the available values error | warn | info | debug. By default LOG_LEVEL is set to info.

💡 This application uses winston as logging library with support for multiple transports. A transport is essentially a storage device for your logs. Default transports are Console and DailyRotateFile

ESLint disables the use of console.* methods, use the following methods instead:

logger.error('Message to the logger');
logger.warn('Message to the logger');
logger.info('Message to the logger');
logger.debug('Message to the logger');

Prepare the app

After cloning the repository, run the following commands only once:

npm install
npm run prepare

Running the server

Just run the command

npm run mongod
npm run dev:server

Swagger

This project uses the Hapi Swagger plugin that generates API documentation following this approach: Document your API while defining your routes. It adds a page on your website with the path /documentation, where the full URL would be localhost:4000/documentation. Alternatively, you can use hapi-swaggered-ui which follows the same approach, or if you want to create a custom documentation site, see swagger-ui.

💡 Joi is recommended as a schema and data validator for routes, and also works well together with «hapi-swaggered» and «hapi-swagger».

Apollo GraphQL

The path that resolves Apollo queries is: /graphql. The following queries require the Authorization header with a valid JWT token:

  • getPaintings
  • getPaintingById
  • createPainting
  • deletePainting
  • editPainting

You can generate a valid JWT token by executing the query login, and retrieve the value from the property "jwtoken" in the response. See an example of generating a valid token for an existing user here: /user.auth.http

In the .http files that require Authorization you will see the @auth_token variable which points to the environment JWT_AUTH_TOKEN, so you will need to create that variable in your .env file in order to get those .http files working properly.

# .env
JWT_AUTH_TOKEN=some_valid_token_generated_by_login

💡 When running in non-production environment, the /graphql and /sandbox paths are enabled to run an Apollo Sandbox environment where we can now execute GraphQL queries on our own server.

⚠️ Running the local server requires docker, if docker has not been configured, then follow next steps: docker 👇

Docker

MongoDB is loaded as a docker container, sou you need to make sure to create a .env file with the following environment variables:

DB_ROOT_USERNAME=root
DB_ROOT_PASSWORD=root
DB_INIT_USERNAME=appuser
DB_INIT_PASSWORD=symfony
DB_NAME=vantage
DB_HOST=localhost
DB_PORT=27012
ME_PORT=3001

In order to run MongoDB, you need to mount the docker container, and start the mongod service. To do that just run the command:

npm run mongod

After that, let's make sure the container is running:

docker ps -a

You should have a result like this:

CONTAINER ID   IMAGE         COMMAND                  CREATED      STATUS        PORTS                     NAMES
db936f592d4d   mongo:6.0.8   "docker-entrypoint.s…"   1 hour ago   Up 2 minute   0.0.0.0:27012->27017/tcp  mongodb_6
6f518f569c4b   mongo-express "tini -- /docker-ent…"   1 hour ago   Up 2 minutes  0.0.0.0:3001->8081/tcp    mongo-express

To open mongo-express, the web-based MongoDB admin interface, go to the browser and navigate to http://localhost:3001/, using credentials in the env variables DB_INIT_USERNAME and DB_INIT_PASSWORD

If you want to open the terminal to run commands on the container, just run the following command for the specific container:

docker exec -it mongodb_6 bash
root@mongodb_host:/#

Standard-version

standard-version is a utility for versioning using semver and CHANGELOG generation powered by Conventional Commits. You need to have a starting point to append the CHANGELOG and other versions to. The first time simply run:

npm run release -- --first-release

Then, for a new release, just run:

npm run release

⚠️ standard-version is deprecated. It is recommended to use an alternative like release-please.

For more details, please visit the Github site standard-version

Husky

Husky supports all Git hooks. You can use it to lint your commit messages, run tests, lint code, etc... when you commit or push.

See more in the Github site: husky

Configure

To set up husky in a new project, follow these steps:

npm install husky -D

Edit package.json > prepare script, and add as follow:

"prepare": "husky install"

Then, run the command:

npm run prepare

To add a hook:

npx husky add .husky/pre-commit "eslint and prettier commands"
npx husky add .husky/post-commit "git update-index -g"
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit ${1}'

See: conventional-changelog/commitlint.

hapi-mongo-swagger's People

Contributors

jherax avatar

Watchers

 avatar  avatar  avatar

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.