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.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
- Node.js 16
- PostgreSQL 11.8
Install the project dependencies:
npm install
Create a .env
file in the project root using the .env.example
structure.
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
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:
-
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. -
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 onDockerfile.dev
and starts the services defined indocker-compose.yml
.- The
server
service runs in development mode, executingnpm 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
.
- The
-
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. -
To stop the Docker containers, press
Ctrl+C
in the terminal wheredocker-compose up
is running. To remove the containers completely, run:docker-compose down
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.
/src
: Source files./build
: Production build of the application./db/migrations
: Database migration files.
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.
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.
When you need to create a new database migration, follow these steps:
-
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. -
Open the newly created migration file and implement the
up
anddown
methods. Theup
method should apply your new changes to the database, while thedown
method should revert those changes. -
Ensure to thoroughly test your migration in a development environment before deploying it to production.
-
After testing, commit the new migration file.
For detailed information on how to use node-pg-migrate
, including its command-line options and API, refer to the node-pg-migrate documentation.