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:
$(MAKE) -C frontend/ build
fwatch:
$(MAKE) -C frontend/ watch
backend:
$(MAKE) -C backend/ build
bstart:
$(MAKE) -C backend/ start

View File

@ -1,10 +1,16 @@
node_modules:
npm install
.PHONY: build
build:
npm run-script build
WEBPACK_BUILD := ./node_modules/.bin/webpack-cli --config webpack.config.js
.PHONY: prod
prod:
${WEBPACK_BUILD} --env.NODE_ENV=production
.PHONY: dev
dev:
${WEBPACK_BUILD} --env.NODE_ENV=development
.PHONY: start
start:
node dist/main.js
start: dev
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": {
"@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(async (req, res, next) => {
if ("token" in req.body || req.get("token")) {
const token = req.body.token || req.get("token");
const token = req.get("Token");
if (token) {
// Check if were authenticated
const auth = await isAuthenticated(token);
if (auth)
@ -57,6 +56,9 @@ authRouter.get("/class/:id/topTen", async (req, res) => {
});
});
authRouter.get("/level/:id/vocab", async (req, res) => {
// TODO: Implement
console.log("Stub: /auth/level/:id/vocab");
if (!req.params) {
res.send({
error: "400",
@ -67,8 +69,6 @@ authRouter.get("/level/:id/vocab", async (req, res) => {
return;
}
console.log("Stub: /auth/level/:id/vocab");
// TODO: Implement
res.send({
error: "0",
data: {
@ -94,13 +94,7 @@ app.use((req, res, next) => {
// TODO: Change this to our domain
res.append("Access-Control-Allow-Origin", "*");
res.append("Access-Control-Allow-Headers", "Content-Type,Token");
if (res.method === "OPTIONS") {
// TODO: Send 200
res.end();
} else {
next();
}
next();
});
app.use("/auth", authRouter);
app.get("/health", (req, res) => {
@ -117,7 +111,7 @@ app.post("/login", async (req, res) => {
console.log("Stub: /login");
// Check if all arguments were sent
if (!body || !body.hasOwnProperty("username") || !body.hasOwnProperty("hash")) {
if (!body || !("username" in body) || !("password" in body)) {
res.send({
error: "400",
data: {
@ -129,8 +123,10 @@ app.post("/login", async (req, res) => {
}
// Try to log the user in
const userData = await performLogin(body.username, body.hash)
const userData = await performLogin(body.username, body.password)
.catch((err) => {
console.log("Could not resolve login promise!", err);
// If anything was wrong, just tell the client
res.send({
error: "1",

View File

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

View File

@ -8,6 +8,10 @@
"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,
};
}