Skip to content

About

This is the backend service for the Falai application. It provides the API endpoints for user management, messaging, and other core features.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Falai - Backend

This is the backend service for the Falai application, a real-time chat platform. It is responsible for managing users, authentication, and sending/receiving messages through a RESTful API and WebSockets.

Technologies Used

  • Backend: Node.js, Express.js
  • Database: PostgreSQL
  • ORM: Drizzle ORM
  • Real-time Communication: Socket.IO
  • Authentication: JSON Web Tokens (JWT)
  • Containerization: Docker & Docker Compose
  • Validation and Security: express-rate-limit for request rate limiting.

Prerequisites

Before you begin, ensure you have the following software installed:

For local development without Docker, you will also need:


Getting Started

Follow these instructions to get the project up and running.

1. Clone the Repository

git clone https://github.com/Mikaelnotfound/FALAI-backend.git
cd FALAI-backend

2. Configure Environment Variables

Create a .env file in the project root. You can copy the .env.example if it exists.

# Example of creating the .env file
touch .env

Fill in the .env file with the required variables:

  • PORT: The port on which the backend server will run (e.g., 3000).
  • HOST: The host address the backend server will bind to (e.g., 0.0.0.0 for Docker, 127.0.0.1 for localhost).
  • POSTGRES_USER: Your desired username for the PostgreSQL database.
  • POSTGRES_PASSWORD: A secure password for the PostgreSQL user.
  • POSTGRES_DB: The name of the database.
  • DATABASE_URL: The connection string for the database. It should follow the format: postgresql://<USER>:<PASSWORD>@<HOST>:<PORT>/<DB>
  • JWT_SECRET: A secret key for signing JSON Web Tokens.
  • JWT_EXPIRATION: The expiration time for JWTs (e.g., 1h, 7d).

Example for Docker: When using Docker, the DATABASE_URL host should be the database service name in docker-compose.yml, which is falai-db. DATABASE_URL="postgresql://user:password@falai-db:5432/falai"


Running the Application

Using Docker (Recommended)

  1. Build and Run the Containers:

    npm run docker:start

    This command uses docker-compose.yml to orchestrate the application and database services.

  2. Apply Database Migrations: Once the containers are running, push the Drizzle schema.

    docker-compose exec backend npm run db:push

The application will be available at http://localhost:3000 (or the PORT you specified).

Running Locally

  1. Install Dependencies:

    npm install
  2. Set up PostgreSQL: Ensure your PostgreSQL server is running and create a database with the credentials defined in your .env file.

  3. Apply Database Migrations:

    npm run db:push
  4. Start the Server:

    • For development with auto-reloading:
      npm run dev
    • For production:
      npm start

Available Scripts

  • npm start: Starts the server in production mode.
  • npm run dev: Starts the server in development mode with --watch.
  • npm run docker:start: Starts Docker containers in detached mode.
  • npm run docker:restart: Stops and recreates Docker containers.
  • npm run docker:stop: Stops and removes containers and volumes.
  • npm run docker:logs: Displays logs from the backend container.
  • npm run db:push: Synchronizes the Drizzle schema with the database.
  • npm run db:studio: Opens Drizzle Studio to visualize and manage the database.

API Endpoints

All endpoints requiring authentication need a JWT token in the Authorization header.

Authentication and Users (/)

  • POST /login: Authenticates a user and returns a JWT token.
  • POST /register: Creates a new user.
  • GET /users: (Authenticated) Returns a list of all users.
  • GET /users/:id: (Authenticated) Returns a specific user by ID.
  • DELETE /users/:id: (Authenticated) Deletes a user by ID.

Messages (/messages)

  • GET /messages: (Authenticated) Returns all messages.
  • GET /messages/:id: (Authenticated) Returns a specific message by ID.
  • GET /messages/user/:userId: (Authenticated) Returns all messages from a specific user.
  • POST /messages: (Authenticated) Creates and sends a new message.

Status

  • GET /status: Health check endpoint. Returns "OK!".

Real-time Events (Socket.IO)

The server uses Socket.IO for bidirectional communication.

  • Connection: A client connects to the server.
  • chat message (emission): A client sends a message to the server.
  • chat message (reception): The server retransmits the received message to all other connected clients.
  • user connected: Emitted when a new user connects.
  • user disconnected: Emitted when a user disconnects.

Database

The project uses Drizzle ORM to interact with the PostgreSQL database. The table schemas (users, messages, etc.) are defined in src/database/schema.js.

To interactively visualize your database, run:

npm run db:studio

Stopping the Application

  • With Docker:

    npm run docker:stop
  • Locally: Press Ctrl + C in the terminal where the server is running.

About

This is the backend service for the Falai application. It provides the API endpoints for user management, messaging, and other core features.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages