adding dns service
This commit is contained in:
parent
b48f806da4
commit
be5117feca
6 changed files with 56 additions and 6 deletions
|
@ -1,9 +1,11 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
import { UrlShortenerModule } from './url-shortener/url-shortener.module';
|
import { UrlShortenerModule } from './url-shortener/url-shortener.module';
|
||||||
|
import { DnsService } from './dns/dns.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [UrlShortenerModule],
|
imports: [UrlShortenerModule],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
|
providers: [DnsService],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|
18
src/dns/dns.service.spec.ts
Normal file
18
src/dns/dns.service.spec.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { DnsService } from './dns.service';
|
||||||
|
|
||||||
|
describe('DnsService', () => {
|
||||||
|
let service: DnsService;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [DnsService],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
service = module.get<DnsService>(DnsService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(service).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
20
src/dns/dns.service.ts
Normal file
20
src/dns/dns.service.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import * as dns from 'dns';
|
||||||
|
import * as util from 'util';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DnsService {
|
||||||
|
private lookup = util.promisify(dns.lookup);
|
||||||
|
|
||||||
|
async isValidUrl(url: string): Promise<boolean> {
|
||||||
|
console.log('HERE!!!')
|
||||||
|
try {
|
||||||
|
const hostname = new URL(url).hostname;
|
||||||
|
await this.lookup(hostname);
|
||||||
|
console.log('HERE!')
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,12 +6,12 @@ export class UrlShortenerController {
|
||||||
constructor(private readonly urlShortenerService: UrlShortenerService) {}
|
constructor(private readonly urlShortenerService: UrlShortenerService) {}
|
||||||
|
|
||||||
@Post('shorten')
|
@Post('shorten')
|
||||||
shortenUrl(@Body('url') url: string): { shortUrl: string } {
|
async shortenUrl(@Body('url') url: string): Promise<{ shortUrl: string }> {
|
||||||
// example route http://localhost:3000/url-shortener/shorten POST body { url: 'https://www.google.com' }
|
// example route http://localhost:3000/url-shortener/shorten POST body { url: 'https://www.google.com' }
|
||||||
if (!url) {
|
if (!url) {
|
||||||
throw new HttpException('URL is required', HttpStatus.BAD_REQUEST);
|
throw new HttpException('URL is required', HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
const shortUrl = this.urlShortenerService.generateShortUrl(url);
|
const shortUrl = await this.urlShortenerService.generateShortUrl(url);
|
||||||
return { shortUrl };
|
return { shortUrl };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { UrlShortenerService } from './url-shortener.service';
|
import { UrlShortenerService } from './url-shortener.service';
|
||||||
import { UrlShortenerController } from './url-shortener.controller';
|
import { UrlShortenerController } from './url-shortener.controller';
|
||||||
|
import { DnsService } from '../dns/dns.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [UrlShortenerService],
|
providers: [UrlShortenerService, DnsService],
|
||||||
controllers: [UrlShortenerController]
|
controllers: [UrlShortenerController],
|
||||||
})
|
})
|
||||||
export class UrlShortenerModule {}
|
export class UrlShortenerModule {}
|
||||||
|
|
|
@ -1,14 +1,23 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import * as shortid from 'shortid';
|
import * as shortid from 'shortid';
|
||||||
|
import { DnsService } from '../dns/dns.service';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UrlShortenerService {
|
export class UrlShortenerService {
|
||||||
private urlMap = new Map<string, string>();
|
|
||||||
|
|
||||||
generateShortUrl(originalUrl: string): string {
|
private urlMap = new Map<string, string>();
|
||||||
|
|
||||||
|
constructor(private readonly dnsService: DnsService) {}
|
||||||
|
|
||||||
|
async generateShortUrl(originalUrl: string): Promise<string> {
|
||||||
if(!originalUrl) {
|
if(!originalUrl) {
|
||||||
throw new Error('URL is required');
|
throw new Error('URL is required');
|
||||||
}
|
}
|
||||||
|
let isValid = await this.dnsService.isValidUrl(originalUrl);
|
||||||
|
if(!isValid) {
|
||||||
|
throw new Error('URL is invalid');
|
||||||
|
}
|
||||||
let shortUrl = shortid.generate();
|
let shortUrl = shortid.generate();
|
||||||
this.urlMap.set(shortUrl, originalUrl);
|
this.urlMap.set(shortUrl, originalUrl);
|
||||||
return shortUrl
|
return shortUrl
|
||||||
|
|
Loading…
Reference in a new issue