-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Comprehensive and detailed documentation was written for gland.
- Loading branch information
Showing
12 changed files
with
891 additions
and
7 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# Getting Started with @medishn/gland | ||
|
||
Welcome to the getting started guide for `@medishn/gland`, a lightweight, modular web framework for building scalable, high-performance Node.js applications. This guide will walk you through the installation, setup, and usage of `gland`, including its built-in support for logging, database queries, and routing. | ||
|
||
## Prerequisites | ||
|
||
Before starting, ensure you have the following: | ||
|
||
- **Node.js** (version 14 or higher) | ||
- **npm** or **yarn** for package management | ||
- **SQL Database** (MySQL, PostgreSQL, or MariaDB) if using the database query functionality | ||
|
||
## Installation | ||
|
||
The `@medishn/gland` package includes built-in integrations for logging and SQL database queries. When you install `gland`, the necessary dependencies are automatically included. | ||
|
||
To install `gland`, run the following command in your project directory: | ||
|
||
```bash | ||
npm install @medishn/gland | ||
``` | ||
|
||
This will install `@medishn/gland` along with its internal dependencies: | ||
|
||
- `@medishn/gland-logger` – for logging | ||
- `@medishn/gland-qiu` – for SQL database querying | ||
|
||
## Basic Application Setup | ||
|
||
The following is a step-by-step guide to setting up a basic application using `gland`. | ||
|
||
### Step 1: Create a Web Server | ||
|
||
Start by creating an entry file for your server, such as `app.ts`: | ||
|
||
```typescript | ||
import gland from '@medishn/gland'; | ||
import path from 'path'; | ||
|
||
const app = new gland(); | ||
|
||
// Load the configuration file that specifies routes, middlewares, and other settings | ||
app.load(path.join(__dirname, '.confmodule')); | ||
|
||
// Initialize the server on port 3000 | ||
app.init(3000, () => { | ||
console.log('Server is running on port 3000'); | ||
}); | ||
``` | ||
|
||
### Step 2: Configure the Server | ||
|
||
You can configure routes and other settings using a configuration file (`.confmodule`). This file defines where your route files and other modules are located. For example, in your `.confmodule`, you can specify the path to the router: | ||
|
||
``` | ||
path=router | ||
``` | ||
|
||
### Step 3: Define Routes | ||
|
||
Create a route module in the `router` directory. For instance, `router/index.ts` could contain a simple route that responds with "Hello, world!": | ||
|
||
```typescript | ||
import { Context, Get, Route } from '@medishn/gland'; | ||
|
||
@Route('/') | ||
export class Test { | ||
@Get() | ||
test(ctx: Context) { | ||
ctx.write('Hello, world!'); | ||
ctx.end(); | ||
} | ||
} | ||
``` | ||
|
||
### Step 4: Run the Server | ||
|
||
Now you can run your server using `ts-node` or transpile your TypeScript files and run the generated JavaScript: | ||
|
||
```bash | ||
ts-node app.ts | ||
``` | ||
|
||
Your web server will be running at `http://localhost:3000/`, and visiting this URL should return the message "Hello, world!". | ||
|
||
## Using the Built-in Logger | ||
|
||
`gland` integrates with the `@medishn/gland-logger` package, providing flexible logging capabilities. You can log messages to the console or files, and configure options like log level and file rotation. | ||
|
||
### Example: Basic Logger Setup | ||
|
||
In your application, configure the logger like this: | ||
|
||
```typescript | ||
import gland from '@medishn/gland'; | ||
|
||
const app = new gland(); | ||
|
||
// Logger options | ||
const options = { | ||
level: 'info', // Set the log level | ||
transports: ['file', 'console'], // Log to both file and console | ||
file: 'combined.log', // Log file path | ||
rotation: { | ||
maxSize: 1024 * 1024, // 1 MB max file size for rotation | ||
}, | ||
}; | ||
|
||
const logger = new app.logger(options); | ||
|
||
// Use the logger | ||
logger.log('Server started', 'info'); // Logs an informational message | ||
logger.log('An error occurred', 'error'); // Logs an error message | ||
``` | ||
|
||
Logs will be written to the console and to the file specified in the `file` option (`combined.log` in this example). | ||
|
||
## Using SQL Database Queries with Qiu | ||
|
||
`gland` also includes `Qiu`, a query runner for interacting with SQL databases like MySQL, PostgreSQL, and MariaDB. | ||
|
||
### Example: Connecting to a PostgreSQL Database | ||
|
||
In your application, initialize the `Qiu` query runner and connect to a PostgreSQL database: | ||
|
||
```typescript | ||
import gland from '@medishn/gland'; | ||
|
||
const app = new gland(); | ||
|
||
// Initialize the Qiu query runner | ||
const db = new app.Qiu('pg', 'postgresql://username:password@localhost:5432/mydatabase'); | ||
|
||
// Set the database to use | ||
db.use('mydatabase'); | ||
|
||
// Run a query to fetch all users | ||
async function getUsers() { | ||
const users = await db.exec('SELECT * FROM users;'); | ||
console.log(users); | ||
} | ||
|
||
getUsers(); | ||
``` | ||
|
||
You can easily switch between different SQL databases by changing the database connection string in the `Qiu` constructor. For example, to use MySQL: | ||
|
||
```typescript | ||
const db = new app.Qiu('mysql', 'mysql://username:password@localhost:3306/mydatabase'); | ||
``` | ||
|
||
## Adding Middleware | ||
|
||
`gland` supports middleware, which allows you to intercept and process requests before they reach your route handlers. You can define global middleware that applies to all routes or route-specific middleware. | ||
|
||
### Example: Adding Global Middleware | ||
|
||
```typescript | ||
import gland, { Context, NxtFunction } from '@medishn/gland'; | ||
|
||
const app = new gland(); | ||
|
||
// Define a simple logging middleware | ||
app.use(async (ctx: Context, next: NxtFunction) => { | ||
console.log(`${ctx.method} ${ctx.url}`); | ||
await next(); | ||
}); | ||
|
||
// Load routes and start the server | ||
app.load(path.join(__dirname, '.confmodule')); | ||
app.init(3000, () => { | ||
console.log('Server is running on port 3000'); | ||
}); | ||
``` | ||
|
||
In this example, every incoming request will log the HTTP method and URL before reaching the route handler. | ||
|
||
## Conclusion | ||
|
||
This guide has provided an introduction to getting started with `@medishn/gland`. With its modular architecture, support for logging and database queries, and robust routing system, `gland` is a powerful framework for building web applications. | ||
|
||
Explore the [API Documentation](./api) for detailed information about the available classes, methods, and decorators. | ||
|
||
### Next Steps | ||
|
||
- Explore the [Examples](../examples) folder for more use cases. | ||
- Learn how to add your own [Custom Middleware](./4.middleware.md). | ||
- Dive into the [Routing System](./3.routing.md) for advanced routing features. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# Configuration Guide for @medishn/gland | ||
|
||
This guide provides a detailed overview of the configuration options available in `@medishn/gland`, allowing you to customize the behavior of your web server, routing, logging, middleware, and database query execution. These configurations can be defined in a separate configuration file, typically named `.confmodule`, and loaded into your application. | ||
|
||
## Table of Contents | ||
|
||
- [Configuration Guide for @medishn/gland](#configuration-guide-for-medishngland) | ||
- [Table of Contents](#table-of-contents) | ||
- [Introduction](#introduction) | ||
- [Basic Configuration](#basic-configuration) | ||
- [Configuration Options](#configuration-options) | ||
- [Server Configuration](#server-configuration) | ||
- [Example](#example) | ||
- [Logging Configuration](#logging-configuration) | ||
- [Example](#example-1) | ||
- [Database Configuration (Qiu)](#database-configuration-qiu) | ||
- [Example](#example-2) | ||
- [Loading Configuration](#loading-configuration) | ||
- [Best Practices](#best-practices) | ||
|
||
--- | ||
|
||
## Introduction | ||
|
||
`gland` supports both programmatic and file-based configuration to allow flexible and maintainable setups. The framework uses the `.confmodule` file for configuration, which can include details about routing, middleware, logging, and database connections. This file is loaded during server initialization. | ||
|
||
The configuration system is designed to: | ||
|
||
- Define paths for routes and middleware. | ||
- Set up logging preferences (log levels, transport options, etc.). | ||
- Configure SQL database connections for `Qiu`. | ||
- Customize global or route-specific middleware. | ||
|
||
## Basic Configuration | ||
|
||
Below is an example of a basic `.confmodule` configuration file: | ||
|
||
``` | ||
path=router | ||
recursive=true | ||
pattern=*.ts | ||
cacheModules=true | ||
logLevel=info | ||
``` | ||
|
||
In this example: | ||
- **`path=router`** specifies the directory where route modules are located. | ||
- **`recursive=true`** indicates that the framework should search for route files recursively. | ||
- **`pattern=*.ts`** sets the file pattern to match for route files. | ||
- **`cacheModules=true`** enables module caching. | ||
- **`logLevel=info`** sets the logging level to `info`. | ||
|
||
## Configuration Options | ||
|
||
The `.confmodule` file can contain several key configuration options, each affecting different parts of the application. | ||
|
||
### Server Configuration | ||
|
||
The server configuration controls how the web server behaves and interacts with routes and middleware. Key server settings include: | ||
|
||
| Option | Type | Description | Default Value | | ||
|-------------------|---------|---------------------------------------------------------------------------------------|---------------| | ||
| `path` | String | Specifies the directory where route modules are located. | `'router'` | | ||
| `recursive` | Boolean | Whether to recursively search for route files in subdirectories. | `true` | | ||
| `pattern` | String | File pattern to match for route modules (e.g., `*.ts`, `*.js`). | `*.ts` | | ||
| `cacheModules` | Boolean | Enables or disables caching of route and middleware modules for better performance. | `true` | | ||
| `logLevel` | String | Defines the level of logging (`error`, `warn`, `info`, `debug`). | `'info'` | | ||
|
||
#### Example | ||
|
||
```ini | ||
path=router | ||
recursive=true | ||
pattern=*.js | ||
cacheModules=false | ||
logLevel=debug | ||
``` | ||
|
||
### Logging Configuration | ||
|
||
Logging configuration defines how logs are captured, where they are stored, and how they are formatted. `gland` uses the `@medishn/gland-logger` package for logging, which can log to both the console and files with support for log rotation. | ||
|
||
| Option | Type | Description | Default Value | | ||
|------------------|---------|--------------------------------------------------------------------------------------|---------------| | ||
| `logLevel` | String | Defines the log level (`info`, `warn`, `error`, `debug`). | `'info'` | | ||
| `transports` | Array | Specifies where to log (`console`, `file`). | `['console']` | | ||
| `file` | String | Path to the log file (e.g., `combined.log`). | `null` | | ||
| `rotation` | Object | Configuration for log rotation, including `maxSize` for file size limits. | `null` | | ||
|
||
#### Example | ||
|
||
```json | ||
{ | ||
"logLevel": "debug", | ||
"transports": ["file", "console"], | ||
"file": "app.log", | ||
"rotation": { | ||
"maxSize": 1048576 // 1MB | ||
} | ||
} | ||
``` | ||
|
||
This configuration logs to both the console and a file (`app.log`). Log files are rotated when they exceed 1 MB in size. | ||
|
||
### Database Configuration (Qiu) | ||
|
||
`gland` includes built-in support for SQL databases via `Qiu`, which allows you to run SQL queries for databases like MySQL, PostgreSQL, and MariaDB. | ||
|
||
| Option | Type | Description | Default Value | | ||
|-------------|---------|----------------------------------------------------------------|---------------| | ||
| `driver` | String | Database driver (`pg`, `mysql`, `mariadb`). | `'pg'` | | ||
| `url` | String | Database connection URL (e.g., `postgresql://...`). | `null` | | ||
| `dbName` | String | Name of the database to use. | `null` | | ||
|
||
#### Example | ||
|
||
```json | ||
{ | ||
"driver": "mysql", | ||
"url": "mysql://user:password@localhost:3306/mydatabase", | ||
"dbName": "mydatabase", | ||
} | ||
``` | ||
|
||
In this example, `Qiu` connects to a MySQL database hosted locally, using a connection pool of 10 connections. | ||
|
||
### Loading Configuration | ||
|
||
You can load the configuration file (`.confmodule`) in your main application file using the `load()` method. | ||
|
||
```typescript | ||
import gland from '@medishn/gland'; | ||
import path from 'path'; | ||
|
||
const app = new gland(); | ||
|
||
// Load configuration | ||
app.load(path.join(__dirname, '.confmodule')); | ||
|
||
// Initialize the server | ||
app.init(3000, () => { | ||
console.log('Server running on port 3000'); | ||
}); | ||
``` | ||
|
||
### Best Practices | ||
|
||
- **Use separate configuration files for different environments (development, production, etc.).** | ||
You can name them `.confmodule.dev`, `.confmodule.prod`, and load the appropriate configuration based on the environment. | ||
|
||
- **Avoid hardcoding sensitive information** (e.g., database credentials) in your configuration files. Instead, use environment variables or a secrets management system. | ||
|
||
- **Use the `cacheModules` option in production** to improve performance by caching route and middleware modules. |
Oops, something went wrong.