-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
115 lines (104 loc) · 3.06 KB
/
app.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import 'dotenv/config';
import path from 'node:path';
import crypto from 'node:crypto';
import Fastify from 'fastify';
import FastifyAuth from '@fastify/auth';
import FastifyVite from '@fastify/vite';
import FastifyCookie from '@fastify/cookie';
import FastifySession from '@fastify/session';
import FastifyPostgres from '@fastify/postgres';
import FastifyFormBody from '@fastify/formbody';
import AutoLoad from '@fastify/autoload';
import Swagger from '@fastify/swagger';
import SwaggerUI from '@fastify/swagger-ui';
export async function build(opts = {}) {
const fastify = Fastify(opts);
await fastify.register(FastifyFormBody);
await fastify.register(FastifyVite, {
root: import.meta.url,
renderer: '@fastify/react',
dev: process.argv.includes('--dev'),
});
await fastify.vite.ready();
fastify.register(FastifyCookie);
fastify.register(FastifySession, {
cookieName: 'SESSIONID',
secret: crypto.randomBytes(32).toString('hex'),
cookie: {
secure: false,
},
expires: 60 * 60 * 1000,
});
fastify.register(Swagger, {
openapi: {
info: {
title: 'Lumina Solar API',
description: 'Provides access to the Lumina Solar API',
version: '1.0',
},
components: {
securitySchemes: {
cookieAuth: {
type: 'apiKey',
in: 'cookie',
name: 'SESSIONID',
},
},
},
},
refResolver: {
buildLocalReference: (json, _baseUri, _fragment, _i) => {
return json.$id || `def-{i}`;
},
},
transform: ({ schema, url, _route, _swaggerObject }) => {
const transformedSchema = Object.assign({}, schema);
if (!url.startsWith('/api')) {
transformedSchema.hide = true;
}
return { schema: transformedSchema, url };
},
});
fastify.register(SwaggerUI, {
routePrefix: '/api-docs',
});
fastify.register(FastifyPostgres, {
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false,
},
});
// This loads all plugins defined in plugins
// those should be support plugins that are reused
// through your application
fastify.register(AutoLoad, {
dir: path.join(import.meta.dirname, 'plugins'),
});
fastify
.decorate('verifyUserAndPassword', async function (request, _reply) {
const { username, password } = request.body;
const isAuthenticated = await fastify.db.authenticate(username, password);
if (!isAuthenticated) {
throw new Error('Invalid credentials');
}
})
.decorate('verifySession', async function (request, _reply) {
if (!request.session || !request.session.user) {
throw new Error('User not logged in');
}
})
.register(FastifyAuth)
.after(() => {
// This loads all plugins defined in routes
// define your routes in one of these
fastify.register(AutoLoad, {
dir: path.join(import.meta.dirname, 'routes'),
options: {
prefix: '/api',
},
});
});
await fastify.ready();
fastify.swagger();
return fastify;
}