Skip to content

Commit

Permalink
Add search app
Browse files Browse the repository at this point in the history
  • Loading branch information
needs committed Nov 23, 2024
1 parent ce03123 commit 9aa551e
Show file tree
Hide file tree
Showing 16 changed files with 727 additions and 34 deletions.
18 changes: 18 additions & 0 deletions apps/search/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
24 changes: 24 additions & 0 deletions apps/search/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file is generated by Nx.
#
# Build the docker image with `npx nx docker-build search`.
# Tip: Modify "docker-build" options in project.json to change docker build args.
#
# Run the container with `docker run -p 3000:3000 -t search`.
FROM docker.io/node:lts-alpine

ENV HOST=0.0.0.0
ENV PORT=3000

WORKDIR /app

RUN addgroup --system search && \
adduser --system -G search search

COPY dist/apps/search search/
RUN chown -R search:search .

# You can remove this install step if you build with `--bundle` option.
# The bundled output will include external dependencies.
RUN npm --prefix search --omit=dev -f install

CMD [ "node", "search" ]
10 changes: 10 additions & 0 deletions apps/search/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
displayName: 'search',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps/search',
};
73 changes: 73 additions & 0 deletions apps/search/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"name": "search",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/search/src",
"projectType": "application",
"tags": [],
"targets": {
"build": {
"executor": "@nx/esbuild:esbuild",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"platform": "node",
"outputPath": "dist/apps/search",
"format": ["cjs"],
"bundle": false,
"main": "apps/search/src/main.ts",
"tsConfig": "apps/search/tsconfig.app.json",
"assets": ["apps/search/src/assets"],
"generatePackageJson": true,
"esbuildOptions": {
"sourcemap": true,
"outExtension": {
".js": ".js"
}
}
},
"configurations": {
"development": {},
"production": {
"generateLockfile": true,
"esbuildOptions": {
"sourcemap": false,
"outExtension": {
".js": ".js"
}
}
}
}
},
"serve": {
"executor": "@nx/js:node",
"defaultConfiguration": "development",
"dependsOn": ["build"],
"options": {
"buildTarget": "search:build",
"runBuildTargetDependencies": false
},
"configurations": {
"development": {
"buildTarget": "search:build:development"
},
"production": {
"buildTarget": "search:build:production"
}
}
},
"lint": {
"executor": "@nx/eslint:lint"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "apps/search/jest.config.ts"
}
},
"docker-build": {
"dependsOn": ["build"],
"command": "docker build -f apps/search/Dockerfile . -t search"
}
}
}
20 changes: 20 additions & 0 deletions apps/search/src/app/app.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Fastify, { FastifyInstance } from 'fastify';
import { app } from './app';

describe('GET /', () => {
let server: FastifyInstance;

beforeEach(() => {
server = Fastify();
server.register(app);
});

it('should respond with a message', async () => {
const response = await server.inject({
method: 'GET',
url: '/',
});

expect(response.json()).toEqual({ message: 'Hello API' });
});
});
27 changes: 27 additions & 0 deletions apps/search/src/app/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as path from 'path';
import { FastifyInstance } from 'fastify';
import AutoLoad from '@fastify/autoload';

/* eslint-disable-next-line */
export interface AppOptions {}

export async function app(fastify: FastifyInstance, opts: AppOptions) {
// Place here your custom code!

// Do not touch the following lines

// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
options: { ...opts },
});

// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'routes'),
options: { ...opts },
});
}
12 changes: 12 additions & 0 deletions apps/search/src/app/plugins/sensible.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FastifyInstance } from 'fastify';
import fp from 'fastify-plugin';
import sensible from '@fastify/sensible';

/**
* This plugins adds some utilities to handle http errors
*
* @see https://github.com/fastify/fastify-sensible
*/
export default fp(async function (fastify: FastifyInstance) {
fastify.register(sensible);
});
7 changes: 7 additions & 0 deletions apps/search/src/app/routes/root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FastifyInstance } from 'fastify';

export default async function (fastify: FastifyInstance) {
fastify.get('/', async function () {
return { message: 'Hello API' };
});
}
Empty file added apps/search/src/assets/.gitkeep
Empty file.
23 changes: 23 additions & 0 deletions apps/search/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Fastify from 'fastify';
import { app } from './app/app';

const host = process.env.HOST ?? 'localhost';
const port = process.env.PORT ? Number(process.env.PORT) : 3001;

// Instantiate Fastify with some config
const server = Fastify({
logger: true,
});

// Register your application as a normal plugin.
server.register(app);

// Start listening.
server.listen({ port, host }, (err) => {
if (err) {
server.log.error(err);
process.exit(1);
} else {
console.log(`[ ready ] http://${host}:${port}`);
}
});
10 changes: 10 additions & 0 deletions apps/search/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
16 changes: 16 additions & 0 deletions apps/search/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"compilerOptions": {
"esModuleInterop": true
}
}
14 changes: 14 additions & 0 deletions apps/search/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
5 changes: 5 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
},
"@nx/esbuild:esbuild": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
}
},
"namedInputs": {
Expand Down
Loading

0 comments on commit 9aa551e

Please sign in to comment.