Natsuki's API!
https://api.natsuki.app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.7 KiB
61 lines
2.7 KiB
const express = require('express');
|
|
const bodyParser = require('body-parser');
|
|
const cors = require('cors');
|
|
const helmet = require('helmet');
|
|
const {set, createConnection} = require('mongoose');
|
|
const chalk = require('chalk');
|
|
|
|
const reqTypeColors = require('./util/misc/rtcolors');
|
|
|
|
const app = express();
|
|
set('strictQuery', false);
|
|
|
|
app.use(helmet());
|
|
app.use(bodyParser.json());
|
|
app.use(cors());
|
|
app.use(express.urlencoded({extended: true}));
|
|
|
|
app.get('/', (req, res) => res.send("You've reached the Natsuki API! This 200 status indicates we're online, and currently on v1. Natsuki bot-related queries live at /v1/, ani database queries live at /ani/v1/."));
|
|
|
|
app.auth = {};
|
|
app.auth.jwt_secret = require('../auth.json').jwt_secret;
|
|
|
|
let server;
|
|
const anidb = createConnection(`mongodb://127.0.0.1:27017/natsuki-anime-api`, {
|
|
useNewUrlParser: true, dbName: 'natsuki-anime-api', useUnifiedTopology: true
|
|
});
|
|
const botdb = createConnection(`mongodb://127.0.0.1:27017/natsuki-api`, {
|
|
useNewUrlParser: true, dbName: 'natsuki-api', useUnifiedTopology: true
|
|
});
|
|
console.log("Connected to Mongo Databases");
|
|
server = app.listen(4062, async () => {
|
|
console.log(`API online at port ${server.address().port}`);
|
|
|
|
app.db = {}; //set db connections into app object
|
|
app.db.ani = anidb;
|
|
app.db.bot = botdb;
|
|
|
|
require('../db/build')(app); //place all models in memory to prevent double-compiling
|
|
|
|
app.auth.token = require('./util/auth/authenticate')(app); //jwt token validation
|
|
app.auth.tokenPass = require('./util/auth/authenticate')(app, true); //"next()" will run even if auth is not passed
|
|
app.auth.perms = require('./util/auth/authorize')(app); //permissions checking
|
|
app.auth.permsPass = require('./util/auth/authorize')(app, true);
|
|
|
|
app.util = {};
|
|
app.util.list = require('./util/list');
|
|
app.util.editString = require('./util/editstring').masterInit(app); //TODO consistency shithead
|
|
|
|
await require('./util/startup/cache')(app);
|
|
|
|
require('./v1/index')(app); //initialize bot API branch
|
|
require('./ani/index')(app); //initialize ani API branch
|
|
|
|
const checkMiddleware = (middleware, string) => {
|
|
if (middleware.route) {Object.keys(middleware.route.methods).forEach(method => {
|
|
if (middleware.route.methods[method]) {console.log(`${chalk.gray(`[`)}${chalk[reqTypeColors[method.toUpperCase()]](method.toUpperCase())}${chalk.gray(`]`)} ${string}${middleware.route.path}`);}
|
|
});} else if (middleware.name === 'router') {middleware.handle.stack.forEach(handler => {checkMiddleware(handler, string + middleware.handle.location);});}
|
|
};
|
|
app._router.stack.forEach(middleware => checkMiddleware(middleware, '')); //log all routes
|
|
console.log('\n[Access logs for all routes]');
|
|
}); |