adding dns service

This commit is contained in:
Kfir Dayan 2024-01-21 11:19:52 +02:00
parent b48f806da4
commit be5117feca
6 changed files with 56 additions and 6 deletions

View file

@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { UrlShortenerModule } from './url-shortener/url-shortener.module';
import { DnsService } from './dns/dns.service';
@Module({
imports: [UrlShortenerModule],
controllers: [AppController],
providers: [DnsService],
})
export class AppModule {}

View 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
View 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;
}
}
}

View file

@ -6,12 +6,12 @@ export class UrlShortenerController {
constructor(private readonly urlShortenerService: UrlShortenerService) {}
@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' }
if (!url) {
throw new HttpException('URL is required', HttpStatus.BAD_REQUEST);
}
const shortUrl = this.urlShortenerService.generateShortUrl(url);
const shortUrl = await this.urlShortenerService.generateShortUrl(url);
return { shortUrl };
}

View file

@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { UrlShortenerService } from './url-shortener.service';
import { UrlShortenerController } from './url-shortener.controller';
import { DnsService } from '../dns/dns.service';
@Module({
providers: [UrlShortenerService],
controllers: [UrlShortenerController]
providers: [UrlShortenerService, DnsService],
controllers: [UrlShortenerController],
})
export class UrlShortenerModule {}

View file

@ -1,14 +1,23 @@
import { Injectable } from '@nestjs/common';
import * as shortid from 'shortid';
import { DnsService } from '../dns/dns.service';
@Injectable()
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) {
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();
this.urlMap.set(shortUrl, originalUrl);
return shortUrl