Rest API Framework

Introduction

Glife Rest API Framework is a boilerplate in which all the libraries and other frameworks we talked about in the last sections (NextJS, MongoDB driver, WebSocket are all well pre-configured and ready to be used on both Front-end and Back-end.


Featuring

MongoDB

  • Using Native NodeJS Driver for full-speed query control
  • Model-based for data type checking and validation with customizable validators
  • Ready-to-use and customizable ODMs (Object Document Mapper) to fit your need
  • Built-in Data Loaders to optimize relationship resolving

WebSocket

  • Using NodeJS Native WebSocket package for speed control
  • Mimicking Socket.io event-based WebSocketServer Class
  • Support horizontal scaling (multiple servers, load-balancer) with RedisEventEmitter
  • Built-in connections alive checking (Ping/Pong)

NextJS

  • Automatically generate REST API Endpoint based on MongoDB Models
  • TailwindCSS integrated
  • Well-tested Form UI Components with React Hook Forms for validation
  • All needed React Hooks for server communication (useQuery, useMutation, useSubscription)

Folder Structure

Below here is the basic folder structure of Glife REST API Framework when you first install it

├── src/
│   ├── pages/
│   │   ├── api/
│   │   │   └── [...params].js
│   │   ├── index.js
│   │   └── login.js
│   ├── components/
│   │   ├── component1.js
│   │   ├── component2.js
│   │   └── component3.js
│   ├── hooks/
│   │   ├── useQuery.js
│   │   ├── useMutation.js
│   │   └── useSubscription.js
│   └── styles/
│       └── global.css
├── server/
│   ├── models/
│   │   ├── customTypes.js
│   │   └── User.js
│   ├── controllers/
│   │   ├── base.js
│   │   └── users.js
│   ├── utils/
│   │   ├── mongodb/
│   │   │   ├── buildMongoFilters.js
│   │   │   ├── buildMongoOrders.js
│   │   │   ├── validator.js
│   │   │   └── index.js
│   │   ├── auth.js
│   │   ├── common.js
│   │   ├── redisEventEmitter.js
│   │   ├── request.js
│   │   └── websocket.js
│   ├── tests/
│   └── index.js
├── .env
├── next.config.js
├── tailwind.config.js
├── package.json
└── README.md
  • src/: client-side stuff
  • src/pages/: the folder containing page-level components files in NextJS
  • src/pages/api/: special folder to create API endpoints on the server side with dynamic routes for automatic controllers mapping
  • src/components/: React small components for composition
  • src/hooks/: React custom hooks folder with built-in query, mutation and subscription hooks
  • src/styles/: extra styles folder
  • server/: server-side stuff
  • server/models: MongoDB ODMs folder to define models
  • server/controllers: REST API Controllers to handle request for specific paths
  • server/utils: All needed utility files for REST API Framework to work:
    • auth.js for basic authentication
    • common.js common utilities
    • mongodb for Mongodb parsing, validating utilities and Object Document Mapper
    • redisEventEmitter.js for WebSocket Server scaling
    • request.js for request handling and mapping endpoints to services
    • websocket.js for native WebSocket Server implementation
  • next.config.js: NextJS config file, see NextJS for more information
  • tailwind.config.js: TaillwindCSS config file, see TailwindCSS Configuration for more information
Previous
Event Emitter