-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from medishen/dev/v2
feat(example): A simple example was added which includes AppConfig as…
- Loading branch information
Showing
30 changed files
with
388 additions
and
115 deletions.
There are no files selected for viewing
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 @@ | ||
ENV="development" |
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,2 @@ | ||
node_modules | ||
.env |
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,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. |
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,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" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 {} |
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,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}`); | ||
}); |
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,6 @@ | ||
import { AppConfig } from '../../../../dist'; | ||
|
||
export default { | ||
app_name: 'BasicApp', | ||
environment: process.env.ENV, | ||
} as AppConfig; |
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,9 @@ | ||
import { Controller, Get, ServerRequest } from '../../../../dist'; | ||
|
||
@Controller('/') | ||
export class HomeController { | ||
@Get('/') | ||
index(ctx: ServerRequest): void { | ||
ctx.send('Welcome to the Gland Framework!'); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,9 @@ | ||
import { Logger } from '@medishn/logger'; | ||
export class AppLogger { | ||
logger: Logger; | ||
constructor() { | ||
this.logger = new Logger(); | ||
} | ||
} | ||
|
||
export const appLogger = new AppLogger().logger; |
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,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"] | ||
} |
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
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
Oops, something went wrong.