# Capital Index Database Middleware
## Overview
### About This Project
The **Capital Index Database Middleware** provides an abstraction layer for your database, enhancing query management and change monitoring. It streamlines database interactions while ensuring efficient performance and security.
### Repository Contents
This repository includes:
- **Middleware Source Code** – The core logic for database interaction.
- **Dockerfile** – Configuration for containerized deployment.
- **Install & Management Scripts** – Scripts for streamlined installation and maintenance.
### Table of Content:
- [Capital Index Database Middleware](#capital-index-database-middleware)
- [Overview](#overview)
- [About This Project](#about-this-project)
- [Repository Contents](#repository-contents)
- [Table of Content:](#table-of-content)
- [Getting Started](#getting-started)
- [Installation](#installation)
- [Step 1: Install Required Dependencies](#step-1-install-required-dependencies)
- [Step 2: Run the Setup Script](#step-2-run-the-setup-script)
- [Configuration](#configuration)
- [1. API\_PORT](#1-api_port)
- [2. CONTAINER\_NAME](#2-container_name)
- [3. HAS\_LOCAL\_DBS](#3-has_local_dbs)
- [Test your setup](#test-your-setup)
- [Using the `status` command:](#using-the-status-command)
- [Using the `test_setup` command:](#using-the-test_setup-command)
## Getting Started
### Installation
#### Step 1: Install Required Dependencies
Before installing, ensure you have the necessary dependencies to download and build the Docker container.
Debian/Ubuntu
```bash
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y wget git docker.io
```
[Optional] Install Docker BuildKit:
```bash
sudo apt-get install -y docker-buildx
```
CentOS/Fedora
> **Note:** If using CentOS 7, replace `dnf` with `yum`.
```bash
sudo dnf update -y && sudo dnf upgrade -y
sudo dnf install -y wget git docker
```
[Optional] Install Docker BuildKit:
```bash
sudo dnf install -y docker-buildx
```
Arch Linux
```bash
sudo pacman -Syu --noconfirm
sudo pacman -S --noconfirm wget git docker
```
[Optional] Install Docker BuildKit:
```bash
sudo pacman -S --noconfirm docker-buildx
```
Alpine Linux
```bash
sudo apk update && sudo apk upgrade
sudo apk add --no-cache wget git docker
```
[Optional] Install Docker BuildKit:
```bash
sudo apk add --no-cache docker-cli-buildx
```
#### Step 2: Run the Setup Script
After installing dependencies, configure Docker permissions and run the setup script:
1. Add your user to the Docker group and re-login:
```bash
sudo usermod -aG docker $USER && sudo su - $USER
```
2. Download and execute the `setup.sh` script:
```bash
mkdir -p ~/.db-middleware/scripts \
&& rm -rf ~/.db-middleware/scripts/* \
&& cd ~/.db-middleware/scripts \
&& wget -qO setup.sh https://gitea.abdulhade.com/abdulhade/db-middleware/raw/branch/main/scripts/setup.sh \
&& bash setup.sh
```
3. Give the container permission to edit its files:
```bash
sudo groupadd -g 2000 dbmiddleware
sudo usermod -aG dbmiddleware $USER
sudo chown -R $USER:dbmiddleware /home/$USER/.db-middleware/files
sudo chmod -R 775 /home/$USER/.db-middleware/files
```
---
## Configuration
During installation, you will be prompted to configure the following variables:
### 1. API_PORT
- Defines the port the middleware listens on.
- **Default:** `8080`
- Access within your machine: `http://localhost:8080`
- Ensure this port is forwarded if external access is required.
### 2. CONTAINER_NAME
- Specifies the name of the Docker container running the middleware.
### 3. HAS_LOCAL_DBS
Determines how the middleware connects to databases:
- **Enter `0`** – If all the databases are hosted on a remote server.
- **Enter `1`** – If one or more of the database runs on the same machine.
> **Note:** By default, Docker containers operate on an isolated network. If your database is local, the middleware must be configured to run on the same network to ensure connectivity.
## Test your setup
### Using the `status` command:
Check the status of the container
```bash
$ db-middleware status
```
If the container is stopped, run `db-middleware start` command.
It shows something like this:
```
Config file loaded successfully.
+------------------------------+
| Checking container status... |
+------------------------------+
+--------------------------------------------+
| Database Middleware Status: |
| |
| |
| [Container] |
| |
| Name: con-db-middleware |
| Status: Up 11 minutes |
| |
| [Performance] |
| |
| CPU Usage: 0.33% |
| Memory Usage: 62.27MiB / 3.63GiB (1.68%) |
| Block I/O: 0B / 0B |
| Network I/O: 6.34kB / 4.09kB |
| |
| [Network] |
| |
| Network Mode: bridge |
| IP Address: 172.17.0.2 |
| 172.17.0.2 |
| Ports: 0.0.0.0:8080->8080/tcp |
| |
| [App] |
| |
| Run Command: "bash /app/scripts/run.sh" |
+--------------------------------------------+
```
### Using the `test_setup` command:
If the container is created and running, use the test setup command:
```bash
$ db-middleware test_setup
```
```bash
Config file loaded successfully.
+-------------------------------------------+
| Container 'con-db-middleware' is Found. |
| Container 'con-db-middleware' is running. |
| App returned Ok to ping request. |
+-------------------------------------------+
```
---
For further assistance, refer to the project documentation or open an issue in the repository.