fix: Add a build system for the backend

This commit is contained in:
Alexander Polynomdivision 2018-09-21 16:27:25 +02:00
parent 8061535936
commit db4b46b5aa
9 changed files with 4752 additions and 29 deletions

View File

@ -2,11 +2,5 @@
frontend: frontend:
$(MAKE) -C frontend/ build $(MAKE) -C frontend/ build
fwatch:
$(MAKE) -C frontend/ watch
backend: backend:
$(MAKE) -C backend/ build $(MAKE) -C backend/ build
bstart:
$(MAKE) -C backend/ start

View File

@ -1,10 +1,16 @@
node_modules: node_modules:
npm install npm install
.PHONY: build WEBPACK_BUILD := ./node_modules/.bin/webpack-cli --config webpack.config.js
build:
npm run-script build .PHONY: prod
prod:
${WEBPACK_BUILD} --env.NODE_ENV=production
.PHONY: dev
dev:
${WEBPACK_BUILD} --env.NODE_ENV=development
.PHONY: start .PHONY: start
start: start: dev
node dist/main.js node dist/backend/src/main.js

4634
backend/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -24,6 +24,9 @@
}, },
"devDependencies": { "devDependencies": {
"@types/express": "4.16.0", "@types/express": "4.16.0",
"typescript": "3.0.3" "ts-loader": "^5.1.1",
"typescript": "3.0.3",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.0"
} }
} }

View File

@ -9,9 +9,8 @@ const authRouter = express.Router();
authRouter.use(bodyparser.json()); authRouter.use(bodyparser.json());
authRouter.use(async (req, res, next) => { authRouter.use(async (req, res, next) => {
if ("token" in req.body || req.get("token")) { const token = req.get("Token");
const token = req.body.token || req.get("token"); if (token) {
// Check if were authenticated // Check if were authenticated
const auth = await isAuthenticated(token); const auth = await isAuthenticated(token);
if (auth) if (auth)
@ -57,6 +56,9 @@ authRouter.get("/class/:id/topTen", async (req, res) => {
}); });
}); });
authRouter.get("/level/:id/vocab", async (req, res) => { authRouter.get("/level/:id/vocab", async (req, res) => {
// TODO: Implement
console.log("Stub: /auth/level/:id/vocab");
if (!req.params) { if (!req.params) {
res.send({ res.send({
error: "400", error: "400",
@ -67,8 +69,6 @@ authRouter.get("/level/:id/vocab", async (req, res) => {
return; return;
} }
console.log("Stub: /auth/level/:id/vocab");
// TODO: Implement
res.send({ res.send({
error: "0", error: "0",
data: { data: {
@ -94,13 +94,7 @@ app.use((req, res, next) => {
// TODO: Change this to our domain // TODO: Change this to our domain
res.append("Access-Control-Allow-Origin", "*"); res.append("Access-Control-Allow-Origin", "*");
res.append("Access-Control-Allow-Headers", "Content-Type,Token"); res.append("Access-Control-Allow-Headers", "Content-Type,Token");
next();
if (res.method === "OPTIONS") {
// TODO: Send 200
res.end();
} else {
next();
}
}); });
app.use("/auth", authRouter); app.use("/auth", authRouter);
app.get("/health", (req, res) => { app.get("/health", (req, res) => {
@ -117,7 +111,7 @@ app.post("/login", async (req, res) => {
console.log("Stub: /login"); console.log("Stub: /login");
// Check if all arguments were sent // Check if all arguments were sent
if (!body || !body.hasOwnProperty("username") || !body.hasOwnProperty("hash")) { if (!body || !("username" in body) || !("password" in body)) {
res.send({ res.send({
error: "400", error: "400",
data: { data: {
@ -129,8 +123,10 @@ app.post("/login", async (req, res) => {
} }
// Try to log the user in // Try to log the user in
const userData = await performLogin(body.username, body.hash) const userData = await performLogin(body.username, body.password)
.catch((err) => { .catch((err) => {
console.log("Could not resolve login promise!", err);
// If anything was wrong, just tell the client // If anything was wrong, just tell the client
res.send({ res.send({
error: "1", error: "1",

View File

@ -1,5 +1,7 @@
import { pbkdf2Sync } from "crypto"; import { pbkdf2Sync } from "crypto";
import { IUser } from "shared/user";
export function isAuthenticated(token: string): Promise<boolean> { export function isAuthenticated(token: string): Promise<boolean> {
return new Promise((res, rej) => { return new Promise((res, rej) => {
// TODO // TODO
@ -7,7 +9,7 @@ export function isAuthenticated(token: string): Promise<boolean> {
}); });
} }
export function performLogin(username: string, password: string): Promise<any | {}> { export function performLogin(username: string, password: string): Promise<IUser | {}> {
return new Promise((res, rej) => { return new Promise((res, rej) => {
// Hash the password // Hash the password
// TODO: Fetch the salt // TODO: Fetch the salt
@ -18,8 +20,9 @@ export function performLogin(username: string, password: string): Promise<any |
res({ res({
username: "Polynom", username: "Polynom",
uid: "1", uid: "1",
showWelcome: false, showWelcome: true,
classId: "test", classId: "test",
score: 4,
sessionToken: "abc123", sessionToken: "abc123",
}); });

View File

@ -8,6 +8,10 @@
"outDir": "./dist/", "outDir": "./dist/",
"baseUrl": "./src/" "baseUrl": "./src/",
"paths": {
"shared/*": ["../../shared/*"]
}
} }
} }

32
backend/webpack.config.js Normal file
View File

@ -0,0 +1,32 @@
const path = require("path");
module.exports = env => {
const production = env.NODE_ENV || "production";
return {
entry: "./src/main.ts",
devtool: production ? "source-map": "inline-source-map",
target: "node",
mode: env.NODE_ENV,
module: {
rules: [
{
test: /\.ts$/,
use: "ts-loader",
exclude: /node_modules/
}
]
},
resolve: {
extensions: [".ts", ".js"],
alias: {
shared: path.join(__dirname, "../shared/")
}
},
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
}
};
};

51
shared/user.ts Normal file
View File

@ -0,0 +1,51 @@
export interface IUser {
username: string;
uid: string;
showWelcome: boolean;
score: number;
sessionToken: string;
};
export interface IUserLevel {
// The numerical representation
level: number;
// The string representation
name: string;
// The user has this level until: score => levelCap
levelCap: number;
};
function levelFactory(): (name: string, levelCap: number) => IUserLevel {
let level = 1;
return (name: string, levelCap: number) => {
return {
level: level++,
name,
levelCap,
};
};
}
const l = levelFactory();
export const UserLevels: IUserLevel[] = [
l("Sklave", 35),
l("Farmer", 75),
l("Soldat", 120),
l("Gladiator", 170),
l("Zenturio", 220),
l("Prätor", 270),
l("Reiter", 320),
l("General", 370),
l("Konsul", 420),
l("Caesar", 470),
];
export function userScoreToLevel(userScore: number): IUserLevel {
// NOTE: The failure level should never be returned
return UserLevels.find((el) => userScore < el.levelCap) || {
level: 0,
name: "Failure",
levelCap: 10000000,
};
}