Optionally server static files
This commit is contained in:
parent
000b62fd40
commit
94e85e04d5
1 changed files with 20 additions and 9 deletions
|
|
@ -9,6 +9,8 @@ import auth from './Auth.js'
|
||||||
import db from './Database.js'
|
import db from './Database.js'
|
||||||
import jg from './JusticeGuard.js'
|
import jg from './JusticeGuard.js'
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
const protect = (req, res, next) => {
|
const protect = (req, res, next) => {
|
||||||
const authHeader = req.headers.authorization;
|
const authHeader = req.headers.authorization;
|
||||||
|
|
||||||
|
|
@ -42,19 +44,21 @@ const neededArguments = (args) => {
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(morgan('combined'))
|
app.use(morgan('combined'))
|
||||||
app.use(express.json())
|
|
||||||
app.use(cors())
|
app.use(cors())
|
||||||
|
|
||||||
app.get("/", (_, res) => {
|
router.use(express.json())
|
||||||
|
|
||||||
|
router.get("/", (_, res) => {
|
||||||
res.json({ message: "Hello! API here" })
|
res.json({ message: "Hello! API here" })
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/secured", protect, (req, res) => {
|
router.get("/secured", protect, (req, res) => {
|
||||||
res.json({ message: "You can see it!", payload: req.user })
|
res.json({ message: "You can see it!", payload: req.user })
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post("/signup", neededArguments(['name']), async (req, res) => {
|
router.post("/signup", neededArguments(['name']), async (req, res) => {
|
||||||
|
|
||||||
const user = await auth.createAccount(req.body.name)
|
const user = await auth.createAccount(req.body.name)
|
||||||
|
|
||||||
if (user.error)
|
if (user.error)
|
||||||
|
|
@ -65,7 +69,7 @@ app.post("/signup", neededArguments(['name']), async (req, res) => {
|
||||||
res.json(user)
|
res.json(user)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post("/login", neededArguments(['key']), async (req, res) => {
|
router.post("/login", neededArguments(['key']), async (req, res) => {
|
||||||
const user = auth.login(req.body.key)
|
const user = auth.login(req.body.key)
|
||||||
|
|
||||||
if (user.error)
|
if (user.error)
|
||||||
|
|
@ -76,7 +80,7 @@ app.post("/login", neededArguments(['key']), async (req, res) => {
|
||||||
res.json(user)
|
res.json(user)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post("/record", protect, neededArguments(['points', 'shoots', 'time']), jg, async (req, res) => {
|
router.post("/record", protect, neededArguments(['points', 'shoots', 'time']), jg, async (req, res) => {
|
||||||
db.updateRecord(req.user.name, req.body.points)
|
db.updateRecord(req.user.name, req.body.points)
|
||||||
const rank = db.getRank(req.user.name)
|
const rank = db.getRank(req.user.name)
|
||||||
|
|
||||||
|
|
@ -88,14 +92,14 @@ app.post("/record", protect, neededArguments(['points', 'shoots', 'time']), jg,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/top", (req, res) => {
|
router.get("/top", (req, res) => {
|
||||||
res.json({
|
res.json({
|
||||||
status: "ok",
|
status: "ok",
|
||||||
records: db.getTop()
|
records: db.getTop()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/start", protect, async (req, res) => {
|
router.get("/start", protect, async (req, res) => {
|
||||||
await db.setLastPlayedToNow(req.user.name)
|
await db.setLastPlayedToNow(req.user.name)
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
|
|
@ -103,13 +107,20 @@ app.get("/start", protect, async (req, res) => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (process.env.STATIC) {
|
||||||
|
app.use(express.static(process.env.STATIC));
|
||||||
|
app.use("/api", router);
|
||||||
|
} else {
|
||||||
|
app.use("/", router);
|
||||||
|
}
|
||||||
|
|
||||||
app.use(function (err, req, res, next) {
|
app.use(function (err, req, res, next) {
|
||||||
if (err.name === 'UnauthorizedError') {
|
if (err.name === 'UnauthorizedError') {
|
||||||
res.status(401).send('invalid token...');
|
res.status(401).send('invalid token...');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await db.read()
|
(async function () { await db.read() })()
|
||||||
|
|
||||||
const port = process.env.PORT || 3000
|
const port = process.env.PORT || 3000
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue