Skip to content

Commit

Permalink
Merge branch 'ke/nextjs-geoip' of https://github.com/kitspace/kitspac…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kitspace Auto-Merge Bot committed Jan 20, 2025
2 parents cb9660e + 1d91328 commit 882a27a
Show file tree
Hide file tree
Showing 6 changed files with 448 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
- ./frontend:/app
- frontend-node_modules:/app/node_modules
- frontend-next:/app/.next
command: /bin/sh --verbose -c 'yarn install && yarn dev'
command: /bin/sh --verbose -c 'yarn install && yarn download-geoip && yarn dev'

gitea:
ports:
Expand Down
3 changes: 3 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# geoip
*.mmdb
1 change: 1 addition & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ WORKDIR /build
COPY . .
ENV NODE_ENV=development
RUN yarn --frozen-lockfile
RUN yarn run download-geoip
ENV NODE_ENV=production
RUN yarn build
WORKDIR /deps
Expand Down
6 changes: 5 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
"build-fallback": "next build && next export -o static_fallback",
"start": "node src/server.js",
"fmt": "prettier --write '*.json' 'src/**/*.{ts,js,tsx,jsx}'",
"lint": "next lint"
"lint": "next lint",
"download-geoip": "node -e \"import('geolite2-redist').then(geolite => geolite.downloadDbs({dbList: ['GeoLite2-Country'], path: 'src/pages/api/geoip/'}))\""
},
"license": "AGPL-3.0",
"dependencies": {
"1-click-bom-minimal": "https://github.com/kitspace/npm-1-click-bom-minimal",
"@fortawesome/fontawesome-svg-core": "^6.7.1",
"@fortawesome/free-brands-svg-icons": "^6.7.1",
"@fortawesome/react-fontawesome": "^0.2.2",
"@types/node": "^20.0.0",
"express": "^4.20.0",
"file-loader": "^6.0.0",
"geolite2-redist": "^3.1.1",
"highlight.js": "^11.6.0",
"lodash": "^4.17.21",
"lodash.debounce": "^4.0.8",
"maxmind": "^4.3.23",
"meilisearch": "^0.25.1",
"morgan": "^1.10.0",
"next": "^12.1.6",
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/pages/api/geoip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CountryResponse, Reader } from 'maxmind'
import type { NextApiRequest, NextApiResponse } from 'next'
import fs from 'node:fs'
import path, { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const file = fileURLToPath(import.meta.url)
const dir = dirname(file)
const buffer = fs.readFileSync(path.join(dir, 'GeoLite2-Country.mmdb'))

const geo = new Reader<CountryResponse>(buffer)

export default async function handleGeoip(
req: NextApiRequest,
res: NextApiResponse,
) {
const ip = req.headers['x-forwarded-for']
const lookup = geo.get(ip as string)
res.json({ country_code: lookup?.country?.iso_code })
}
Loading

0 comments on commit 882a27a

Please sign in to comment.