-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
79 lines (66 loc) · 2.34 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Hono } from 'hono'
import { logger } from 'hono/logger'
import { cors } from 'hono/cors'
import { Repo, RawJob } from './repo'
import nunjucks from 'nunjucks'
const app = new Hono()
const repo = new Repo()
app.use(logger())
app.use('*', cors())
nunjucks.configure('templates', { autoescape: true })
app.get('/', (c) => {
const newJobs = repo.getNewJobs()
const jobsCount = repo.countJobs()
return c.html(nunjucks.render('jobs.html',
{ jobs: newJobs, jobType: 'new', jobsCount: jobsCount, page: 1, nextPage: 2, prevPage: 1 }))
})
app.get('/new/:page', (c) => {
const page = Number(c.req.param('page'))
const newJobs = repo.getNewJobs(page)
const jobsCount = repo.countJobs()
return c.html(nunjucks.render('jobs.html',
{ jobs: newJobs, jobType: 'new', jobsCount: jobsCount, page: page, nextPage: page + 1, prevPage: page - 1 }))
})
app.get('/applied/:page', (c) => {
const page = Number(c.req.param('page'))
const appliedJobs = repo.getAppliedJobs(page)
const jobsCount = repo.countJobs()
return c.html(nunjucks.render('jobs.html',
{ jobs: appliedJobs, jobType: 'applied', jobsCount: jobsCount, page: page, nextPage: page + 1, prevPage: page - 1 }))
})
app.get('/ignored/:page', (c) => {
const page = Number(c.req.param('page'))
const ignoredJobs = repo.getIgnoredJobs(page)
const jobsCount = repo.countJobs()
return c.html(nunjucks.render('jobs.html',
{ jobs: ignoredJobs, jobType: 'ignored', jobsCount: jobsCount, page: page, nextPage: page + 1, prevPage: page - 1 }))
})
app.post('/api/save-jobs', async (c) => {
const jobs: Array<RawJob> = await c.req.json()
try {
repo.saveJobs(jobs)
c.status(201)
return c.json({ status: "success" })
} catch (error) {
console.error(error)
c.status(500)
return c.json({ status: "failed" })
}
})
app.post('/api/applied/:jobid', async (c) => {
const jobid = c.req.param('jobid')
const status = repo.toggleApplied(jobid)
return c.json({ status: status })
})
app.post('/api/ignored/:jobid', async (c) => {
const jobid = c.req.param('jobid')
const status = repo.toggleIgnored(jobid)
return c.json({ status: status })
})
console.log("\nHono server started...\n")
Bun.serve({
port: 3000,
hostname: "127.0.0.1",
development: false,
fetch: app.fetch
})