Skip to content

Backend Node.js application built with typescript for communicating between user, MongoDB and Discord API, managing authorization with discord passport

License

Notifications You must be signed in to change notification settings

RomaDevWorld/Kozak-API

Repository files navigation

Cossack-API

Backend Node.js application built with typescript for communicating between user, MongoDB and Discord API, managing authorization with discord passport

This application is still under development. Stay tuned for new updates!

Folder structure explanation

.env.example – Example file for environment variables required to run the app
types.d.ts - Typescript type declarations
src/
├── index.ts – File to start the app
├── utils/
│   ├── app.ts – Create express app
│   ├── client.ts – Connect to discord web socket
│   ├── mongodb.ts – Connect to MongoDB
│   └── strategy.ts – Discord authorization strategy
├── services/ – Useful reusable services
│   └── guilds/
│       ├── getUserGuilds.ts
│       └── getBotGuilds.ts
├── schemas/ – Mongoose schemas
├── routes/
│   ├── router.ts – Index router endpoint
│   ├── guilds/
│   └── auth/
├── middlewares/
│   ├── isAuthenticated.ts
│   └── isGuildAdmin.ts
└── controllers/ – Code that is being called in routes
    └── guild/

Router endpoints explanation

GET: / - global router (can be configured inside /src/utils/app.ts)

GET: /auth/discord - will execute the discord authentication passport and redirect to the bot authorization page.

GET: /auth/redirect - auth redirect URL, that user will be redirected to after authorizing the bot

GET: /auth/user - return information about current logged user

GET: /guilds/ - if the user is authorized, returns a JSON object with all the user's guilds.

GET: /guilds/[guildId] - returns a JSON object with general information about the given guild.

GET: /guilds/[guildId]/channel - returns a JSON object with all the channels in a given guild.

GET: /guilds/[guildId]/roles - returns a JSON object with all the roles in a given guild.

GET: /guilds/[guildId]/members - returns a JSON object with all the members in a given guild.

GET: /guilds/[guildId]/admin/modules - will query the database for guild modules (/src/schemas/Modules.ts) and return a JSON object with the result.

POST: /guilds/[guildId]/admin/modules - takes a data (mongoose update query e.g { log.channel: 101010101010101 }), updates the values in the database.

GET: /guilds/[guildId]/admin/modules/tickets - queries the database to find all the tickets with specified guildId, returns a JSON object with the result.

POST: /guilds/[guildId]/admin/modules/tickets/[ticketId] - takes a data (categoryId, prefix, etc.), queries the database with specified ticketId, updates the values.

GET: /guilds/[guildId]/admin/modules/ranks - queries the database to find all the rankings with specified guildId, returns a JSON object with the result.

POST: /guilds/[guildId]/admin/modules/ranks - takes a data (roleId, lvl), queries the database with specified guildId and roleId, updates the values.

Understanding env variables

PORT - Port on what application will run

ROUTE - Global route. / by default, /api recommended

FRONTEND_URL - Publicly accessible url to Frontend UI (After the authorization, it will redirect the user to it)

MONGOURL - Full MongoDB connection string (starts with mongodb://)

SECRET - String used to encrypt user's sessions (Create some with openssl rand -base64 32 or just type random characters)

RATE_LIMIT_TIME - Window of time in which user can make the requests

RATE_LIMIT_MAX - Max requests per window time

Next variables require discord bot application. You can create one here: https://discord.com/developers/applications

DISCORD_TOKEN - Discord application token from (Bot tab)

DISCORD_CLIENT_ID - Discord application ID (OAuth2 tab)

DISCORD_CLIENT_SECRET - Discord application secret (OAuth2 tab)

DISCORD_REDIRECT - Authorization redirect URL (Have to be configured in OAuth2 tab, should look something like http://localhost:3000/auth/discord/redirect)

How to run?

Locally with Node.js

  1. Install Node.js runtime
  2. Install Yarn npm i -g yarn
  3. Install Git
  4. Clone source code with git clone [email protected]:RomaDevWorld/Cossack-API.git or however you like it
  5. Open cloned directory in terminal, and run yarn install to install the dependencies
  6. Create .env file and specify all the necessary values (Mentioned previously)
  7. Build source code with yarn build
  8. Run the app yarn start

Using Docker

  1. Install Docker engine on your system
  2. Run command below after specifying with all the env variables -e (Mentioned previously):
sudo docker run \
--name api \
--restart unless-stopped \
-p 80:3000 \
-e DISCORD_TOKEN='Your discord bot token' \
-e DISCORD_CLIENT_ID='Your discord bot client id' \
-e DISCORD_CLIENT_SECRET="Discord application client secret" \
-e DISCORD_REDIRECT="Auth redirect url" \
-e SECRET="String for encryption" \
-e FRONTEND_URL="URL to frontend UI" \
-e MONGOURL="MongoDB connection string" \
-e ROUTE="/api" \
romadevworld/cossackapi:latest

About

Backend Node.js application built with typescript for communicating between user, MongoDB and Discord API, managing authorization with discord passport

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published