Skip to content

Commit

Permalink
Merge pull request #16 from medishen/dev/v2
Browse files Browse the repository at this point in the history
feat(example): A simple example was added which includes AppConfig as…
  • Loading branch information
0xii00 authored Jan 17, 2025
2 parents d8cfaec + 0326fa0 commit 4f46df9
Show file tree
Hide file tree
Showing 30 changed files with 388 additions and 115 deletions.
Empty file added example/README.md
Empty file.
1 change: 1 addition & 0 deletions example/basic/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENV="development"
2 changes: 2 additions & 0 deletions example/basic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
43 changes: 43 additions & 0 deletions example/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Basic Application Example for Gland Framework

This is a simple example application demonstrating how to build and structure applications using the **Gland** framework. Gland is a lightweight, extensible, and modular Node.js framework designed to help you build fast and scalable web servers with clean, maintainable code.

This example covers the following core features:

- Controllers and decorators for routing
- Services for business logic separation
- Application configuration

### Core Features of the Example App

1. **Controllers and Routing**:

- `home.controller.ts`: This file demonstrates how to create routes and handle HTTP requests using the `@Controller` decorator.
- `user.controller.ts`: Another example of a controller that handles user-related routes.

2. **Services**:

- `user.service.ts`: A service for handling business logic, such as user operations.

3. **Configuration**:

- `app.config.ts`: Configuration file to handle application-wide settings, such as environment variables and server settings.

### Getting Started

To start using this example app, follow these steps:

1. Clone this repository.
2. Run the following commands to install dependencies:

```bash
npm install
```

3. Start the server:

```bash
npm run dev
```

4. The application should now be running on `http://localhost:3000`. You can access different routes (like `/home` and `/user`) as defined in the controllers.
16 changes: 16 additions & 0 deletions example/basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "gland-basic-app",
"version": "1.0.0",
"description": "Basic application example using the Gland framework",
"main": "src/app.ts",
"scripts": {
"dev": "NODE_ENV=development node --trace-uncaught -r ts-node/register --env-file=.env src/app.ts"
},
"dependencies": {
"@medishn/logger": "^2.0.1",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
},
"author": "Mahdi",
"license": "MIT"
}
173 changes: 173 additions & 0 deletions example/basic/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions example/basic/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from '../../../dist';
import { HomeController } from './controllers/home.controller';
import { UserController } from './controllers/user.controller';
import { UserService } from './services/user.service';

@Module({
controllers: [HomeController, UserController],
providers: [
{
provide: UserService,
useClass: UserService,
scope: 'singleton',
},
],
})
export class AppModule {}
10 changes: 10 additions & 0 deletions example/basic/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Application } from '../../../dist';
import { AppModule } from './app.module';
import appConfig from './config/app.config';
import { appLogger } from './utils/logger';

const app = Application.create(AppModule, appConfig);

app.listen(3000, 'localhost', (port, hostname) => {
appLogger.info(`Server running on http://${hostname}:${port}`);
});
6 changes: 6 additions & 0 deletions example/basic/src/config/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AppConfig } from '../../../../dist';

export default {
app_name: 'BasicApp',
environment: process.env.ENV,
} as AppConfig;
9 changes: 9 additions & 0 deletions example/basic/src/controllers/home.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller, Get, ServerRequest } from '../../../../dist';

@Controller('/')
export class HomeController {
@Get('/')
index(ctx: ServerRequest): void {
ctx.send('Welcome to the Gland Framework!');
}
}
29 changes: 29 additions & 0 deletions example/basic/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Controller, Get, Inject, Post, ServerRequest } from '../../../../dist';
import { UserService } from '../services/user.service';

@Controller('/user')
export class UserController {
constructor(@Inject(UserService) private readonly userService: UserService) {}

@Get('/')
async getAllUsers(ctx: ServerRequest): Promise<void> {
const users = await this.userService.getAllUsers();
console.log('users:', users);
ctx.send(users);
}

@Get('/:id')
async getUserById(ctx: ServerRequest): Promise<void> {
const id = ctx.params.id;
const user = await this.userService.getUserById(id);
ctx.send(user);
}

@Post('/')
async createUser(ctx: ServerRequest): Promise<void> {
const body = ctx.body;
const newUser = await this.userService.createUser(body);
ctx.status = 201;
ctx.send(newUser);
}
}
23 changes: 23 additions & 0 deletions example/basic/src/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Injectable } from '../../../../dist';

@Injectable()
export class UserService {
private users: Record<string, any>[] = [
{ id: '1', name: 'John Doe', email: 'john@example.com' },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com' },
];

getAllUsers(): Promise<Record<string, any>[]> {
return Promise.resolve(this.users);
}

getUserById(id: string): Promise<Record<string, any> | null> {
return Promise.resolve(this.users.find((user) => user.id === id) || null);
}

createUser(user: Record<string, any>): Promise<Record<string, any>> {
const newUser = { id: String(this.users.length + 1), ...user };
this.users.push(newUser);
return Promise.resolve(newUser);
}
}
9 changes: 9 additions & 0 deletions example/basic/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Logger } from '@medishn/logger';
export class AppLogger {
logger: Logger;
constructor() {
this.logger = new Logger();
}
}

export const appLogger = new AppLogger().logger;
20 changes: 20 additions & 0 deletions example/basic/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"moduleResolution": "node",
"outDir": "dist",
"rootDir": "src",
"types": ["node"],
"resolveJsonModule": true,
"noImplicitAny": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}
3 changes: 1 addition & 2 deletions lib/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Environment } from './enums';
import { IDManager } from './IDManager';
import { AppConfig } from './interfaces';
import { RouterUtils } from '../utils';
export const defaultConfig: AppConfig = {
app_name: 'GlandMyApp',
app_version: '1.0.0',
environment: Environment.DEVELOPMENT,
environment: 'DEVELOPMENT',
server_id: IDManager.generateServerId(),

paths: {
Expand Down
2 changes: 1 addition & 1 deletion lib/common/interfaces/app-settings.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BodyParserOptions } from './app.interface';
export interface AppConfig {
[KEY_SETTINGS.APP_NAME]?: string;
[KEY_SETTINGS.APP_VERSION]?: string;
[KEY_SETTINGS.ENVIRONMENT]?: Environment;
[KEY_SETTINGS.ENVIRONMENT]?: keyof typeof Environment;
[KEY_SETTINGS.SERVER_ID]?: string;
[KEY_SETTINGS.PATHS]?: PathConfig;
[KEY_SETTINGS.CACHE]?: CacheConfigQiks<string>;
Expand Down
Loading

0 comments on commit 4f46df9

Please sign in to comment.