kemalelmizan / si-backend

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Startup Incubation Class - Backend Setup

Github Project Setup

  1. Create new repo with name <your_app>-api or <your_app>-backend

Heroku Project setup

  1. Create new app with name <your_app>-api or <your_app>-backend
  2. Connect to previously created github repo
  3. Download heroku CLI tools
  4. Install heroku postgres plugin

Local Project setup

  1. git clone https://github.com/kemalelmizan/<your_app>-api.git
  2. npm init
  3. Fill details
  4. npm i --save express
  5. npm i --save body-parser
  6. npm i --save pg
  7. npm i --save db-migrate
  8. npm i --save db-migrate-pg
  9. Add "start": "node index.js" in package.json scripts

Database migration setup

  1. npm i -g db-migrate
  2. db-migrate -v
  3. Add database.json config file
  4. DATABASE_URL=postgres://xxx db-migrate create <table_name> --sql-file -e production
  5. Repeat previous step for every tables
  6. Fill in SQL up and down script in ./migrations/sqls
  7. DATABASE_URL=postgres://xxx db-migrate up -e production

API setup

  1. Setup express routers
  2. Setup queries
  3. Generate random tokens by Array(64).fill(0).map(x => Math.random().toString(36).charAt(2)).join('')
  4. Setup API_TOKEN in heroku
  5. DATABASE_URL=postgres://xxx SSL=true npm start

Heroku CLI Commands

  1. heroku auth:login
  2. heroku auth:whoami
  3. heroku apps
  4. heroku addons --all
  5. heroku logs --tail -a si-backend
  6. to restart app: heroku restart -a si-backend

Local Docker Postgres Dev DB Setup

  1. Install and run docker
  2. docker -v
  3. docker run -p 5432:5432 --name si-backend -e POSTGRES_PASSWORD=sibackend -d postgres
  4. docker ps -a
  5. to start container: docker start si-backend
  6. Add dev environment in database.json
  7. db-migrate up -e dev
  8. DATABASE_URL=postgres://postgres:sibackend@localhost:5432/postgres npm start
  9. Create DB migration using local docker: db-migrate create <table_name> --sql-file -e dev

Generate User Specific Token (Postgres)

  1. db-migrate create users --sql-file -e dev
  2. Fill in SQL up and down script in ./migrations/sqls
  3. db-migrate down -e dev
  4. db-migrate up -e dev
  5. DATABASE_URL=postgres://postgres:sibackend@localhost:5432/postgres API_TOKEN=abc npm start

ERD

erd

Modular Development

  1. Single Responsibility Principle
  2. Add folders according to folder structure
  3. Restructure index.js into corresponding controllers and models
  4. Add controller/helper/response.js to wrap the response
  5. Add controller/helper/validation.js for validation libraries
  6. Add controller/access/admin.js, buyer.js and seller.js for access control matrix
  7. Add controller/auth/api.js for API_TOKEN validation
  8. Separate controller/auth/user.js and model/auth/user.js
  9. Add controller/auth/access.js to validate module access rights
  10. Set environment variables in postman for easy access, add host, API_TOKEN, user_email and access_token

Passport & Authentication

  1. npm install --save dotenv
  2. Create .env file
  3. Register Google API Credential to get Client ID GOOGLE_KEY and Client secret GOOGLE_SECRET here
https://console.developers.google.com/apis/credentials
  1. Add following values to .env file
SESSION_SECRET=abcd
PORT=8080
DATABASE_URL=postgres://postgres:sibackend@localhost:5432/postgres
SSL=false
API_TOKEN=abc
GOOGLE_KEY=xxx.apps.googleusercontent.com
GOOGLE_SECRET=xxx
CLIENT_ORIGIN=http://localhost:3000
  1. npm install --save passport
  2. npm install --save passport-google-oauth
  3. const googleAuth = passport.authenticate('google', { scope: ['profile'] }) in index.js
  4. Route triggered by the React client app.get('/google', googleAuth)
  5. Routes that are triggered by callbacks from OAuth providers once the user has authenticated successfully
app.get("/google/callback", googleAuth, (req, res) => {
  const io = req.app.get("io");
  const user = {
    name: req.user.displayName,
    photo: req.user.photos[0].value.replace(/sz=50/gi, "sz=250")
  };
  io.in(req.session.socketId).emit("google", user);
  res.end();
});
  1. Setup socket
app.use((req, res, next) => {
  req.session.socketId = req.query.socketId;
  next();
});
  1. Clone https://github.com/funador/react-auth-client for frontend test, change API_URL in config.js to http://localhost:8080

Module Specific Setup

Homework for next session

  • Finish CRUD for each modules (model, controller and route)
  • Create all requests template for CRUD in postman

To be covered in next sessions

  1. unit tests and test coverage
  2. third party libraries: file upload, nodemailer
  3. third party APIs: mailgun, user ID APIs https://github.com/sahat/hackathon-starter#obtaining-api-keys
  4. ngrok for public testing
  5. caching using redis
  6. timeout and circuit breaker handling
  7. security: penetration testing, injection, OWASP
  8. documentation: swagger

Links and docs

  1. Heroku postgres docs
  2. db-migrate docs
  3. Express API
  4. Express middleware
  5. Docker Cheat Sheet

About


Languages

Language:JavaScript 100.0%