Skip to content

Umbrella API for all STEM related services in STEM Homework Helpers & Students

License

Notifications You must be signed in to change notification settings

stem-discord/stem-help-backend

Repository files navigation

banner

file count badge Dev badge

ci badge Coverage Status

Enviroment file, do

npm run help:env

Install packages

yarn install --frozen-lockfile

test
  env - used for options when testing. Is not loaded to process.env
.env - used for development and production
.env.test - used for testing. Loaded by the test

If you have any questions on running this ask me in discord.gg/stem

Starting the project for yourself

git clone https://github.com/stem-discord/stem-help-backend.git

If there is no .env file in root directory, then create one.

To setup some files to run the project (you only need to run it once) do npm run setup

npm run dev - runs the dev server

Explanation of the project

dependency graph

project structure

Lower modules require upper modules. The listing is not alphabetical

ex) A, B(requires A), C(requires B)


# Directory src/

config #module export default

tool #module export { ... }
├───passport.js - is not used directly
├───roleManager.js
├───logger.js - used instead of console.log
├───morgan.js #(logger)
└───index.js #export

util #module export { ... }
├───<name>.js
└───index.js #export

types #module export { ... }
├───<namespace>
│   ├───<file>.js
│   └───index.js #export
└───index.js #export

models #module export { ... } - **mongoose Schemas** (does not initiate connection with mongoose)
├───base
│   ├───<file>.js
│   └───index.js #export
├───plugins
│   ├───<file>.js
│   └───index.js #export
├───env.js
└───index.js #export

connection #module export { ... } - clients, connections, apis: might be offline. It is up to the service to implement custom functions. If connection field is none, return null
└───index.js #export

shared #(connections) export { ... } - shared initialization. For example, client.channels.cache.get(`839399426643591188`). 
└───index.js #export, single

service #(services, models(mongo), connections(everything else), shared) export { ... } - functions: interacts with models, connections, and each other. return 503 if any of the according connection(s) are unavailable.
├───<file>.js
└───index.js #export

middlewares #(models, tools) export { ... } - passthrough/assert middlewares
├───<file>.js
└───index.js #export

auth #(models, tools) export { ... }- middlewares specified for authentication/authorization
├───<file>.js
└───index.js #export

validations #module export { ... } - joi validations. semantic grouping instead of functional
├───<name>.js
└───index.js #export

// There is no controller. Controllers should be implemented in the route itself.

pages #module export { ... } - ejs html pages
// These exist to return a friendly error html page in a case something is misconfigured in the server
└───index.js # export

routes #(validations, services) export { ... } - project structure matches end point
├───index.js # exports { v1: require('v1') }
├───lib
│   └───index.js #(validations,services) - eliminate redundant ../ also prevent circular dependency. APIs should ONLY use the modules declared in lib
└───v1 - /v1
    ├───auth - starts with /auth
    │   └───index.js # - implement it here. return Router
    └───index.js #export Router

static - #module export default - static view for testing api in browser
├───views
│   └───(ejs files here)
└───router.js

app.js - #(middlewares, routes, static) - express app
index.js - #(app.js, db.js) registers top level process hooks and exits.

script folder is used for helpers in npm run

Auth flow (database)

User

A User has multiple refresh tokens. Usually one for each client.

However, the property sessions does not exist on the User model

This is because sessions automatically

JWT

Refresh token

{
    "sub": "user id",
    "type": "REFRESH",
}

Access token

{
    "sub": "user id",
    "type": "ACCESS"
}

Testing

test/
client - client side requests. Does not import anything from src. Starts a server to test
server - server side functional testing. Imports most packeges from src.
unit   - 0 latency tdd style testing. Unit testing functions.

copy pasta

npm run dev -- --db-uri mongodb://127.0.0.1:27017/stem-backend-db-test