Code Monkey home page Code Monkey logo

fatsy's Introduction

Fatsy

Fastify Server Template done in TypeScript.

Routing convention

Fatsy is using the same route definition standard as in Foxx Builder, Umi, Ice, and many others:

/routes
-- get.js
-- /echo
---- get.js
-- /users
---- /$id
------- get.js
---- get.js
---- post.js

The routes folder should contain all your routes. Each route is a folder with a name that is the same as the route path. The route path is the path that is used in the browser to access the route. For example, if you have a route /users, then the route path is /users. If your route has a parameter, then the route path contains a $ followed by the name of the parameter.

Each route folder can contain one or more files. Each file is a route handler. The name of the file is the HTTP method that the route handler handles. For example, if you have a route handler that handles the POST method, then the name of the file is post.js. If you have a route handler that handles the GET method, then the name of the file is get.js.

Support for websockets

In order to support websockets, you need to create a file ws.js in the route folder. This file will be used as a websocket handler.

The following example demonstrates how to create a websocket handler that will use OpenAI to answer questions from the user. It is using Weaviate as a vector store for the document retrieval and index.

import weaviate from 'weaviate-ts-client';
import {WeaviateStore} from 'langchain/vectorstores/weaviate';
import {OpenAIEmbeddings} from 'langchain/embeddings/openai';

const ws: FatsyDynamicRoute = {
    private: false,
    handler: async (connection: SocketStream, _request: FastifyRequest) =>
    {
        const client = weaviate.client({
            scheme: process.env.WEAVIATE_SCHEME || 'http',
            host: process.env.WEAVIATE_HOST || 'localhost'
        });

        const store = await WeaviateStore.fromExistingIndex(new OpenAIEmbeddings(), {
            client,
            indexName: 'Test',
            textKey: 'content',
            metadataKeys: ['productId', 'versionId']
        });

        const chat = new OpenAI({
            streaming: true,
            callbacks: [
                {
                    handleLLMNewToken(token)
                    {
                        connection.socket.send(token);
                    }
                }
            ]
        });

        const chain = ConversationalRetrievalQAChain.fromLLM(
            chat,
            store.asRetriever()
        );

        connection.socket.on('message', async (message: string) =>
        {
            await chain.call({
                question: message.toString(),
                chat_history: []
            });
        });

        connection.socket.on('close', () =>
        {
            console.log('Client disconnected');
        });
    }
};

export default ws;

Config is in YAML format

No more confusing JSON, - simple and readable YAML config

app:
  title: My demo API server

server:
  host: "0.0.0.0"
  port: 3000
  logger:
    level: trace

Core features available

  • Application routing convention similar to the one we use on Foxx Builder
  • Application configuration with load order to the config npm package, so the first one loads is default, then local, then your NODE_ENV profile
  • Templating engine - by default, there is Handlebars bundled for you
  • S3 storage support (Example)
  • Websockets support

Environments that were tested

  • Runs in standalone mode
  • Runs in Docker
  • Runs in Amazon ECS
  • Runs in amazon ECS via HashiCorp waypoint

Useful links

fatsy's People

Contributors

dependabot[bot] avatar renovate[bot] avatar skitsanos avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

fatsy's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

docker-compose
docker-compose.yaml
dockerfile
Dockerfile
github-actions
.github/workflows/dependabot-automerge.yml
  • dependabot/fetch-metadata v1.6.0
.github/workflows/pr.yaml
npm
package.json
  • @aws-sdk/client-s3 ^3.325.0
  • @aws-sdk/s3-request-presigner ^3.325.0
  • @fastify/cookie ^9.0.4
  • @fastify/jwt ^8.0.0
  • @fastify/static ^7.0.0
  • @fastify/view ^9.0.0
  • @fastify/websocket ^9.0.0
  • fastify ^4.17.0
  • fastify-file-upload ^4.0.0
  • fs-extra ^11.1.1
  • handlebars ^4.7.7
  • node-forge ^1.3.1
  • yaml ^2.2.2
  • @istanbuljs/nyc-config-typescript ^1.0.2
  • @swc-node/register ^1.6.5
  • @swc/core ^1.3.56
  • @types/fs-extra ^11.0.1
  • @types/node ^20.1.0
  • @types/node-forge ^1.3.2
  • cpy-cli ^5.0.0
  • esbuild ^0.20.0
  • esbuild-register ^3.4.2
  • nyc ^15.1.0
  • typescript ^5.0.4

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.