Skip to content

Commit

Permalink
feat: Koji loading fix & reloading API (#877)
Browse files Browse the repository at this point in the history
* fix: koji config reading

* fix: geofence file watching

* feat: add reload api

* refactor: simplify
  • Loading branch information
TurtIeSocks authored Nov 3, 2023
1 parent d5c6c39 commit 353fdd1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,12 @@ async function run() {
setTimeout(processPogoEvents, 30000)
setTimeout(processPossibleShiny, 30000)

let watchGeofence = Array.isArray(config.geofence.path) ? config.geofence.path : [config.geofence.path]
watchGeofence = watchGeofence.map((x) => path.join(__dirname, `../${x}`))
let watchGeofence = Array.isArray(config.geofence.path)
? config.geofence.path
: [config.geofence.path]
watchGeofence = watchGeofence.map((x) => (x.startsWith('http')
? path.join(__dirname, '../.cache', `${x.replace(/\//g, '__')}.json`)
: path.join(__dirname, `../${x}`)))

chokidar.watch(watchGeofence, {
awaitWriteFinish: true,
Expand Down
33 changes: 30 additions & 3 deletions src/routes/apiGeofence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const geofenceTileGenerator = require('../lib/geofenceTileGenerator')
const { getKojiFences } = require('../util/koji')

module.exports = async (fastify, options, next) => {
fastify.get('/api/geofence/:area/map', options, async (req) => {
Expand Down Expand Up @@ -225,10 +226,10 @@ module.exports = async (fastify, options, next) => {
const outPath = []
if (inGeofence.multipath) {
for (let j = 0; j < inGeofence.multipath.length; j++) {
const path = inGeofence.multipath[j]
const geofencePath = inGeofence.multipath[j]
const outSubPath = []
for (let k = 0; k < path.length; k++) {
const coord = path[k]
for (let k = 0; k < geofencePath.length; k++) {
const coord = geofencePath[k]
outSubPath.push([coord[1], coord[0]])
}
if (outSubPath.at(-1)[0] !== outSubPath[0][0] || outSubPath.at(-1)[1] !== outSubPath[0][1]) {
Expand All @@ -255,5 +256,31 @@ module.exports = async (fastify, options, next) => {
}
})

fastify.get('/api/geofence/reload', options, async (req) => {
fastify.logger.info(`API: ${req.ip} ${req.routeOptions.method} ${req.routeOptions.url}`)

if (fastify.config.server.ipWhitelist.length && !fastify.config.server.ipWhitelist.includes(req.ip)) {
return {
webserver: 'unhappy',
reason: `ip ${req.ip} not in whitelist`,
}
}
if (fastify.config.server.ipBlacklist.length && fastify.config.server.ipBlacklist.includes(req.ip)) {
return {
webserver: 'unhappy',
reason: `ip ${req.ip} in blacklist`,
}
}

const secret = req.headers['x-poracle-secret']
if (!secret || !fastify.config.server.apiSecret || secret !== fastify.config.server.apiSecret) {
return { status: 'authError', reason: 'incorrect or missing api secret' }
}

await getKojiFences()

return { status: 'ok' }
})

next()
}
19 changes: 1 addition & 18 deletions src/util/koji.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
const fs = require('fs')
const { resolve } = require('path')
const fetch = require('node-fetch')
const json5 = require('json5')
const config = require('config')

const { log } = require('../lib/logger')

const parseConfigSafe = () => {
let config = {}
try {
if (fs.existsSync(resolve(__dirname, '../../config/local.json'))) {
const string = fs.readFileSync(resolve(__dirname, '../../config/local.json')).toString()
config = json5.parse(string)
} else {
log.warn('[KŌJI] Local.json was not found, skipping')
}
return config
} catch (e) {
log.error('[KŌJI] Could not parse local config', e)
return config
}
}

const getKojiFences = async () => {
const config = parseConfigSafe()
try {
if (config?.geofence?.kojiOptions?.bearerToken) {
const fences = Array.isArray(config.geofence?.path)
Expand Down

0 comments on commit 353fdd1

Please sign in to comment.