-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (39 loc) · 1.45 KB
/
index.js
File metadata and controls
43 lines (39 loc) · 1.45 KB
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
import { Elysia } from 'elysia'
import { bearer } from '@elysiajs/bearer'
import { cors } from '@elysiajs/cors'
import { getInfo, getStatus } from './api/user'
import { bookDevice, unBookDevice } from './api/booking'
import { getDevices } from './api/device'
import { login } from './api/auth'
const PESCARA_CITYCODE = 81
new Elysia()
.use(bearer())
.use(cors())
.post('/login', async ({ body: { email, password }, set }) => {
const token = await login(email, password)
if (token) return { token }
set.status = 400
return { message: 'Error' }
})
.group('/api', (app) => {
app.onBeforeHandle(async ({ set, bearer }) => {
const info = await getInfo(bearer)
if (!info) {
set.status = 401
return { message: 'Token expired' }
}
})
return app
.get('/devices', async ({ bearer }) => getDevices(bearer, PESCARA_CITYCODE))
.get('/info', async ({ bearer }) => getInfo(bearer))
.get('/status', async ({ bearer }) => getStatus(bearer))
.get('/book', async ({ query: {name}, bearer }) => bookDevice(bearer, name))
.get('/unbook', async ({ bearer }) => unBookDevice(bearer))
})
.listen({
port: 5001,
tls: {
key: Bun.file(`${import.meta.dir}/certs/server.key`),
cert: Bun.file(`${import.meta.dir}/certs/server.crt`)
}
})