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

feat: add request ip to location #161

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ RUN npm install
# Copy the rest of the application code
COPY . .

# Convert setup.sh to Unix-style line endings (LF)
RUN sed -i 's/\r$//' ./setup.sh


# Run any additional setup script
RUN chmod +x ./setup.sh
RUN ./setup.sh

# Set environment variable
ENV NODE_ENV production

# Expose the application port
EXPOSE 3000

# Start the application
CMD ["npm", "run", "start:dev"]
50 changes: 50 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@
"fuse.js": "^7.0.0",
"multer": "^1.4.5-lts.1",
"reflect-metadata": "^0.1.13",
"request-ip": "^3.3.0",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@nestjs/testing": "^10.0.0",
"@samagra-x/schematics": "0.0.5",
"@samagra-x/stencil-cli": "0.0.5",
"@types/express": "^4.17.17",
"@types/fastify-multipart": "^0.7.0",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/request-ip": "^0.0.41",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AppService } from './app.service';
GeorevModule,
LocationModule,
ConfigModule.forRoot({
envFilePath: `${process.cwd()}/config/env/${process.env.NODE_ENV || 'default'}.env`,
envFilePath: `.env`,
load: [config],
isGlobal: true,
}),
Expand Down
2 changes: 2 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const config = () => ({
NODE_ENV: process.env.NODE_ENV || 'default',
port: parseInt(process.env.PORT) || 3000,
host: parseInt(process.env.HOST) || '0.0.0.0',
method: parseInt(process.env.METHOD) || 'http',
requiredGeoLocationLevels: ['SUBDISTRICT', 'DISTRICT', 'STATE'],
geoLocationLevels: {
VILLAGE: 'VILLAGE',
Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ async function bootstrap() {

const configService = app.get(ConfigService);
const port = configService.get<number>('port');
const host = configService.get<string>('host');
const method = configService.get<string>('method');

// Register plugins and middleware
await app.register(multipart);
Expand All @@ -43,8 +45,8 @@ async function bootstrap() {
SwaggerModule.setup('api-docs', app, document);

// Start the server
await app.listen(port, () => {
logger.log(`Server running on http://localhost:${port}`);
await app.listen(port, host, (err, address) => {
logger.log(`Server running on ${method}://${host}:${port}`);
});

// Log additional information as needed
Expand Down
14 changes: 13 additions & 1 deletion src/modules/city/city.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@ import {
Logger,
Param,
Post,
HttpCode,
HttpCode, Req, Ip,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import { CityService } from './city.service';
import { formatSuccessResponse } from '../../utils/serializer/success';
import { formatErrorResponse } from '../../utils/serializer/error';
import * as RequestIp from 'request-ip'

@ApiTags('/city')
@Controller('city')
export class CityController {
constructor(private readonly cityService: CityService) {}
private readonly logger = new Logger(CityController.name);

@Get('self')
getGeoFromRequestIp(@Req() req) {
const ip = RequestIp.getClientIp(req)
try {
const city = this.cityService.getCity(ip);
return formatSuccessResponse(city);
} catch (error) {
return formatErrorResponse(error, ip);
}
}

@Get(':ip')
getCity(@Param('ip') ip: string) {
try {
Expand Down
Loading