/watcher-server

A simple web server for storing and fetching data collected by a watcher script

Primary LanguageTypeScriptOtherNOASSERTION

watcher-server

watcher-server is a Node.js application designed to persist and retrieve events generated by a watcher script. It uses Express.js for server functionality and PostgreSQL for database operations. It's also used by a watcher-dashboard to generate data visualizations from collected session events.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

  • Node.js 16
  • PostgreSQL 11.8

Installing

Install the project dependencies:

npm install

Setting Up the Environment

Create a .env file in the project root using the .env.example structure.

Running the Application

To start the application in development mode:

npm run dev

This will start the server with nodemon and ts-node, enabling hot reloading.

For production, first build the application:

npm run build

Then start the server:

npm start

Running with Docker

This application can be easily run using Docker and Docker Compose. This ensures a consistent environment for development and testing. Ensure you have the following installed:

Steps to Run

  1. Make sure you have installed the dependencies using the command npm install and the .env file is set up in the project root as described in the 'Setting Up the Environment' section of this README.

  2. From the root directory of the project, execute the following command to build and start the server along with the PostgreSQL database:

    docker-compose up --build

    This command builds the server service based on Dockerfile.dev and starts the services defined in docker-compose.yml.

    • The server service runs in development mode, executing npm i && npm run dev.
    • The postgres service starts a PostgreSQL 11.8 server on Alpine Linux.
    • Ports, volumes, and environment variables are set as per the configuration in docker-compose.yml.
  3. Once the services are up and running, the server will be accessible at http://localhost:3000. However, this can vary depending on the PORT environment variable set in your .env file. Ensure to use the correct port as specified in your environment configuration.

  4. To stop the Docker containers, press Ctrl+C in the terminal where docker-compose up is running. To remove the containers completely, run:

    docker-compose down

Scripts

  • start: Run the built server.
  • dev: Run the server in development mode with hot reloading.
  • build: Build the project for production.
  • lint: Lint the project files.
  • migrate: Run database migrations.

Project Structure

  • /src: Source files.
  • /build: Production build of the application.
  • /db/migrations: Database migration files.

API Endpoints

The server exposes several API endpoints:

  • GET /healthcheck: Health check endpoint.
  • GET /authorized: Check authorization.
  • GET /apps: Fetch applications.
  • GET /sessions/:appId: Get sessions for an app.
  • GET /events/:sessionId: Get events for a session.
  • POST /events: Add new events.
  • DELETE /events/:sessionId: Delete events for a session.

Database Migrations

Using node-pg-migrate

This project uses node-pg-migrate for handling database migrations. It's essential that any new database schema changes are managed through migrations using this tool to ensure consistency and version control of the database schema.

Creating New Migrations

When you need to create a new database migration, follow these steps:

  1. Generate a new migration file by running:

    npm run migrate -- create migration-name

    Replace migration-name with a descriptive name for your migration. This command will create a new file in the /db/migrations directory with a timestamp as part of the filename.

  2. Open the newly created migration file and implement the up and down methods. The up method should apply your new changes to the database, while the down method should revert those changes.

  3. Ensure to thoroughly test your migration in a development environment before deploying it to production.

  4. After testing, commit the new migration file.

Documentation

For detailed information on how to use node-pg-migrate, including its command-line options and API, refer to the node-pg-migrate documentation.