Skip to content

Commit

Permalink
chore: release version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mdy-m committed Sep 16, 2024
1 parent 0a30139 commit 9c372a0
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 296 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ To get started with `@medishn/gland`, follow these steps:
```

2. **Define Routes and Handlers**
Create a `.confmodule` file to configure your routes, caching, and file-watching behavior. This file allows you to dynamically load and configure modules in your application. Below is an example configuration file:

Create a `.confmodule` file with the following content:

```
path=router
```
```conf
path = path.join(__dirname, 'router');
router {
[0]: 'index.ts';
[1]: 'test.ts';
}
cache = true;
watch = true;
```

3. **Create Router:(/router/example.ts)**
```typescript
Expand Down
18 changes: 17 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,20 @@ Initial release with the following features:
- Currently, the load method accepts arguments in the form of objects and there is no need to load a file.

### Changed
- logger and qiu dependencies are removed along with their documents
- logger and qiu dependencies are removed along with their documents

## [1.1.0] - 2024-09-16

### Added
- **Batch Loading Support**: Implemented batch loading of routes with a configurable batch size of 10 to improve the efficiency of module loading, especially with a large number of routes.
- **File Watcher**: Added a file watcher feature to reload modules dynamically when the files change. This can be enabled via the `watch` option in the configuration.
- **Route Parsing**: Enhanced configuration parsing to support the definition of multiple routes under the `router` section. The routes are automatically resolved based on the provided configuration file.

### Changed
- **Improved Configuration Handling**: The module loader now merges the default configuration with the provided configuration, ensuring proper handling of missing or optional fields like `path`, `routes`, `cache`, and `watch`.

### Fixed
- **Error Handling**: Improved error handling for missing routes in the configuration file. A clear error message is now thrown if no routes are specified.

### Performance Improvements
- **Module Caching**: Added caching logic to prevent redundant module imports, leading to faster performance when caching is enabled.
148 changes: 87 additions & 61 deletions docs/documents/guides/2.configuration.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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.
This guide provides a comprehensive overview of the `.confmodule` configuration file, which is used to customize the behavior of the `@medishn/gland` framework. The `.confmodule` allows you to define routing, caching, and file-watching settings that control how the framework operates.

## Table of Contents

Expand All @@ -9,92 +9,118 @@ This guide provides a detailed overview of the configuration options available i
- [Introduction](#introduction)
- [Basic Configuration](#basic-configuration)
- [Configuration Options](#configuration-options)
- [Server Configuration](#server-configuration)
- [Example](#example)
- [Loading Configuration](#loading-configuration)
- [Best Practices](#best-practices)
- [Path Configuration](#path-configuration)
- [Router Configuration](#router-configuration)
- [Cache](#cache)
- [Watch](#watch)
- [Example](#example)
- [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.
`@medishn/gland` supports flexible configuration through the `.confmodule` file, allowing you to define routing paths, module caching, and file-watching behavior. This file is loaded when the framework starts, providing an easy way to configure the application without hardcoding settings.

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.
Key use cases for the `.confmodule` file include:
- Defining where your route files are located.
- Specifying which files should be loaded as routes.
- Enabling or disabling module caching.
- Enabling file watching for automatic reloads when files change.

## Basic Configuration

Below is an example of a basic `.confmodule` configuration file:
The `.confmodule` file uses a simple key-value format to specify configuration options. Below is an example of a basic configuration file:

```ini
path = path.join(__dirname, 'router');
router {
[0]: 'index.ts';
[1]: 'test.ts';
}
cache = true;
watch = false;
```
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.
The following sections describe the available options in the `.confmodule` file in detail.

### Server Configuration
### Path Configuration

The server configuration controls how the web server behaves and interacts with routes and middleware. Key server settings include:
- **`path`**: Defines the base directory where the router files are located.
- Type: `String`
- Default: `'router'`

The `path` option typically uses `path.join` to resolve the absolute path to the directory where your route modules are stored.

| 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 = path.join(__dirname, 'router');
```

#### Example
### Router Configuration

```ini
path=router
recursive=true
pattern=*.js
cacheModules=false
logLevel=debug
```
### Loading Configuration
- **`router`**: Specifies the list of route files to be loaded by the framework.
- Type: `Array<String>`
- Default: `[]`

The `router` section lists the files that should be used as route modules. Each file is referenced with a specific index (e.g., `[0]`, `[1]`). The files are loaded in the order they appear.

**Example:**
```ini
router {
[0]: 'index.ts';
[1]: 'test.ts';
}
```

You can load the configuration file (`.confmodule`) in your main application file using the `load()` method.
### Cache

```typescript
import gland from '@medishn/gland';
import path from 'path';
- **`cache`**: Controls whether the router modules should be cached to improve performance.
- Type: `Boolean`
- Default: `true`

When `cache` is set to `true`, the route modules are loaded once and then reused from memory, reducing overhead. If set to `false`, the modules are reloaded every time they are needed.

const app = new gland();
**Example:**
```ini
cache = true;
```

// Load configuration
app.load(path.join(__dirname, '.confmodule'));
### Watch

// Initialize the server
app.init(3000, () => {
console.log('Server running on port 3000');
});
- **`watch`**: Enables or disables file watching. When enabled, the framework will automatically reload route modules if changes are detected in the files.
- Type: `Boolean`
- Default: `false`

When `watch` is set to `true`, the framework watches the route files for changes. If any file is modified, it will be reloaded automatically. This is useful during development but may not be necessary in production.

**Example:**
```ini
watch = false;
```

### Example

Here is a full example of a `.confmodule` file with all options:

```ini
path = path.join(__dirname, 'router');
router {
[0]: 'index.ts';
[1]: 'test.ts';
}
cache = true;
watch = false;
```

### Best Practices
## Best Practices

- **Separate configuration files by environment**: Create multiple configuration files for different environments (e.g., `.confmodule.dev` for development, `.confmodule.prod` for production) and load them based on the current environment.

- **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.
- **Use `cache` in production**: Caching the modules improves performance by reducing the need to reload them on each request. This is particularly useful for production environments.

- **Avoid hardcoding sensitive information** (e.g., database credentials) in your configuration files. Instead, use environment variables or a secrets management system.
- **Enable `watch` during development**: Automatically reloading route modules when files change can speed up the development process by reducing the need for manual restarts.

- **Use the `cacheModules` option in production** to improve performance by caching route and middleware modules.
- **Keep the configuration simple**: Only include essential options in your `.confmodule` file to maintain clarity and avoid complexity.
8 changes: 7 additions & 1 deletion examples/.confmodule
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
path=router
path = path.join(__dirname, 'router');
router {
[0]: 'index.ts';
[1]: 'test.ts'
}
cache=true;
watch= false;
2 changes: 1 addition & 1 deletion examples/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import gland from '../dist';
const g = new gland();
g.load({ path: path.join(__dirname, 'router') });
g.load(path.join(__dirname, '.confmodule'));
g.init(3000, () => {
console.log('server run on port 3000');
});
10 changes: 10 additions & 0 deletions examples/router/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Context, Get, Route } from '../../dist';

@Route('/test')
export class Test {
@Get()
test(ctx: Context) {
ctx.write('hello world2222');
ctx.end();
}
}
2 changes: 1 addition & 1 deletion lib/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class WebServer extends Server implements Gland.APP {
init(...args: ListenArgs): this {
return this.listen(...args);
}
async load(conf: Partial<ModuleConfig> | string) {
async load(conf: string) {
await LoadModules.load(conf);
}
}
Loading

0 comments on commit 9c372a0

Please sign in to comment.