-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
104 lines (93 loc) · 2.46 KB
/
main.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
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
import { serve } from '@hono/node-server';
import { apiReference } from '@scalar/hono-api-reference';
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { accounts } from './pkg/accounts/mod.js';
import { drive } from './pkg/drive/mod.js';
import { noteHandlers } from './pkg/notes/mod.js';
import { timeline } from './pkg/timeline/mod.js';
import { Logger } from 'tslog';
import { isProduction } from './pkg/adaptors/env.js';
const coreLogger = new Logger({
name: "Pulsate",
type: "pretty",
})
export const app = new Hono().get('/doc', async (c) => {
// NOTE: If you create a new module, you must add module API doc base path here.
const modulePath: string[] = ['accounts', 'notes', 'drive', 'timeline'];
const basePath = 'http://localhost:3000/';
const openAPIBase = {
openapi: '3.1.0',
info: {
description: '',
title: 'Pulsate API Document',
version: '0.1.0',
},
servers: [
{
url: 'http://localhost:3000',
description: 'Local server',
},
],
components: {
securitySchemes: {
bearer: {
type: 'http',
scheme: 'bearer',
},
},
schemas: {},
parameters: {},
},
paths: {},
};
const res = modulePath.map(async (path) => {
return (await fetch(`${basePath}${path}/doc.json`)).json();
});
for (const v in res) {
const data = await res[v];
openAPIBase.components.schemas = Object.assign(
openAPIBase.components.schemas,
data.components.schemas,
);
openAPIBase.components.parameters = Object.assign(
openAPIBase.components.parameters,
data.components.parameters,
);
openAPIBase.paths = Object.assign(openAPIBase.paths, data.paths);
}
return c.json(openAPIBase);
});
app.use(
'*',
cors({
origin: '*',
allowMethods: ['POST', 'GET', 'OPTIONS', 'PUT', 'PATCH'],
}),
);
/*
All routes must be "/"
(The "/" account cannot be written in the library specification.
*/
app.route('/', noteHandlers);
app.route('/', accounts);
app.route('/', drive);
app.route('/', timeline);
app.get(
'/reference',
apiReference({
pageTitle: 'Pulsate API',
spec: {
url: '/doc',
},
}),
);
serve({ fetch: app.fetch, port: 3000 }, (addr) => {
coreLogger.info("Pulsate v0.1");
if (isProduction) {
coreLogger.info("Production mode");
} else {
coreLogger.info("Development mode");
}
coreLogger.info(`Server started at ${addr.address}:${addr.port} ${addr.family}`);
});