Skip to content

Commit

Permalink
feat: add internationalization support
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakia committed Feb 20, 2024
1 parent fa11e6e commit 084457d
Show file tree
Hide file tree
Showing 63 changed files with 4,132 additions and 3,077 deletions.
43 changes: 40 additions & 3 deletions api/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import serveStatic from 'serve-static'
import cors from 'cors'
import favicon from 'express-favicon'
import robots from 'express-robots-txt'
import { slowDown } from 'express-slow-down'

import * as config from '#config'
import * as routes from '#api/routes/index.mjs'
Expand Down Expand Up @@ -70,6 +71,9 @@ api.use((req, res, next) => {
const resourcesPath = path.join(__dirname, '..', 'resources')
api.use('/resources', serveStatic(resourcesPath))

const localesPath = path.join(__dirname, '..', 'locales')
api.use('/locales', serveStatic(localesPath))

const dataPath = path.join(__dirname, '..', 'data')
api.use('/data', serveStatic(dataPath))

Expand All @@ -94,9 +98,42 @@ api.use('/api/representatives', routes.representatives)
api.use('/api/weight', routes.weight)

const docsPath = path.join(__dirname, '..', 'docs')
api.use('/api/docs', serveStatic(docsPath))
api.get('/api/docs/*', (req, res) => {
res.status(404).send('Not found')

const speedLimiter = slowDown({
windowMs: 10 * 60 * 1000, // 10 minutes
delayAfter: 50, // allow 50 requests per 10 minutes, then...
delayMs: 500, // begin adding 500ms of delay per request above 50:
maxDelayMs: 20000 // maximum delay of 20 seconds
})

api.use('/api/docs', speedLimiter, serveStatic(docsPath))
api.use('/api/docs/en', speedLimiter, serveStatic(docsPath))
api.get('/api/docs/:locale/*', speedLimiter, async (req, res) => {
const { locale } = req.params
const doc_id = req.params[0] // Capture the rest of the path as doc_id
const localized_doc_path = path.join(docsPath, locale, `${doc_id}.md`)
const default_doc_path = path.join(docsPath, 'en', `${doc_id}.md`)

// check if paths are under the docs directory
if (
!localized_doc_path.startsWith(docsPath) ||
!default_doc_path.startsWith(docsPath)
) {
return res.status(403).send('Forbidden')
}

try {
if (fs.existsSync(localized_doc_path)) {
return res.sendFile(localized_doc_path)
} else if (fs.existsSync(default_doc_path)) {
return res.redirect(`/api/docs/en/${doc_id}`)
} else {
return res.status(404).send('Document not found')
}
} catch (error) {
console.error(error)
return res.status(500).send('Internal Server Error')
}
})

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a file system access
, but is not rate-limited.
This route handler performs
a file system access
, but is not rate-limited.
This route handler performs
a file system access
, but is not rate-limited.

api.use('/api/*', (err, req, res, next) => {
Expand Down

0 comments on commit 084457d

Please sign in to comment.