This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
Lateinicus/backend/src/main.ts

119 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-09-20 15:38:12 +00:00
import * as express from "express";
2018-09-28 21:33:28 +00:00
import * as cors from "cors";
2018-09-20 15:38:12 +00:00
import * as bodyparser from "body-parser";
import { isAuthenticated, performLogin } from "./security/auth";
2018-09-23 20:17:35 +00:00
import UserRouter from "./api/user";
import ClassRouter from "./api/class";
import LevelRouter from "./api/level";
2018-09-20 15:38:12 +00:00
const baseRouter = express.Router();
const authRouter = express.Router();
2018-09-29 12:23:09 +00:00
import { MongoClient } from "mongodb";
const assert = require('assert');
(async function() {
// Connection URL
const url = 'mongodb://128.1.0.2:27017/myproject';
// Database Name
const dbName = 'myproject';
let client: MongoClient;
try {
// Use connect method to connect to the Server
client = await MongoClient.connect(url);
console.log("Connected to MongoDB");
} catch (err) {
console.log(err.stack);
assert(1, 2);
2018-09-20 15:38:12 +00:00
}
2018-09-29 12:23:09 +00:00
const db = client.db(dbName);
console.log("Connected to the database");
2018-09-20 15:38:12 +00:00
2018-09-29 12:23:09 +00:00
const app = express();
app.use(bodyparser.json());
app.options("*", cors());
app.use((req, res, next) => {
// Every route should have access to the database so that
// we can easily make calls to it
// @ts-ignore
req.db = db;
next();
});
app.use("/api/level", LevelRouter);
app.use("/api/class", ClassRouter);
app.use("/api/user", UserRouter);
app.get("/api/levels", async (req, res) => {
const levels = [{
name: "Der Bauer auf dem Feld",
desc: "So fängt alles an: Du bist ein einfacher Bauer und musst dich die Karriereleiter mit deinen freshen Latein-Skills hinaufarbeiten",
level: 1,
done: true,
}, {
name: "???",
desc: "Warum schreibe ich überhaupt was?dsd dddddddddddddddddddddd",
level: 2,
done: false,
}];
res.send({
error: "0",
data: {
levels,
},
});
});
app.get("/api/health", (req, res) => {
2018-09-20 15:38:12 +00:00
res.send({
2018-09-29 12:23:09 +00:00
error: "0",
2018-09-20 15:38:12 +00:00
data: {
2018-09-29 12:23:09 +00:00
msg: "lol",
2018-09-20 15:38:12 +00:00
},
});
2018-09-29 12:23:09 +00:00
});
app.post("/api/login", async (req, res) => {
// Check if all arguments were sent
const { body } = req;
if (!body || !("username" in body) || !("password" in body)) {
res.send({
error: "400",
data: {
msg: "Username or password not specified",
},
});
2018-09-20 15:38:12 +00:00
2018-09-29 12:23:09 +00:00
return;
}
2018-09-20 15:38:12 +00:00
2018-09-29 12:23:09 +00:00
// Try to log the user in
try {
const userData = await performLogin(body.username, body.password, db);
res.send({
error: "0",
data: userData,
});
} catch (err) {
console.log("Could not resolve login promise!", err);
2018-09-20 15:38:12 +00:00
// If anything was wrong, just tell the client
res.send({
error: "1",
data: {
msg: "Username or password is wrong",
},
});
2018-09-29 12:23:09 +00:00
}
});
const server = app.listen(8080, () => {
console.log("Starting on port 8080");
2018-09-20 15:38:12 +00:00
});
2018-09-29 12:23:09 +00:00
})();