Compare commits

..

4 Commits

Author SHA1 Message Date
ad57086bcc Update dist output
Some checks failed
CI / main (push) Failing after 23s
this may need to be adjusted moving forward
2025-03-07 17:08:18 +02:00
a33149366b Implement basic caching mechanisms 2025-03-07 17:07:35 +02:00
eee8db226f Default to platform android
No iOS for the moment
2025-03-07 17:06:49 +02:00
efff71e59c Address assets issue
when items are in git ignore, they won't be copied.  Explicitely ncluding in .nxignore resolves this.
2025-03-07 17:06:23 +02:00
11 changed files with 199 additions and 17 deletions

5
.gitignore vendored
View File

@ -45,7 +45,4 @@ Thumbs.db
*.sqlite3 *.sqlite3
*.sqlite-journal *.sqlite-journal
*.db *.db
*.db-journal *.db-journal
cert.pem
key.pem

View File

@ -1,3 +1,3 @@
node_modules node_modules
# Keep environment variables out of version control
.env .env
**/certs/*.pem

View File

@ -0,0 +1,2 @@
# Include certs for nx (for example, copying as assets)
!**/certs/*.pem

View File

@ -11,7 +11,7 @@
"defaultConfiguration": "production", "defaultConfiguration": "production",
"options": { "options": {
"platform": "node", "platform": "node",
"outputPath": "dist/apps/blakus-api", "outputPath": "dist",
"format": ["cjs"], "format": ["cjs"],
"bundle": false, "bundle": false,
"main": "apps/blakus-api/src/main.ts", "main": "apps/blakus-api/src/main.ts",

View File

@ -0,0 +1,65 @@
import { FastifyInstance } from 'fastify';
import fp from 'fastify-plugin';
import Caching from '@fastify/caching';
declare module 'fastify' {
interface FastifyContextConfig {
serverCache?: {
key?: string;
ttl: number;
};
}
}
export default fp(async function (fastify: FastifyInstance) {
fastify.register(Caching,{
expiresIn: 10000,
privacy: Caching.privacy.PUBLIC,
// TODO: default is a mem cache, should use redis cache for prod, for example
})
.addHook('onRoute', (routeOptions) => {
const { config } = routeOptions;
if (config?.serverCache) {
const { key, ttl = 10000 } = config.serverCache;
// set or add our handler to the route preHandler hook
Object.entries({
preHandler: (request, reply, done) => {
fastify.cache.get(key || encodeURIComponent(request.raw.url), (err, cached) => {
if (err) {
// TODO: handle error
done();
return;
}
if (cached?.item) {
console.log('from cache');
reply.send(cached.item);
} else {
console.log('getting fresh data');
}
done();
});
return reply;
},
onSend: (request, _reply, payload, done) => {
fastify.cache.set(key || encodeURIComponent(request.raw.url), payload, ttl, () => {done();});
},
}).forEach(([hook, handler]) => {
if (!routeOptions[hook]) {
routeOptions[hook] = [handler]
return
}
if (Array.isArray(routeOptions[hook])) {
routeOptions[hook].push(handler)
return
}
routeOptions[hook] = [routeOptions[hook], handler]
})
}
})
});

View File

@ -22,15 +22,16 @@ export default async function (fastify: FastifyInstance) {
} }
} }
} }
},
config: {
serverCache: { ttl: 10000 }
} }
}, async function ({query}) { }, async function (request, reply) {
const {lat, lon} = request.query;
const elements = await overpassService.getAmenitiesAroundLocation({ const elements = await overpassService.getAmenitiesAroundLocation({
latlon: { latlon: { lat, lon },
lat: query.lat,
lon: query.lon,
},
amenities: ['restaurant', 'cafe', 'bar', 'pub', 'biergarten', 'fast_food', 'food_court', 'ice_cream'] amenities: ['restaurant', 'cafe', 'bar', 'pub', 'biergarten', 'fast_food', 'food_court', 'ice_cream']
}); });
return elements; reply.send(elements);
}); });
} }

View File

@ -2,8 +2,12 @@ import { FastifyInstance } from 'fastify';
import db from '../../db'; import db from '../../db';
export default async function (fastify: FastifyInstance) { export default async function (fastify: FastifyInstance) {
const users = await db.select('*').from('users'); fastify.get('/', {
fastify.get('/', async function () { config: {
return { users }; serverCache: { ttl: 10000 }
}
}, async function (req, reply) {
const users = await db.select('*').from('users');
reply.send({ users });
}); });
} }

View File

@ -1,7 +1,7 @@
{ {
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"outDir": "../../dist/out-tsc", "outDir": "./dist/out-tsc",
"module": "commonjs", "module": "commonjs",
"types": ["node"], "types": ["node"],
"allowJs": true, "allowJs": true,

View File

@ -11,7 +11,8 @@
"uglify": false, "uglify": false,
"release": false, "release": false,
"forDevice": false, "forDevice": false,
"prepare": false "prepare": false,
"platform": "android"
}, },
"configurations": { "configurations": {
"build": { "build": {

110
package-lock.json generated
View File

@ -10,6 +10,8 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@fastify/autoload": "~6.0.3", "@fastify/autoload": "~6.0.3",
"@fastify/caching": "^9.0.3",
"@fastify/etag": "^6.0.3",
"@fastify/sensible": "~6.0.2", "@fastify/sensible": "~6.0.2",
"@nativescript/core": "~8.8.0", "@nativescript/core": "~8.8.0",
"@sinclair/typebox": "^0.34.28", "@sinclair/typebox": "^0.34.28",
@ -2686,12 +2688,52 @@
], ],
"license": "MIT" "license": "MIT"
}, },
"node_modules/@fastify/caching": {
"version": "9.0.3",
"resolved": "https://registry.npmjs.org/@fastify/caching/-/caching-9.0.3.tgz",
"integrity": "sha512-5K/2shYpvWHWiSAs59SaCVBoFhHEF8Yz4TTiXZf8YWVDcxuIxw0Adn5eDQ7s132s7vwURNOnCKHBjUQSOI+PLA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fastify"
},
{
"type": "opencollective",
"url": "https://opencollective.com/fastify"
}
],
"license": "MIT",
"dependencies": {
"abstract-cache": "^1.0.1",
"fastify-plugin": "^5.0.0",
"uid-safe": "^2.1.5"
}
},
"node_modules/@fastify/error": { "node_modules/@fastify/error": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/@fastify/error/-/error-4.0.0.tgz", "resolved": "https://registry.npmjs.org/@fastify/error/-/error-4.0.0.tgz",
"integrity": "sha512-OO/SA8As24JtT1usTUTKgGH7uLvhfwZPwlptRi2Dp5P4KKmJI3gvsZ8MIHnNwDs4sLf/aai5LzTyl66xr7qMxA==", "integrity": "sha512-OO/SA8As24JtT1usTUTKgGH7uLvhfwZPwlptRi2Dp5P4KKmJI3gvsZ8MIHnNwDs4sLf/aai5LzTyl66xr7qMxA==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/@fastify/etag": {
"version": "6.0.3",
"resolved": "https://registry.npmjs.org/@fastify/etag/-/etag-6.0.3.tgz",
"integrity": "sha512-pMXohCA6Bk9JBNLl/KhHkSCGN7PCbgwVqHydwMsd1sVKtV8YNuzRW7lEe1VYap/MTNOn/Q8OfcR667lyrNbT/A==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fastify"
},
{
"type": "opencollective",
"url": "https://opencollective.com/fastify"
}
],
"license": "MIT",
"dependencies": {
"fastify-plugin": "^5.0.0"
}
},
"node_modules/@fastify/fast-json-stringify-compiler": { "node_modules/@fastify/fast-json-stringify-compiler": {
"version": "5.0.2", "version": "5.0.2",
"resolved": "https://registry.npmjs.org/@fastify/fast-json-stringify-compiler/-/fast-json-stringify-compiler-5.0.2.tgz", "resolved": "https://registry.npmjs.org/@fastify/fast-json-stringify-compiler/-/fast-json-stringify-compiler-5.0.2.tgz",
@ -5368,6 +5410,26 @@
"license": "ISC", "license": "ISC",
"optional": true "optional": true
}, },
"node_modules/abstract-cache": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/abstract-cache/-/abstract-cache-1.0.1.tgz",
"integrity": "sha512-EfUeMhRUbG5bVVbrSY/ogLlFXoyfMAPxMlSP7wrEqH53d+59r2foVy9a5KjmprLKFLOfPQCNKEfpBN/nQ76chw==",
"license": "MIT",
"dependencies": {
"clone": "^2.1.1",
"lru_map": "^0.3.3",
"merge-options": "^1.0.0"
}
},
"node_modules/abstract-cache/node_modules/clone": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
"integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==",
"license": "MIT",
"engines": {
"node": ">=0.8"
}
},
"node_modules/abstract-logging": { "node_modules/abstract-logging": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/abstract-logging/-/abstract-logging-2.0.1.tgz", "resolved": "https://registry.npmjs.org/abstract-logging/-/abstract-logging-2.0.1.tgz",
@ -9430,6 +9492,15 @@
"node": ">=0.12.0" "node": ">=0.12.0"
} }
}, },
"node_modules/is-plain-obj": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
"integrity": "sha512-yvkRyxmFKEOQ4pNXCmJG5AEQNlXJS5LaONXo5/cLdTZdWvsZ1ioJEonLGAosKlMWE8lwUy/bJzMjcw8az73+Fg==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/is-plain-object": { "node_modules/is-plain-object": {
"version": "2.0.4", "version": "2.0.4",
"resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz", "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz",
@ -10694,6 +10765,12 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/lru_map": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/lru_map/-/lru_map-0.3.3.tgz",
"integrity": "sha512-Pn9cox5CsMYngeDbmChANltQl+5pi6XmTrraMSzhPmMBbmgcxmqWry0U3PGapCU1yB4/LqCcom7qhHZiF/jGfQ==",
"license": "MIT"
},
"node_modules/lru-cache": { "node_modules/lru-cache": {
"version": "5.1.1", "version": "5.1.1",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz",
@ -10822,6 +10899,18 @@
"node": ">= 4.0.0" "node": ">= 4.0.0"
} }
}, },
"node_modules/merge-options": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-options/-/merge-options-1.0.1.tgz",
"integrity": "sha512-iuPV41VWKWBIOpBsjoxjDZw8/GbSfZ2mk7N1453bwMrfzdrIk7EzBd+8UVR6rkw67th7xnk9Dytl3J+lHPdxvg==",
"license": "MIT",
"dependencies": {
"is-plain-obj": "^1.1"
},
"engines": {
"node": ">=4"
}
},
"node_modules/merge-source-map": { "node_modules/merge-source-map": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/merge-source-map/-/merge-source-map-1.1.0.tgz", "resolved": "https://registry.npmjs.org/merge-source-map/-/merge-source-map-1.1.0.tgz",
@ -12327,6 +12416,15 @@
"integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==", "integrity": "sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/random-bytes": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
"integrity": "sha512-iv7LhNVO047HzYR3InF6pUcUsPQiHTM1Qal51DcGSuZFBil1aBBWG5eHPNek7bvILMaYJ/8RU1e8w1AMdHmLQQ==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/randombytes": { "node_modules/randombytes": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
@ -14096,6 +14194,18 @@
"typescript": ">=4.8.4 <5.8.0" "typescript": ">=4.8.4 <5.8.0"
} }
}, },
"node_modules/uid-safe": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz",
"integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==",
"license": "MIT",
"dependencies": {
"random-bytes": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/unicode-canonical-property-names-ecmascript": { "node_modules/unicode-canonical-property-names-ecmascript": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.1.tgz", "resolved": "https://registry.npmjs.org/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.1.tgz",

View File

@ -38,6 +38,8 @@
}, },
"dependencies": { "dependencies": {
"@fastify/autoload": "~6.0.3", "@fastify/autoload": "~6.0.3",
"@fastify/caching": "^9.0.3",
"@fastify/etag": "^6.0.3",
"@fastify/sensible": "~6.0.2", "@fastify/sensible": "~6.0.2",
"@nativescript/core": "~8.8.0", "@nativescript/core": "~8.8.0",
"@sinclair/typebox": "^0.34.28", "@sinclair/typebox": "^0.34.28",