feat(chat): added base for chat service
added front html nigel in the mud route function with openapi - gen clean up the code a little after pull request
This commit is contained in:
parent
73a4946d17
commit
9ce9fa44e4
122 changed files with 9354 additions and 2615 deletions
2
src/chat/.dockerignore
Normal file
2
src/chat/.dockerignore
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/dist
|
||||
/node_modules
|
||||
8
src/chat/README.md
Normal file
8
src/chat/README.md
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Nginx Configuration
|
||||
|
||||
You want to have a new microservice ?
|
||||
|
||||
Edit/add a file in `conf/locations/`
|
||||
take example on `conf/locations/icons.conf` on how to make a reverse proxy and on how to serve static files
|
||||
|
||||
# Good Luck Have Fun
|
||||
18
src/chat/entrypoint.sh
Normal file
18
src/chat/entrypoint.sh
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
set -x
|
||||
# do anything here
|
||||
|
||||
mkdir -p /volumes/static/chat/
|
||||
|
||||
cp -r /extra/chat.html /volumes/static/chat/chat.html
|
||||
|
||||
cp -r /extra/chat.js /volumes/static/chat/chat.js
|
||||
cp -r /extra/style.css /volumes/static/chat/style.css
|
||||
|
||||
|
||||
# chmod -R a+r /volumes/static/chat || true
|
||||
|
||||
# run the CMD [ ... ] from the dockerfile
|
||||
exec "$@"
|
||||
0
src/chat/extra/.gitkeep
Normal file
0
src/chat/extra/.gitkeep
Normal file
4
src/chat/extra/chat.html
Normal file
4
src/chat/extra/chat.html
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<!-- OLD HTML to be deleted later -->
|
||||
</html>
|
||||
1
src/chat/extra/chat.js
Normal file
1
src/chat/extra/chat.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
// OLD JS file to be deleted later
|
||||
2
src/chat/extra/style.css
Normal file
2
src/chat/extra/style.css
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/*! tailwindcss v4.1.16 | MIT License | https://tailwindcss.com */
|
||||
/* Old css to be deleted later */
|
||||
130
src/chat/openapi.json
Normal file
130
src/chat/openapi.json
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
{
|
||||
"openapi": "3.1.0",
|
||||
"info": {
|
||||
"version": "9.6.1",
|
||||
"title": "@fastify/swagger"
|
||||
},
|
||||
"components": {
|
||||
"schemas": {}
|
||||
},
|
||||
"paths": {
|
||||
"/api/chat/test": {
|
||||
"get": {
|
||||
"operationId": "chatTest",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Default Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"kind",
|
||||
"msg",
|
||||
"payload"
|
||||
],
|
||||
"properties": {
|
||||
"kind": {
|
||||
"enum": [
|
||||
"success"
|
||||
]
|
||||
},
|
||||
"msg": {
|
||||
"enum": [
|
||||
"chat.success"
|
||||
]
|
||||
},
|
||||
"payload": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"name",
|
||||
"id",
|
||||
"guest"
|
||||
],
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"guest": {
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"401": {
|
||||
"description": "Default Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"kind",
|
||||
"msg"
|
||||
],
|
||||
"properties": {
|
||||
"kind": {
|
||||
"enum": [
|
||||
"notLoggedIn"
|
||||
]
|
||||
},
|
||||
"msg": {
|
||||
"enum": [
|
||||
"auth.noCookie",
|
||||
"auth.invalidKind",
|
||||
"auth.noUser",
|
||||
"auth.invalid"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"required": [
|
||||
"kind",
|
||||
"msg"
|
||||
],
|
||||
"properties": {
|
||||
"kind": {
|
||||
"enum": [
|
||||
"notLoggedIn"
|
||||
]
|
||||
},
|
||||
"msg": {
|
||||
"enum": [
|
||||
"auth.noCookie",
|
||||
"auth.invalidKind",
|
||||
"auth.noUser",
|
||||
"auth.invalid"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"servers": [
|
||||
{
|
||||
"url": "https://local.maix.me:8888",
|
||||
"description": "direct from docker"
|
||||
},
|
||||
{
|
||||
"url": "https://local.maix.me:8000",
|
||||
"description": "using fnginx"
|
||||
}
|
||||
]
|
||||
}
|
||||
43
src/chat/package.json
Normal file
43
src/chat/package.json
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
{
|
||||
"type": "module",
|
||||
"private": false,
|
||||
"name": "chat",
|
||||
"version": "1.0.0",
|
||||
"description": "This project was bootstrapped with Fastify-CLI.",
|
||||
"main": "app.ts",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "npm run build && node dist/run.js",
|
||||
"build": "vite build",
|
||||
"build:prod": "vite build --outDir=/dist --minify=true --sourcemap=false",
|
||||
"build:openapi": "VITE_ENTRYPOINT=src/openapi.ts vite build && node dist/openapi.cjs >openapi.json"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@fastify/autoload": "^6.3.1",
|
||||
"@fastify/formbody": "^8.0.2",
|
||||
"@fastify/multipart": "^9.3.0",
|
||||
"@fastify/sensible": "^6.0.3",
|
||||
"@fastify/static": "^8.3.0",
|
||||
"@fastify/websocket": "^11.2.0",
|
||||
"@sinclair/typebox": "^0.34.41",
|
||||
"@tailwindcss/vite": "^4.1.17",
|
||||
"esbuild": "^0.24.2",
|
||||
"fastify": "^5.6.2",
|
||||
"fastify-cli": "^7.4.1",
|
||||
"fastify-plugin": "^5.1.0",
|
||||
"jose": "^6.1.1",
|
||||
"sharp": "^0.34.5",
|
||||
"tailwindcss": "^4.1.17"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.19.1",
|
||||
"rollup-plugin-node-externals": "^8.1.2",
|
||||
"vite": "^7.2.2",
|
||||
"vite-tsconfig-paths": "^5.1.4"
|
||||
}
|
||||
}
|
||||
45
src/chat/src/app.ts
Normal file
45
src/chat/src/app.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { FastifyPluginAsync } from 'fastify';
|
||||
import fastifyFormBody from '@fastify/formbody';
|
||||
import fastifyMultipart from '@fastify/multipart';
|
||||
import * as db from '@shared/database';
|
||||
import * as auth from '@shared/auth';
|
||||
import * as swagger from '@shared/swagger';
|
||||
import * as utils from '@shared/utils';
|
||||
|
||||
declare const __SERVICE_NAME: string;
|
||||
|
||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
|
||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
|
||||
|
||||
// When using .decorate you have to specify added properties for Typescript
|
||||
declare module 'fastify' {
|
||||
export interface FastifyInstance {
|
||||
image_store: string;
|
||||
}
|
||||
}
|
||||
|
||||
const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
||||
void opts;
|
||||
await fastify.register(utils.useMakeResponse);
|
||||
await fastify.register(swagger.useSwagger, { service: __SERVICE_NAME });
|
||||
await fastify.register(db.useDatabase as FastifyPluginAsync, {});
|
||||
await fastify.register(auth.jwtPlugin as FastifyPluginAsync, {});
|
||||
await fastify.register(auth.authPlugin as FastifyPluginAsync, {});
|
||||
|
||||
// Place here your custom code!
|
||||
for (const plugin of Object.values(plugins)) {
|
||||
void fastify.register(plugin as FastifyPluginAsync, {});
|
||||
}
|
||||
for (const route of Object.values(routes)) {
|
||||
void fastify.register(route as FastifyPluginAsync, {});
|
||||
}
|
||||
|
||||
void fastify.register(fastifyFormBody, {});
|
||||
void fastify.register(fastifyMultipart, {});
|
||||
fastify.get('/monitoring', () => 'Ok');
|
||||
};
|
||||
|
||||
export default app;
|
||||
export { app };
|
||||
21
src/chat/src/openapi.ts
Normal file
21
src/chat/src/openapi.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import f, { FastifyPluginAsync } from 'fastify';
|
||||
import * as swagger from '@shared/swagger';
|
||||
import * as auth from '@shared/auth';
|
||||
|
||||
declare const __SERVICE_NAME: string;
|
||||
|
||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
||||
const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
|
||||
|
||||
async function start() {
|
||||
const fastify = f({ logger: false });
|
||||
await fastify.register(auth.authPlugin, { onlySchema: true });
|
||||
await fastify.register(swagger.useSwagger, { service: __SERVICE_NAME });
|
||||
|
||||
for (const route of Object.values(routes)) {
|
||||
await fastify.register(route as FastifyPluginAsync, {});
|
||||
}
|
||||
await fastify.ready();
|
||||
console.log(JSON.stringify(fastify.swagger(), undefined, 4));
|
||||
}
|
||||
start();
|
||||
16
src/chat/src/plugins/README.md
Normal file
16
src/chat/src/plugins/README.md
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# Plugins Folder
|
||||
|
||||
Plugins define behavior that is common to all the routes in your
|
||||
application. Authentication, caching, templates, and all the other cross
|
||||
cutting concerns should be handled by plugins placed in this folder.
|
||||
|
||||
Files in this folder are typically defined through the
|
||||
[`fastify-plugin`](https://github.com/fastify/fastify-plugin) module,
|
||||
making them non-encapsulated. They can define decorators and set hooks
|
||||
that will then be used in the rest of your application.
|
||||
|
||||
Check out:
|
||||
|
||||
* [The hitchhiker's guide to plugins](https://fastify.dev/docs/latest/Guides/Plugins-Guide/)
|
||||
* [Fastify decorators](https://fastify.dev/docs/latest/Reference/Decorators/).
|
||||
* [Fastify lifecycle](https://fastify.dev/docs/latest/Reference/Lifecycle/).
|
||||
10
src/chat/src/plugins/sensible.ts
Normal file
10
src/chat/src/plugins/sensible.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import fp from 'fastify-plugin';
|
||||
import sensible, { FastifySensibleOptions } from '@fastify/sensible';
|
||||
/**
|
||||
* This plugins adds some utilities to handle http errors
|
||||
*
|
||||
* @see https://github.com/fastify/fastify-sensible
|
||||
*/
|
||||
export default fp<FastifySensibleOptions>(async (fastify) => {
|
||||
fastify.register(sensible);
|
||||
});
|
||||
31
src/chat/src/routes/nginx-chat.ts
Normal file
31
src/chat/src/routes/nginx-chat.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { FastifyPluginAsync } from 'fastify';
|
||||
import { MakeStaticResponse, typeResponse } from '@shared/utils';
|
||||
import { Type } from '@sinclair/typebox';
|
||||
|
||||
export const ChatRes = {
|
||||
200: typeResponse('success', 'chat.success', {
|
||||
name: Type.String(),
|
||||
id: Type.String(),
|
||||
guest: Type.Boolean(),
|
||||
}),
|
||||
};
|
||||
|
||||
export type ChatResType = MakeStaticResponse<typeof ChatRes>;
|
||||
|
||||
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
|
||||
fastify.get(
|
||||
'/api/chat/test',
|
||||
{
|
||||
schema: {
|
||||
response: ChatRes,
|
||||
operationId: 'chatTest',
|
||||
},
|
||||
config: { requireAuth: true },
|
||||
},
|
||||
async (req, res) => {
|
||||
console.log('/api/chat called =================>');
|
||||
res.makeResponse(200, 'success', 'chat.success', { name: req.authUser!.name, 'id': req.authUser!.id, guest: false });
|
||||
},
|
||||
);
|
||||
};
|
||||
export default route;
|
||||
36
src/chat/src/run.ts
Normal file
36
src/chat/src/run.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
// this sould only be used by the docker file !
|
||||
|
||||
import fastify, { FastifyInstance } from 'fastify';
|
||||
import app from './app';
|
||||
|
||||
const start = async () => {
|
||||
const envToLogger = {
|
||||
development: {
|
||||
transport: {
|
||||
target: 'pino-pretty',
|
||||
options: {
|
||||
translateTime: 'HH:MM:ss Z',
|
||||
ignore: 'pid,hostname',
|
||||
},
|
||||
},
|
||||
},
|
||||
production: true,
|
||||
test: false,
|
||||
};
|
||||
|
||||
const f: FastifyInstance = fastify({ logger: envToLogger.development });
|
||||
try {
|
||||
process.on('SIGTERM', () => {
|
||||
f.log.info('Requested to shutdown');
|
||||
process.exit(134);
|
||||
});
|
||||
console.log('-------->Serving static files from:');
|
||||
await f.register(app, {});
|
||||
await f.listen({ port: 80, host: '0.0.0.0' });
|
||||
}
|
||||
catch (err) {
|
||||
f.log.error(err);
|
||||
process.exit(1);
|
||||
};
|
||||
};
|
||||
start();
|
||||
15
src/chat/tsconfig.json
Normal file
15
src/chat/tsconfig.json
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// {
|
||||
// "extends": "../tsconfig.base.json",
|
||||
// "compilerOptions": {
|
||||
// "skipLibCheck": true, // skips type checking for all .d.ts files
|
||||
// "moduleResolution": "node",
|
||||
// "esModuleInterop": true,
|
||||
// "types": ["node"] },
|
||||
// "include": ["src/**/*.ts"]
|
||||
// }
|
||||
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
"compilerOptions": {},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
53
src/chat/vite.config.js
Normal file
53
src/chat/vite.config.js
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { defineConfig } from 'vite';
|
||||
import tsconfigPaths from 'vite-tsconfig-paths';
|
||||
import nodeExternals from 'rollup-plugin-node-externals';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
|
||||
function collectDeps(...pkgJsonPaths) {
|
||||
const allDeps = new Set();
|
||||
for (const pkgPath of pkgJsonPaths) {
|
||||
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
||||
for (const dep of Object.keys(pkg.dependencies || {})) {
|
||||
allDeps.add(dep);
|
||||
}
|
||||
for (const peer of Object.keys(pkg.peerDependencies || {})) {
|
||||
allDeps.add(peer);
|
||||
}
|
||||
}
|
||||
return Array.from(allDeps);
|
||||
};
|
||||
|
||||
const externals = collectDeps(
|
||||
'./package.json',
|
||||
'../@shared/package.json',
|
||||
);
|
||||
|
||||
export default defineConfig({
|
||||
root: __dirname,
|
||||
define: {
|
||||
__SERVICE_NAME: '"chat"',
|
||||
},
|
||||
// service root
|
||||
plugins: [tsconfigPaths(), nodeExternals()],
|
||||
build: {
|
||||
ssr: true,
|
||||
outDir: 'dist',
|
||||
emptyOutDir: true,
|
||||
lib: {
|
||||
entry: path.resolve(__dirname, process.env.VITE_ENTRYPOINT ?? 'src/run.ts'),
|
||||
// adjust main entry
|
||||
formats: ['cjs'],
|
||||
// CommonJS for Node.js
|
||||
fileName: () => 'index.js',
|
||||
},
|
||||
rollupOptions: {
|
||||
external: externals,
|
||||
},
|
||||
target: 'node22',
|
||||
// or whatever Node version you use
|
||||
sourcemap: true,
|
||||
minify: false,
|
||||
// for easier debugging
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue