-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #506 from Jameskmonger/fix-live-runtime
fix: various live runtime fixes
- Loading branch information
Showing
53 changed files
with
22,344 additions
and
25,914 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ node_modules | |
npm-debug.log | ||
terraform/ | ||
public/ | ||
.faundb/ | ||
.faunadb/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ apps/**/public/ | |
yarn.lock | ||
*.tfstate | ||
.auth0 | ||
.faunadb/ |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import express from "express"; | ||
import { logger as expressWinston } from "express-winston"; | ||
|
||
import { | ||
authenticate, | ||
convertDatabaseUserToUserModel, | ||
} from "@creature-chess/auth-server"; | ||
import { | ||
createDatabaseConnection, | ||
DatabaseConnection, | ||
} from "@creature-chess/data"; | ||
import { | ||
AVAILABLE_PROFILE_PICTURES, | ||
validateNicknameFormat, | ||
} from "@creature-chess/models"; | ||
|
||
import { logger } from "./src/log"; | ||
import { getManagementClient } from "./src/util/auth0"; | ||
import { sanitize } from "./src/util/sanitize-user"; | ||
|
||
import Filter = require("bad-words"); | ||
|
||
const app = express(); | ||
const PORT = 3000; | ||
|
||
// Define a middleware to parse JSON requests | ||
app.use(express.json()); | ||
app.use(expressWinston({ winstonInstance: logger })); | ||
|
||
async function getNicknameUpdate( | ||
database: DatabaseConnection, | ||
filter: Filter, | ||
body: { nickname?: string } | ||
): Promise<{ error: string | null; nickname: string | null }> { | ||
const { nickname } = body; | ||
|
||
if (!nickname) { | ||
return { error: null, nickname: null }; | ||
} | ||
|
||
const trimmedNickname = nickname.trim(); | ||
|
||
const nicknameError = validateNicknameFormat(nickname); | ||
|
||
if (nicknameError) { | ||
return { error: nicknameError, nickname: null }; | ||
} | ||
|
||
if (filter.isProfane(nickname)) { | ||
return { error: "Profanity filter", nickname: null }; | ||
} | ||
|
||
const isUnique = (await database.user.getByNickname(nickname)) === null; | ||
|
||
if (!isUnique) { | ||
return { error: "Nickname already in use", nickname: null }; | ||
} | ||
|
||
return { error: null, nickname: trimmedNickname }; | ||
} | ||
|
||
async function getPictureUpdate(body: { | ||
picture?: string; | ||
}): Promise<{ error: string | null; picture: number | null }> { | ||
const { picture } = body; | ||
|
||
if (!picture) { | ||
return { error: null, picture: null }; | ||
} | ||
|
||
const pictureId = parseInt(picture, 10); | ||
|
||
if (isNaN(pictureId)) { | ||
return { error: "Invalid picture id", picture: null }; | ||
} | ||
|
||
if (!Object.keys(AVAILABLE_PROFILE_PICTURES).includes(picture.toString())) { | ||
return { error: "Picture id supplied is not useable", picture: null }; | ||
} | ||
|
||
return { error: null, picture: pictureId }; | ||
} | ||
|
||
async function startServer() { | ||
const authClient = getManagementClient(); | ||
const filter = new Filter(); | ||
|
||
const database = await createDatabaseConnection(logger); | ||
|
||
app.get("/user/current", async (req, res) => { | ||
const { authorization } = req.headers; | ||
|
||
if (!authorization) { | ||
logger.info("No authorization header found"); | ||
|
||
return res.status(401).json({ | ||
message: "Not authorized", | ||
}); | ||
} | ||
|
||
const user = await authenticate( | ||
authClient, | ||
database, | ||
authorization as string | ||
); | ||
|
||
res.status(200).json(sanitize(user)); | ||
}); | ||
|
||
app.patch("/user/current", async (req, res) => { | ||
const { authorization } = req.headers; | ||
|
||
if (!authorization) { | ||
logger.info("No authorization header found"); | ||
|
||
return res.status(401).json({ | ||
message: "Not authorized", | ||
}); | ||
} | ||
|
||
const user = await authenticate( | ||
authClient, | ||
database, | ||
authorization as string | ||
); | ||
|
||
if (!user) { | ||
logger.info("No user found"); | ||
|
||
return res.status(401).json({ | ||
message: "Not authorized", | ||
}); | ||
} | ||
|
||
if (user.registered) { | ||
console.log(`Registered user ${user.id} tried to patch`); | ||
|
||
return res.status(403).json({ | ||
message: "Forbidden", | ||
}); | ||
} | ||
|
||
const nicknameUpdate = await getNicknameUpdate(database, filter, req.body); | ||
|
||
if (nicknameUpdate.error) { | ||
return res.status(400).json({ | ||
message: nicknameUpdate.error, | ||
}); | ||
} | ||
|
||
const pictureUpdate = await getPictureUpdate(req.body); | ||
|
||
if (pictureUpdate.error) { | ||
return res.status(400).json({ | ||
message: pictureUpdate.error, | ||
}); | ||
} | ||
|
||
const updatedUser = await database.user.setProfileInfo( | ||
user.id, | ||
nicknameUpdate.nickname, | ||
pictureUpdate.picture | ||
); | ||
|
||
if (!updatedUser) { | ||
return res.status(500).json({ | ||
message: "An error occurred while updating the user", | ||
}); | ||
} | ||
|
||
res.status(200).json(sanitize(convertDatabaseUserToUserModel(updatedUser))); | ||
}); | ||
|
||
// Start the server | ||
app.listen(PORT, () => { | ||
console.log(`Server is listening on port ${PORT}`); | ||
}); | ||
} | ||
|
||
startServer().catch((e) => { | ||
logger.error("An error occurred while starting the server", e); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"private": true, | ||
"name": "@creature-chess/server-info", | ||
"version": "0.0.1", | ||
"description": "HTTP info server", | ||
"keywords": [], | ||
"author": "James Monger <[email protected]>", | ||
"license": "ISC", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Jameskmonger/creature-chess.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/Jameskmonger/creature-chess/issues" | ||
}, | ||
"homepage": "https://github.com/Jameskmonger/creature-chess#readme", | ||
"scripts": { | ||
"build": "yarn run -T tsc -p ./tsconfig.json", | ||
"test": "yarn run -T jest --passWithNoTests" | ||
}, | ||
"dependencies": { | ||
"@creature-chess/auth-server": "workspace:*", | ||
"@creature-chess/data": "workspace:*", | ||
"@creature-chess/models": "workspace:*", | ||
"@types/bad-words": "^3.0.1", | ||
"bad-words": "^3.0.4", | ||
"express": "^4.17.2", | ||
"express-winston": "^4.2.0", | ||
"winston": "^3.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import winston = require("winston"); | ||
|
||
const createWinstonLogger = () => { | ||
const devMode = false; | ||
|
||
const newLogger = winston.createLogger({ | ||
format: devMode | ||
? winston.format.combine( | ||
winston.format.errors({ stack: true }), | ||
winston.format.colorize(), | ||
winston.format.timestamp(), | ||
winston.format.prettyPrint() | ||
) | ||
: undefined, | ||
}); | ||
|
||
newLogger.add(new winston.transports.Console()); | ||
newLogger.info("Sending Winston logs to console"); | ||
|
||
return newLogger; | ||
}; | ||
|
||
export const logger = createWinstonLogger(); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./lib", | ||
"importHelpers": true | ||
}, | ||
"include": ["./src/**/*", "index.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.