Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: GeoIP Interceptor + Test Cases #137

Open
wants to merge 92 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
e8d0359
fix: file upload sample
techsavvyash May 21, 2024
a431b51
(skip-ci) docs: update README
techsavvyash May 21, 2024
0e018d7
Create codeql.yml
Himasnhu-AT May 24, 2024
93806ed
implement geoip blocking
Himasnhu-AT May 26, 2024
680bcce
updated minor changes
RiyaRaj28 Jun 9, 2024
e268f1f
added config service for geoIp url
RiyaRaj28 Jun 11, 2024
92e65be
Delete sample/07-geopip-blocking/src/app.controller.spec.ts
RiyaRaj28 Jun 21, 2024
7b2a280
Delete sample/07-geopip-blocking/test/app.e2e-spec.ts
RiyaRaj28 Jun 21, 2024
4382416
fix: Consists of Suggested changes and even added test cases
DarrenDsouza7273 Jun 23, 2024
cbf686e
fix: Update geoip.interceptor.ts file
DarrenDsouza7273 Jun 24, 2024
0c64956
fix: Removed unnecessary files
DarrenDsouza7273 Jun 24, 2024
43afc36
fix: added e2e test cases
DarrenDsouza7273 Jun 24, 2024
30e4c52
added capp.controller.spec.ts
DarrenDsouza7273 Jun 25, 2024
2a2ea0e
Adds a dev setup and dev workflow guide to CONTRIBUTIING.md
gnaaruag Jul 8, 2024
6b1fc7f
Merge pull request #163 from gnaaruag/contribution-guide-add
techsavvyash Jul 9, 2024
d6a56e0
feat: tweaks + spec file
techsavvyash Jul 9, 2024
82507d1
added dto files
RiyaRaj28 Jul 2, 2024
ac039b3
updated in dev
RiyaRaj28 Jul 5, 2024
f7138d9
Fix : Readme temporal package setup (#116)
ShivamAher30 Jun 4, 2024
5c44725
Updated package.json (#119)
RiyaRaj28 Jun 6, 2024
e20c146
Fix-NPM scripts changed from nest to stencil [05-temporal-package] (#…
ShivamAher30 Jun 6, 2024
e9cae67
Update file-upload.service.ts (#115)
SurAyush Jun 6, 2024
9164759
Update README.md (#112)
SurAyush Jun 6, 2024
be4b892
Implemented Tests for different packages under common folder (#107)
SHARJIDH Jun 6, 2024
2f183ab
chore: add pull request template
techsavvyash Jun 6, 2024
e56c57b
updates cli and monitoring docs (#123)
RiyaRaj28 Jun 9, 2024
1e5399a
Created CONTRIBUTING.md (#133)
RiyaRaj28 Jun 19, 2024
41e3624
Created test.yml
RiyaRaj28 Jul 5, 2024
3a56e81
Update app.service.ts
RiyaRaj28 Jul 5, 2024
05836cd
chore: fix nits
techsavvyash Jul 6, 2024
8fa2fa4
fix: updated format
Savio629 Jun 28, 2024
fa639c8
fix: added test for response-format
Savio629 Jun 30, 2024
2334184
fix: made required changes
Savio629 Jul 2, 2024
5513899
fix: changed variable name
Savio629 Jul 2, 2024
d5a7b03
fix: failing tests and logic
techsavvyash Jul 6, 2024
06359ee
chore: cleanup'
techsavvyash Jul 6, 2024
feae927
chore: restructure
techsavvyash Jul 6, 2024
7e6b6e7
fix: file upload sample
techsavvyash May 21, 2024
de3c19e
fix: minio not picking up SSL var
techsavvyash May 29, 2024
63e5159
fix: minio
Jun 27, 2024
cfd74e0
Implemented Tests for different packages under common folder (#107)
SHARJIDH Jun 6, 2024
3923316
fix:updated all env vars
Jun 29, 2024
b0019d9
fix:added tests for download endpoint
Jun 29, 2024
a3a8bff
fix: added unit tests
Jul 5, 2024
ed7cf9d
fix: credentials changed
Jul 5, 2024
83e1225
Added Multiple FIles Upload Feature
SurAyush Jun 9, 2024
750f591
docs: merge #129
techsavvyash Jun 19, 2024
09c5f73
fix: resolve review comments
techsavvyash Jun 19, 2024
27fe777
fix: Updated File Upload
DarrenDsouza7273 Jun 26, 2024
e8d949a
fix: Updated Readme
DarrenDsouza7273 Jun 27, 2024
d0d2e94
Update README.md
DarrenDsouza7273 Jun 27, 2024
5e4d41f
fix: fixed conflicts
Savio629 Jul 6, 2024
fd01e55
fix: extra conflicts
Savio629 Jul 6, 2024
6eb56f5
fix: removed console.log
Savio629 Jul 6, 2024
c20fe3c
fix: enabled upload-file to have stability
Savio629 Jul 7, 2024
b1ee3e1
branch updated
Savio629 Jul 7, 2024
1d76839
fix: updated the branch
Savio629 Jul 7, 2024
472bf8b
fix: file upload service
techsavvyash Jul 9, 2024
b4d6d98
feat: tweaks + spec file
techsavvyash Jul 9, 2024
bc44c4c
Implemented Tests for different packages under common folder (#107)
SHARJIDH Jun 6, 2024
167e827
fix: added test for response-format
Savio629 Jun 30, 2024
d82b364
fix: failing tests and logic
techsavvyash Jul 6, 2024
2ec9d16
fix: minio not picking up SSL var
techsavvyash May 29, 2024
7852883
Implemented Tests for different packages under common folder (#107)
SHARJIDH Jun 6, 2024
c1ce0be
fix:updated all env vars
Jun 29, 2024
dc3da02
fix: added unit tests
Jul 5, 2024
e30e474
Added Multiple FIles Upload Feature
SurAyush Jun 9, 2024
db622dc
fix: extra conflicts
Savio629 Jul 6, 2024
1e6b2ea
fix: file upload service
techsavvyash Jul 9, 2024
7ba71a9
fix: update filename regex to allow spaces
techsavvyash Jul 9, 2024
af92616
feat: tweaks + spec file
techsavvyash Jul 9, 2024
81a0f4d
Implemented Tests for different packages under common folder (#107)
SHARJIDH Jun 6, 2024
c6127b0
fix: added test for response-format
Savio629 Jun 30, 2024
595e527
fix: minio not picking up SSL var
techsavvyash May 29, 2024
ab6e3a3
Implemented Tests for different packages under common folder (#107)
SHARJIDH Jun 6, 2024
3e87551
fix:updated all env vars
Jun 29, 2024
2c6c9f6
fix: added unit tests
Jul 5, 2024
00096d5
Added Multiple FIles Upload Feature
SurAyush Jun 9, 2024
763fea0
fix: file upload service
techsavvyash Jul 9, 2024
d456dc5
feat: tweaks + spec file
techsavvyash Jul 9, 2024
d09cf23
Merge branch 'dev' into Geoip-updated
Savio629 Jul 11, 2024
49bd406
chore: add commitlint config
techsavvyash Jul 16, 2024
034f683
fix: Deleted src/interceptors/test/response-time.interceptor.spec.ts
Savio629 Jul 18, 2024
cd1b608
Merge branch 'SamagraX-Stencil:main' into Geoip-updated
Savio629 Aug 10, 2024
37430b4
fix: fixed failing ci
Savio629 Aug 10, 2024
a5a72cf
fix: updated the test and some fixes
Savio629 Aug 17, 2024
5deae9f
feat: support for coord, geofence,cities
Savio629 Aug 21, 2024
935db66
fix: fixed failing yarn test
Savio629 Aug 21, 2024
e31ebe9
fix: updated the test
Savio629 Aug 21, 2024
a1b04ae
fix: updated the test1
Savio629 Aug 21, 2024
3fc07e3
fix: removed console.log
Savio629 Aug 21, 2024
8fd357c
fix: updated e2e tests
Savio629 Aug 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: "CodeQL"

on:
push:
branches: [ "main", "master" ]
pull_request:
branches: [ "main", "master" ]

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Find languages
id: findLanguages
run: |
countTS=$(find ./ -type f -name '*.ts' | wc -l)
countJS=$(find ./ -type f -name '*.js' | wc -l)
countPY=$(find ./ -type f -name '*.py' | wc -l)
countCPP=$(find ./ -type f -name '*.cpp' | wc -l)
countJAVA=$(find ./ -type f -name '*.java' | wc -l)
languages=""
if [ $countTS -gt 0 ]; then
languages+="typescript,"
fi
if [ $countJS -gt 0 ]; then
languages+="javascript,"
fi
if [ $countPY -gt 0 ]; then
languages+="python,"
fi
if [ $countCPP -gt 0 ]; then
languages+="cpp,"
fi
if [ $countJAVA -gt 0 ]; then
languages+="java,"
fi
# Remove the trailing comma
languages=${languages%,}
echo "languages=$languages" >> $GITHUB_ENV

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ env.languages }}

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
49 changes: 27 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
# Stencil

Stencil is an opinionated backend framework based on [NestJS](https://nestjs.com) with a lot of required tooling setup right out of the box or possible to setup with just one CLI command so that you can dive directly into writing business logic.
Stencil is an opinionated backend framework based on [NestJS](https://nestjs.com), built keeping Developer Experience and Developer Productivity in mind.
Stencil aims to reduce the development setup times so that the developers writing backend services can spend more time writing business logic unique to the service rather than setting up common tooling which is shared across projects.

## Supported Tooling

- [Prisma ORM](https://prisma.io)
- Don't deal with the hassle of setting up a prisma instance, start writing schemas directly.
- [User-Service](https://github.com/Samagra-Development/user-service)
- User Management is just as simple as calling a decorator over your API Controller.
- [Monitoring](https://github.com/ChakshuGautam/nestjs-monitor)
- Get Prometheus and Grafana dashboards setup right out of the box with beautifully a programatically generated dashboard monitoring your API response times.
- [Temporal](https://temporal.io)
- Focus on implementing the activity and workflow we have got the setup covered.
- [Response formatting]
- Enfore response format standards throughout the app with a single interceptor
- [File Uploads]
- Require handling file uploads and downloads via your service? Get a service setup right out of the box.

**If you have request for a specific tool to be setup automatically, please open a issue ticket and we'll try to get it added at the earliest.**

## Getting started

### Installation
### Usage

1. Install the stencil-cli
1. Install the stencil-cli globally on your system
```bash
npm i -g @samagra-x/stencil-cli
```

2. To scaffold a new project run this:
2. Bootstrap a new project using:
```bash
stencil new <PROJECT_NAME>
```
During the setup the CLI help you setup all the toolings you want by letting you pick and choose between what you want to have in your service.
During the setup the CLI will prompt you to select all the toolings you want by letting you pick and choose between what you want to have in your service.

### Demo

A demo video depicting how to use stencil-cli can be found [here](https://drive.google.com/file/d/1RaafplnJMlfKgYB-WwyfINREjqoRQSLb/view?usp=sharing)

## Local Setup

1. Clone the repository locally
```bash
git clone https://github.com/SamagraX-Stencil/stencil
Expand All @@ -49,4 +35,23 @@ git clone https://github.com/SamagraX-Stencil/stencil
yarn
```

3. Navigate to the required directory where you want to hack around and refer directory level READMEs to understand more about the code stored there.
3. Navigate to the required directory where you want to hack around and refer directory level READMEs to understand more about the code stored there.

## Documentation

You can refer to the technical documentation which lists all the available functionality [here](https://stencil-docs.vercel.app/)

**If you have request for a specific tool to be setup automatically, please open a issue ticket and we'll try to get it added at the earliest.**

## Acknowledgements

- [NestJS](https://nestjs.com/)
- [Prisma ORM](https://prisma.io/)
- [Postgres](https://www.postgresql.org/)
- [FusionAuth](https://fusionauth.io/)
- [Prometheus](https://prometheus.io/)
- [Grafana](https://grafana.com)
- [Elastic Search](https://www.elastic.co/)
- [Logstash](https://www.elastic.co/logstash)
- [Kibana](https://www.elastic.co/kibana)
- [Temporal](https://temporal.io)
88 changes: 88 additions & 0 deletions packages/common/src/interceptors/geoip.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
Logger,
HttpStatus,
InternalServerErrorException,
HttpException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Observable } from 'rxjs';
import { HttpService } from '@nestjs/axios';

@Injectable()
export class GeoIPInterceptor implements NestInterceptor {
private readonly httpService: HttpService;
private readonly allowedCountries: string[];
private readonly configService: ConfigService;
private readonly logger : Logger;
private readonly accessDeniedMessage: string;
private readonly accessDeniedStatus: number;

constructor(allowedCountries: string[], accessDeniedStatus: number = HttpStatus.FORBIDDEN) {
this.logger = new Logger('GeoIPInterceptor');
this.httpService = new HttpService();
this.allowedCountries = allowedCountries;
this.configService = new ConfigService();
this.accessDeniedMessage = 'Access Denied';
this.accessDeniedStatus = accessDeniedStatus;
}

async intercept(
context: ExecutionContext,
next: CallHandler,
): Promise<Observable<any>> {
const request = context.switchToHttp().getRequest();

// Log all headers to see what is being received
this.logger.log('Request Headers:', request.headers);

// Extract IP address
const clientIp = request.headers['request.ip'] || request.headers['x-forwarded-for'] || request.ip;
this.logger.log('Using IP address for geolocation:', clientIp);

try {
// Call the geolocation service to get the country from the IP
const { country, regionName } = await this.getLocation(clientIp);

if (
this.allowedCountries.length > 0 &&
!this.allowedCountries.includes(country)
) {
this.logger.error(
'Denying request from IP: ' + clientIp + ' country: ' + country,
);
throw new HttpException(this.accessDeniedMessage, this.accessDeniedStatus);
}

this.logger.log(
'Allowed request from IP: ' + clientIp + ' region: ' + regionName,
);
} catch (err) {
this.logger.error('Error occurred while reading the geoip database', err);
throw new InternalServerErrorException(
'Error occurred while reading the geoip database',
);
}

// Continue handling the request
return next.handle();
}

private async getLocation(ip: string): Promise<any> {
try {
const geoIp = this.configService.get<string>('GEO_IP');
const resp = await this.httpService.axiosRef.get(
`http://geoip.samagra.io/city/${ip}`
);
return { country: resp.data.country, regionName: resp.data.regionName };
} catch (err) {
this.logger.error('Error occurred while reading the geoip database', err);
throw new InternalServerErrorException(
'Error occurred while reading the geoip database',
);
}
}
}
1 change: 1 addition & 0 deletions packages/common/src/interceptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './response-time.interceptor';
export * from './response-format.interceptor';
export * from './file-upload.interceptor';
export * from './utils';
export * from './geoip.interceptor';
2 changes: 1 addition & 1 deletion sample/06-file-upload/nest-cli.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@samagra-x/schematics",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
Expand Down
7 changes: 4 additions & 3 deletions sample/06-file-upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-fastify": "^10.3.0",
"@samagra-x/stencil": "^0.0.6",
"@types/multer": "^1.4.11",
"fastify": "^4.25.2",
"fastify-multer": "^2.0.3",
Expand All @@ -33,9 +34,9 @@
"rxjs": "^7.8.1"
},
"devDependencies": {
"@samagra-x/stencil-cli": "^0.0.5",
"@samagra-x/schematics": "^0.0.5",
"@nestjs/testing": "^10.0.0",
"@samagra-x/schematics": "^0.0.5",
"@samagra-x/stencil-cli": "^0.0.5",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
Expand All @@ -45,9 +46,9 @@
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"husky": "^9.0.11",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"husky": "^9.0.11",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
Expand Down
Loading
Loading