Skip to content

Commit

Permalink
Merge pull request #15 from vratix-dev/feature/misc-docs
Browse files Browse the repository at this point in the history
FEATURE: Misc docs
  • Loading branch information
ivan-ivanovv authored Nov 13, 2024
2 parents 17826bf + 8b2325b commit 41b3108
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 57 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Vratix API Module Library
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
![GitHub License](https://img.shields.io/github/license/vratix-dev/api-library)
<!-- ![NPM Downloads](https://img.shields.io/npm/dm/vratix) -->

## Overview
Easy-to-use, open-source modules that implement common API logic for seamless integration into Node.js APIs.
Expand Down
10 changes: 5 additions & 5 deletions docs/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ title: The CLI
description: Guide to using our CLI
---

Our CLI offers a quick, hassle-free way to set up a new project or install new API modules with a single command.
Our CLI offers a quick, hassle-free way to set up a new project and install API modules with a single command.

While you’re welcome to copy source code directly from our GitHub repo, the CLI significantly simplifies the setup process by automating file placements, dependency resolutions, and configurations.
While you’re free to copy source code directly from our GitHub repo, the CLI significantly simplifies the setup process by automating file placements, dependency resolutions, and other configurations.

The "raw" source code of API modules in GitHub may appear complex if copied directly. This setup is intentional for development purposes but can be confusing when integrating manually.
The "raw" source code of API modules in GitHub may appear complex if copied directly. This setup is intentional to make the CLI work but can be confusing when integrating manually.

Using the CLI ensures smooth installation by handling:
- Copying files from the module registry
- Installing schema validators and database repositories
- Resolving internal and external dependencies
- Adjusting `import` aliases in each file

## init
## `init`

Use the `init` command to create a new project using our Node.js template or to add a `.config/modules.json` file to an existing project.

Expand All @@ -36,7 +36,7 @@ Options:
-h, --help Display help for the command
```

## add
## `add`

The `add` command allows you to add new API modules to your project. It checks if both `package.json` and `.config/modules.json` exist in the project directory.
If these files are missing, use the [init](#init) command first.
Expand Down
28 changes: 14 additions & 14 deletions docs/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ title: Config File
description: Project configuration
---

When you initialize your API project using our CLI, you will see a `.config/modules.json` file created in your project.
This file includes configurations the CLI uses when adding new modules to your project. See [The CLI](/docs/cli) for more information.
When you initialize your new API project using our CLI, you will see a `.config/modules.json` file created.
It contains configurations the CLI uses when adding new modules to your project. See [The CLI](/docs/cli) for more information.

> **NOTE:** If you're simply copying files from our modules, this configuration file is not required.
> **NOTE:** This configuration file is not required if you're copying code from our GitHub repo.
Most modifications to this file can breatk the CLI’s functionality, but here are a few things you can safely adjust if needed.
Most modifications to this file can break the CLI’s functionality, but here are a few things you can safely adjust if needed.

## packageManger
## `packageManger`

Specifies the package manager used to install dependencies.
Change this if you switch to a different package manager after initial setup.
Expand All @@ -21,7 +21,7 @@ Change this if you switch to a different package manager after initial setup.
}
```

## docker
## `docker`

This property indicates whether Docker containers should be configured when installing new modules.
Set this to `false` if you decide to stop using Docker.
Expand All @@ -32,7 +32,7 @@ Set this to `false` if you decide to stop using Docker.
}
```

## schemaValidator
## `schemaValidator`

Defines the schema validator for request payloads.
If you change the validator in your codebase, update this property so the CLI installs the correct dependencies accordingly.
Expand All @@ -43,7 +43,7 @@ If you change the validator in your codebase, update this property so the CLI in
}
```

## database
## `database`

Set your main database here. If you change databases, update this property so the CLI can install the appropriate DB repositories and schemas.
See **Databases** for supported DB engines.
Expand All @@ -54,14 +54,14 @@ See **Databases** for supported DB engines.
}
```

## folderOverrides
## `folderOverrides`

This section allows path overrides for core files in your Node.js project. Update these properties if you change the folder structure after initialization.

> By default, the CLI considers `./src` as the root directory for all files.
For example, `"controllers": "controllers"` will store controller files in `./src/controllers`.

### folderOverrides.controllers
### `folderOverrides.controllers`

Specifies the folder path for storing controller files, where you’ll implement business logic.

Expand All @@ -73,7 +73,7 @@ Specifies the folder path for storing controller files, where you’ll implement
}
```

### folderOverrides.routes
### `folderOverrides.routes`

Defines the location for endpoint route files.

Expand All @@ -85,7 +85,7 @@ Defines the location for endpoint route files.
}
```

### folderOverrides.middleware
### `folderOverrides.middleware`

Sets the path for middleware files, allowing you to configure custom middleware for Express.js.

Expand All @@ -97,7 +97,7 @@ Sets the path for middleware files, allowing you to configure custom middleware
}
```

### folderOverrides.utils
### `folderOverrides.utils`

Specifies where helper and miscellaneous utility scripts are stored.

Expand All @@ -107,4 +107,4 @@ Specifies where helper and miscellaneous utility scripts are stored.
"utils": "utils"
}
}
```
```
81 changes: 81 additions & 0 deletions docs/deploy.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: Deployment
description: Deployment Guide
---

Deploy your backend service locally or on your cloud service of choice using Docker or directly on your host machine.

## With Docker

If Docker is enabled for your project, a `docker-compose.yaml` file will be available with all necessary container configurations.
Ensure Docker Engine and Docker Compose are installed.

### localhost

**Step 1:** Configure your `.env` file with the necessary environment variables.

**Step 2:** Run the following command to start the service:

```bash
docker compose up -d --build
```

This command performs several actions:

1. **Copies your service files** to the `backend` container.
2. **Builds the project** in the container with `tsup --watch`.
3. **Runs the service** within the container, monitoring for changes and restarting as needed. Refer to the `docker:serve` command in `package.json`.
4. **Builds and runs other containers** (e.g., `postgres` or `ingress-proxy`) as specified in `docker-compose.yaml`.

### Staging/Production

> **Note:** Using a prebuilt container image is recommended for cloud deployments. This guide outlines deployment without a prebuilt image.
If you are using a cloud-provisioned DB resource, you might want to remove any DB containers in staging/production by updating the `include` property in your `docker-compose.yaml`.

**Step 1:** Set `NODE_ENV=production` in your `.env` file to target the production build stage (see `docker-compose.yaml` on line 7).

**Step 2:** Clone your project to the host server and install any required dependencies, such as Docker Compose and `pnpm`.

**Step 3:** Configure your production `.env` with the appropriate variables.

**Step 4 (Optional):** Configure an NGINX proxy for SSL and to handle incoming requests. Refer to the [NGINX guide](/docs/nginx).

**Step 5:** Run the following command to build and start the Docker services:

```bash
docker compose up -d --build
```

## Without Docker

Running the service without Docker simplifies the process, though you may still need a DB container for local development or an NGINX proxy for staging/production.

### localhost

**Step 1:** Configure your `.env` file with required local environment variables.

**Step 2:** Use the following command to start the service:

```bash
npm run dev:local
```

This will:
1. Build the project in watch mode using `tsup --watch`
2. Run the service, automatically restarting on file changes. See the `local:serve` command in `package.json` for configuration.

### Staging/Production

**Step 1:** Clone your project to the production server.

**Step 2:** Configure your production `.env` with the appropriate variables.

**Step 3 (Optional):** Configure a reverse proxy (e.g., NGINX) to handle SSL and incoming requests. Refer to the [NGINX guide](/docs/nginx) for setup.

**Step 4:** Run the following commands to build and start the service:

```bash
npm run build:prod
npm run prod:serve
```
13 changes: 6 additions & 7 deletions docs/install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ description: Set up the CLI

You’re free to copy and use any code from this API Module Library — it's designed to be a foundation you can build on.

To simplify setup and integration, we created a CLI tool that helps you start new projects or integrate our API Modules into existing ones.
The CLI handles imports, configurations, and dependencies automatically, so you can get up and running in minutes.
To simplify setup and integration, we created a CLI tool to help you start new projects or integrate our API Modules into existing ones.
The CLI handles imports, configurations, and dependencies automatically, so you can get up and running in seconds.

### Start a New Project

Expand Down Expand Up @@ -37,6 +37,7 @@ During setup, select any initial API Modules you’d like to install as part of
☐ Auth (Basic)
☐ Stripe Subscriptions
☐ S3 File Upload
...
☐ None
```

Expand All @@ -57,16 +58,14 @@ Customize the paths for main module folders if needed:
### Ready To Go

Once setup is complete, run:
Once setup is complete, to start your service run:

```bash
npm run dev
```

to start your service.

To add additional modules later, simply use:
To add additional modules later, use:

```bash
npx vratix add auth-basic
npx vratix add <module>
```
92 changes: 92 additions & 0 deletions docs/nginx.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: NGINX Configuration Guide
description: Configure an NGINX web proxy for your backend service with SSL support
---

Our Node.js template includes a minimal [NGINX configuration](https://github.com/vratix-dev/api-library/tree/17826bff71a99fc1e74860f3acc9129e27b3dbec/templates/node-ts/nginx)
that works out-of-the-box for most deployments, but you can expand it as needed.

The default template does not include SSL support. Below are detailed instructions on setting up SSL with Let's Encrypt.

## SSL Setup (Optional)

> SSL certificates are generated on the host and then mounted into the NGINX Docker container.
### 1. Install Certbot and Generate SSL Certificates

1. **Install Certbot** (Let's Encrypt client) on your host if it’s not already installed:

```bash
sudo apt install certbot
```

2. **Generate SSL certificates** for your domain. Replace `yourdomain.com` with your actual domain name:

```bash /yourdomain.com/
sudo certbot certonly --standalone -d yourdomain.com -d www.yourdomain.com
```

Certificates will be stored in `/etc/letsencrypt/live/yourdomain.com/`.

### 2. Update `nginx.conf` for SSL

Modify your `nginx.conf` to enable SSL:

1. **Add an HTTPS server block** in `nginx.conf`, configured as follows:
```nginx /yourdomain.com/
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /etc/ssl/certs/fullchain.pem;
ssl_certificate_key /etc/ssl/certs/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://backend:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
2. **Add an HTTP to HTTPS redirect block** (optional):
```nginx /yourdomain.com/
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
```

### 3. Mount Certificates in Docker

Ensure SSL certificates are available to the NGINX container by mounting them in `docker-compose.yml`.
Replace `yourdomain.com` with your domain name:

```yaml /yourdomain.com/
services:
nginx:
volumes:
- ./etc/letsencrypt/live/yourdomain.com:/etc/ssl/certs:ro
```
### 4. Automate SSL Renewal
1. **Open your crontab** to add an automated renewal job:
```bash
sudo crontab -e
```

2. **Add the renewal command** to run daily at 3 AM (or adjust the schedule as desired):

```bash
0 3 * * * certbot renew --quiet && docker compose -f /path-to-your-project/docker-compose.yml restart nginx
```

This setup will check for certificate renewal daily and restart NGINX if renewal occurs.
19 changes: 10 additions & 9 deletions docs/overview.mdx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
---
title: Overview
description: Easy-to-use, open-source modules that implement common API logic for seamless integration into Node.js APIs.
description: Easy-to-use, open-source modules that implement common API logic to simplify backend development
---

We created this library of reusable API modules to streamline API development. As backend developers, we often found ourselves doing repetitive work or copying outdated code from old projects and inconsistent online sources.
We created this library of reusable API modules to simplify API development because we were wasting too much time setting up basic functionality and researching the latest backend best practices.
We wanted a repository of high-quality API modules we can reuse, copy and paste into our projects and have a working backend in seconds.

This led us to build a high-quality repository of reusable API modules that address common functionality used in every backend service.
In the age of AI code assistants, these modules remain reliably crafted by developers, following best practices with minimal assumptions.
This makes it easy for any developer to integrate these modules into any API project with flexibility.
In the age of AI code assistants, the modules are reliably built by us, following best practices with minimal assumptions
so developers can integrate them into any API project and have full ownership and control of their codebase.

Currently, the modules support **Express.js**, and we’re actively working to extend compatibility with other backend languages and popular Node.js frameworks.
Currently, the modules work for **Express.js**, however, we’re actively working to extend compatibility with other backend languages and popular Node.js frameworks.
We would be more than happy for you to contribute and help us achieve this faster.

> **NOTE:** This isn’t just another package; it’s a source code repository you can copy and use — your code, your way.
These modules are designed to serve as a solid foundation for API development, so feel free to adapt them to fit your unique needs.
> This isn’t just another package; it’s a source code repository you can copy and use — your code, your way.
The modules are designed to be a solid foundation for any API service, **you should customize them to fit your unique needs**.

**We recommend using our CLI** to import modules into your codebase. The CLI automates file placement, manages external dependencies, sets up database repositories, and resolves module imports.
**We recommend using our CLI** to import modules into your codebase. It automates file placement, manages external dependencies, sets up database repositories and migrations, and resolves module imports.

Stop reinventing the wheel. Start innovating where it counts.
Loading

0 comments on commit 41b3108

Please sign in to comment.