Browse Source

finally in front of a decent working setup

main
Artemis 1 month ago
parent
commit
f1570e4937
  1. 5
      package.json
  2. 22
      src/index.ts
  3. 13
      src/routes.ts
  4. 16
      src/search-db.ts
  5. 0
      src/templates/index.njk

5
package.json

@ -9,13 +9,16 @@
"watch": "tsnd --respawn src/index.ts"
},
"dependencies": {
"@types/ioredis": "^4.27.2",
"fastify": "^3.20.2",
"liquidjs": "^9.25.1",
"fastify-redis": "^4.3.1",
"nunjucks": "^3.2.3",
"point-of-view": "^4.15.2"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.2",
"@types/node": "^16.7.10",
"fastify-plugin": "^3.0.0",
"ts-node-dev": "^1.1.8",
"typescript": "^4.4.2"
}

22
src/index.ts

@ -1,19 +1,25 @@
import fastify from 'fastify';
import pov from 'point-of-view';
import {join} from 'path';
import redis from 'fastify-redis';
import {db, SearchDb} from './search-db';
import {routes} from './routes';
declare module 'fastify' {
interface FastifyRequest {
searchDb: SearchDb
}
}
const server = fastify();
server.register(pov, {
engine: {
liquid: require('liquidjs'),
},
engine: {nunjucks: require('nunjucks')},
root: join(__dirname, 'templates'),
viewExt: 'liquid',
});
server.get('/', async (_, reply) => {
reply.view('index.liquid');
});
server.register(redis, {url: process.env.REDIS_URL || 'redis://127.0.0.1/0'});
server.register(db);
server.register(routes);
server.listen(parseInt(process.env.PORT || '8080', 10), (err, addr) => {
if (err) {

13
src/routes.ts

@ -0,0 +1,13 @@
import {FastifyPluginAsync} from 'fastify';
import fp from 'fastify-plugin';
type Options = {};
const setupRoutes: FastifyPluginAsync<Options> = async (fastify, opts) => {
fastify.get('/', async (req, reply) => {
const a = await fastify.redis.get('meow');
reply.view('index');
});
};
export const routes = fp(setupRoutes);

16
src/search-db.ts

@ -0,0 +1,16 @@
import {FastifyPluginAsync} from 'fastify';
import fp from 'fastify-plugin';
export class SearchDb {
public ping(): void {
console.log('Meow!');
}
}
type Options = {};
const setupDb: FastifyPluginAsync<Options> = async (fastify, opts) => {
fastify.decorateRequest('searchDb', new SearchDb());
};
export const db = fp(setupDb);

0
src/templates/index.liquid → src/templates/index.njk

Loading…
Cancel
Save