diff --git a/.npmignore b/.npmignore new file mode 100644 index 00000000..05a9d0cf --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +!dist/ \ No newline at end of file diff --git a/README.md b/README.md index bf8eed09..d83120e2 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,51 @@ # Fosscord API Server -This repository contains the HTTP API Server + +This repository contains the Fosscord HTTP API Server ## Bug Tracker + [Project Board](https://github.com/fosscord/fosscord-api/projects/2) ## API -We use [express](https://expressjs.com/) for the HTTP Server and +We use [express](https://expressjs.com/) for the HTTP Server and [lambert-server](https://www.npmjs.com/package/lambert-server) for route handling and body validation (customized). ## Contribution -You should be familiar with: -- [Git](https://git-scm.com/) -- [NodeJS](https://nodejs.org/) -- [TypeScript](https://www.typescriptlang.org/) -- [MongoDB/mongoose](http://mongoosejs.com/) -and the other technologies we use +You should be familiar with: + +- [Git](https://git-scm.com/) +- [NodeJS](https://nodejs.org/) +- [TypeScript](https://www.typescriptlang.org/) +- [MongoDB/mongoose](http://mongoosejs.com/) + +and the other technologies we use ### Getting Started + Clone the Repository: + ```bash git clone https://github.com/fosscord/fosscord-api cd discord-server ``` + #### Install (dev)dependencies: + ```bash npm install npm install --only=dev ``` + #### Starting: + ``` npm start ``` + #### Debugging: + **Vscode:** -The Launch file configuration is in ``./vscode/launch.json``, -so you can just debug the server by pressing ``F5`` or the ``> Launch Server`` button +The Launch file configuration is in `./vscode/launch.json`, +so you can just debug the server by pressing `F5` or the `> Launch Server` button diff --git a/package-lock.json b/package-lock.json index bd7cb447..455aa3c4 100644 Binary files a/package-lock.json and b/package-lock.json differ diff --git a/package.json b/package.json index 2047cb37..b8e591df 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { - "name": "fosscord-api", + "name": "@fosscord/api", "version": "1.0.0", "description": "This repository contains the HTTP API Server", - "main": "index.js", + "main": "dist/Server.js", + "types": "dist/Server.d.ts", "scripts": { "test": "jest", "test:watch": "jest --watch", - "start": "npm run build:util && npm run build && node dist/", + "start": "npm run build:util && npm run build && node dist/start", "build": "tsc -b .", "build:util": "tsc -b ./node_modules/fosscord-server-util/", "postinstall": "npm i github:fosscord/fosscord-server-util && patch-package" @@ -15,14 +16,21 @@ "type": "git", "url": "git+https://github.com/fosscord/fosscord-api.git" }, - "keywords": [], - "author": "", + "keywords": [ + "discord", + "fosscord", + "fosscord-api", + "discord open source", + "discord-open-source" + ], + "author": "Fosscord", "license": "ISC", "bugs": { "url": "https://github.com/fosscord/fosscord-api/issues" }, "homepage": "https://github.com/fosscord/fosscord-api#readme", "dependencies": { + "@fosscord/fosscord-server-util": "github:fosscord/fosscord-server-util", "@types/jest": "^26.0.22", "bcrypt": "^5.0.0", "body-parser": "^1.19.0", diff --git a/src/Server.ts b/src/Server.ts index 19a0fc5f..03222d4a 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -13,21 +13,21 @@ import { BodyParser } from "./middlewares/BodyParser"; import { Router } from "express"; import fetch from "node-fetch"; -export interface DiscordServerOptions extends ServerOptions {} +export interface FosscordServerOptions extends ServerOptions {} declare global { namespace Express { interface Request { // @ts-ignore - server: DiscordServer; + server: FosscordServer; } } } -export class DiscordServer extends Server { - public options: DiscordServerOptions; +export class FosscordServer extends Server { + public options: FosscordServerOptions; - constructor(opts?: Partial) { + constructor(opts?: Partial) { // @ts-ignore super({ ...opts, errorHandler: false, jsonBody: false }); } diff --git a/src/index.ts b/src/index.ts index 471bf90c..c6417126 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,33 +1,19 @@ -process.on("uncaughtException", console.error); -process.on("unhandledRejection", console.error); - -import "missing-native-js-functions"; -import { config } from "dotenv"; -config(); -import { DiscordServer } from "./Server"; -import cluster from "cluster"; -import os from "os"; -const cores = os.cpus().length; - -if (cluster.isMaster && process.env.production == "true") { - console.log(`Primary ${process.pid} is running`); - - // Fork workers. - for (let i = 0; i < cores; i++) { - cluster.fork(); - } - - cluster.on("exit", (worker, code, signal) => { - console.log(`worker ${worker.process.pid} died, restart worker`); - cluster.fork(); - }); -} else { - var port = Number(process.env.PORT); - if (isNaN(port)) port = 1000; - - const server = new DiscordServer({ port }); - server.start().catch(console.error); - - // @ts-ignore - global.server = server; -} +export * from "./Server"; +export * from "./middlewares/"; +export * from "./schema/Ban"; +export * from "./schema/Channel"; +export * from "./schema/Guild"; +export * from "./schema/Invite"; +export * from "./schema/Message"; +export * from "./util/Captcha"; +export * from "./util/Config"; +export * from "./util/Constants"; +export * from "./util/Event"; +export * from "./util/instanceOf"; +export * from "./util/Event"; +export * from "./util/instanceOf"; +export * from "./util/Member"; +export * from "./util/RandomInviteID"; +export * from "./util/String"; +export * from "./util/User"; +export { check as checkPassword } from "./util/passwordStrength"; diff --git a/src/middlewares/index.ts b/src/middlewares/index.ts index e3332f07..6a2993a7 100644 --- a/src/middlewares/index.ts +++ b/src/middlewares/index.ts @@ -1,4 +1,6 @@ -import { Authentication } from "./Authentication"; -import { GlobalRateLimit } from "./GlobalRateLimit"; - -export { Authentication, GlobalRateLimit }; +export * from "./GlobalRateLimit"; +export * from "./Authentication"; +export * from "./BodyParser"; +export * from "./CORS"; +export * from "./ErrorHandler"; +export * from "./RateLimit"; diff --git a/src/start.ts b/src/start.ts new file mode 100644 index 00000000..11e15941 --- /dev/null +++ b/src/start.ts @@ -0,0 +1,33 @@ +process.on("uncaughtException", console.error); +process.on("unhandledRejection", console.error); + +import "missing-native-js-functions"; +import { config } from "dotenv"; +config(); +import { FosscordServer } from "./Server"; +import cluster from "cluster"; +import os from "os"; +const cores = os.cpus().length; + +if (cluster.isMaster && process.env.production == "true") { + console.log(`Primary ${process.pid} is running`); + + // Fork workers. + for (let i = 0; i < cores; i++) { + cluster.fork(); + } + + cluster.on("exit", (worker, code, signal) => { + console.log(`worker ${worker.process.pid} died, restart worker`); + cluster.fork(); + }); +} else { + var port = Number(process.env.PORT); + if (isNaN(port)) port = 1000; + + const server = new FosscordServer({ port }); + server.start().catch(console.error); + + // @ts-ignore + global.server = server; +} diff --git a/src/test/rethink_test.ts b/src/test/rethink_test.ts.disabled similarity index 100% rename from src/test/rethink_test.ts rename to src/test/rethink_test.ts.disabled diff --git a/src/util/Captcha.ts b/src/util/Captcha.ts index e69de29b..cb0ff5c3 100644 --- a/src/util/Captcha.ts +++ b/src/util/Captcha.ts @@ -0,0 +1 @@ +export {}; diff --git a/src/util/Config.ts b/src/util/Config.ts index 60d83e1a..1a038b1d 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -16,7 +16,7 @@ export default { setAll: Config.setAll, }; -export interface RateLimit { +export interface RateLimitOptions { count: number; timespan: number; } @@ -62,8 +62,8 @@ export interface DefaultOptions { }; routes: { auth?: { - login?: RateLimit; - register?: RateLimit; + login?: RateLimitOptions; + register?: RateLimitOptions; }; channel?: {}; // TODO: rate limit configuration for all routes diff --git a/tsconfig.json b/tsconfig.json index 0976d3f7..1d4f4886 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["src/**/*.ts"], + "include": ["src/**/*.ts", "src/test/rethink_test.ts.disabled"], "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */