-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
project start: create server http with endpoint main
- create structure of folders - add contrllers, services, and routes
- Loading branch information
0 parents
commit 760917e
Showing
10 changed files
with
594 additions
and
0 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,8 @@ | ||
{ | ||
"recommendations": [ | ||
"denoland.vscode-deno", | ||
"rangav.vscode-thunder-client", | ||
"dzannotti.vscode-babel-coloring", | ||
"xabikos.javascriptsnippets" | ||
] | ||
} |
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 @@ | ||
{ | ||
"deno.enable": true, | ||
"editor.formatOnSave": true, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "denoland.vscode-deno" | ||
}, | ||
"deno.config": "./deno.jsonc", | ||
"discord.enabled": true | ||
} |
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,19 @@ | ||
{ | ||
"tasks": { | ||
"dev": "deno run --watch --allow-net src/main.ts" | ||
}, | ||
"fmt": { | ||
"files": { | ||
"include": [ | ||
"src/" | ||
] | ||
}, | ||
"options": { | ||
"useTabs": true, | ||
"lineWidth": 120, | ||
"indentWidth": 2, | ||
"singleQuote": true, | ||
"proseWrap": "preserve" | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
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,65 @@ | ||
import { Context } from 'https://deno.land/x/[email protected]/mod.ts'; | ||
import { ScraperService } from '../services/Scraper.service.ts'; | ||
|
||
export async function getJobs(ctx: Context): Promise<void> { | ||
const scraper = new ScraperService('https://www.getonbrd.com'); | ||
const $ = await scraper.execute('/empleos/programacion'); | ||
|
||
const jobsResultList = $( | ||
'body #right-col .main-container ul.sgb-results-list>div', | ||
); | ||
|
||
const jobs = $(jobsResultList).map((_i, el) => { | ||
let elCheerio = $(el); | ||
elCheerio = elCheerio.children('a'); | ||
|
||
const elInfo = elCheerio | ||
.children('.gb-results-list__main') | ||
.children('.gb-results-list__info'); | ||
|
||
// const logo = elCheerio | ||
// .children('.gb-results-list__main') | ||
// .children('.gb-results-list__avatar') | ||
// .children('img.gb-results-list__img') | ||
// .attr('src'); | ||
|
||
const title: string = elInfo | ||
.children('.gb-results-list__title') | ||
.children('strong') | ||
.text(); | ||
|
||
const [role, time]: string[] = elInfo | ||
.children('.gb-results-list__title') | ||
.children('span') | ||
.text() | ||
.split('|') | ||
.map((t) => t.trim()); | ||
|
||
const textInfo: string = elInfo | ||
.children('div') | ||
.text(); | ||
|
||
const [companyName, ...locations]: string[] = textInfo | ||
.split('\n') | ||
.filter((t) => t); | ||
|
||
const location = locations | ||
.filter((_t, i) => i > 0) | ||
.join(' '); | ||
|
||
const url: string = elCheerio.attr('href') || ''; | ||
|
||
return { | ||
title, | ||
role, | ||
time, | ||
companyName, | ||
location, | ||
url, | ||
}; | ||
}).toArray(); | ||
|
||
ctx.response.body = { | ||
jobs, | ||
}; | ||
} |
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 { Server } from './services/Server.service.ts'; | ||
|
||
try { | ||
const server = new Server(); | ||
|
||
server.run(); | ||
} catch (error) { | ||
console.log((error as Error).message); | ||
} |
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,8 @@ | ||
import { Router } from 'https://deno.land/x/[email protected]/mod.ts'; | ||
import { getJobs } from '../controllers/jobs.controller.ts'; | ||
|
||
const router = new Router(); | ||
|
||
router.get('/jobs', getJobs); | ||
|
||
export const routes = router.routes(); |
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,17 @@ | ||
import { CheerioAPI, load } from 'https://esm.sh/[email protected]'; | ||
|
||
export class ScraperService { | ||
private baseUrl: string; | ||
|
||
constructor(baseUrl: string) { | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
public async execute(path: string): Promise<CheerioAPI> { | ||
const pathname: string = path.startsWith('/') ? path : `/${path}`; | ||
const response = await fetch(this.baseUrl + pathname); | ||
const html: string = await response.text(); | ||
const $ = load(html); | ||
return $; | ||
} | ||
} |
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,32 @@ | ||
import { Application } from 'https://deno.land/x/[email protected]/mod.ts'; | ||
import { routes } from '../routes/index.ts'; | ||
|
||
export class Server { | ||
private readonly app = new Application(); | ||
|
||
constructor() { | ||
this.middlewares(); | ||
this.routes(); | ||
} | ||
|
||
private middlewares() { | ||
// Asegurar que todas las peticiones empiecen por "/api" | ||
this.app.use(async (ctx, next) => { | ||
const path: string[] = ctx.request.url.pathname.split('/'); | ||
if (path[1] === 'api') { | ||
ctx.request.url.pathname = `${path[0]}/${path[2]}`; | ||
await next(); | ||
} | ||
}); | ||
} | ||
|
||
private routes(): void { | ||
this.app.use(routes); | ||
} | ||
|
||
public run(): void { | ||
const port = 8000; | ||
this.app.listen({ hostname: '0.0.0.0', port }); | ||
console.log(`Sever listening on port ${port}`); | ||
} | ||
} |