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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
import { Inject, Injectable } from '@nestjs/common';
import Koa from 'koa';
import Router from '@koa/router';
import multer from '@koa/multer';
import bodyParser from 'koa-bodyparser';
import cors from '@koa/cors';
import { ModuleRef } from '@nestjs/core';
import { Config } from '@/config.js';
import { UsersRepository, InstancesRepository, AccessTokensRepository } from '@/models/index.js';
import { DI } from '@/di-symbols.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import endpoints from './endpoints.js';
import { ApiCallService } from './ApiCallService.js';
import { SignupApiService } from './SignupApiService.js';
import { SigninApiService } from './SigninApiService.js';
import { GithubServerService } from './integration/GithubServerService.js';
import { DiscordServerService } from './integration/DiscordServerService.js';
import { TwitterServerService } from './integration/TwitterServerService.js';
@Injectable()
export class ApiServerService {
constructor(
private moduleRef: ModuleRef,
@Inject(DI.config)
private config: Config,
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.instancesRepository)
private instancesRepository: InstancesRepository,
@Inject(DI.accessTokensRepository)
private accessTokensRepository: AccessTokensRepository,
private userEntityService: UserEntityService,
private apiCallService: ApiCallService,
private signupApiServiceService: SignupApiService,
private signinApiServiceService: SigninApiService,
private githubServerService: GithubServerService,
private discordServerService: DiscordServerService,
private twitterServerService: TwitterServerService,
) {
}
public createApiServer() {
const handlers: Record<string, any> = {};
for (const endpoint of endpoints) {
handlers[endpoint.name] = this.moduleRef.get('ep:' + endpoint.name, { strict: false }).exec;
}
// Init app
const apiServer = new Koa();
apiServer.use(cors({
origin: '*',
}));
// No caching
apiServer.use(async (ctx, next) => {
ctx.set('Cache-Control', 'private, max-age=0, must-revalidate');
await next();
});
apiServer.use(bodyParser({
// リクエストが multipart/form-data でない限りはJSONだと見なす
detectJSON: ctx => !ctx.is('multipart/form-data'),
}));
// Init multer instance
const upload = multer({
storage: multer.diskStorage({}),
limits: {
fileSize: this.config.maxFileSize ?? 262144000,
files: 1,
},
});
// Init router
const router = new Router();
/**
* Register endpoint handlers
*/
for (const endpoint of endpoints) {
if (endpoint.meta.requireFile) {
router.post(`/${endpoint.name}`, upload.single('file'), this.apiCallService.handleRequest.bind(this.apiCallService, endpoint, handlers[endpoint.name]));
} else {
// 後方互換性のため
if (endpoint.name.includes('-')) {
router.post(`/${endpoint.name.replace(/-/g, '_')}`, this.apiCallService.handleRequest.bind(this.apiCallService, endpoint, handlers[endpoint.name]));
if (endpoint.meta.allowGet) {
router.get(`/${endpoint.name.replace(/-/g, '_')}`, this.apiCallService.handleRequest.bind(this.apiCallService, endpoint, handlers[endpoint.name]));
} else {
router.get(`/${endpoint.name.replace(/-/g, '_')}`, async ctx => { ctx.status = 405; });
}
}
router.post(`/${endpoint.name}`, this.apiCallService.handleRequest.bind(this.apiCallService, endpoint, handlers[endpoint.name]));
if (endpoint.meta.allowGet) {
router.get(`/${endpoint.name}`, this.apiCallService.handleRequest.bind(this.apiCallService, endpoint, handlers[endpoint.name]));
} else {
router.get(`/${endpoint.name}`, async ctx => { ctx.status = 405; });
}
}
}
router.post('/signup', ctx => this.signupApiServiceService.signup(ctx));
router.post('/signin', ctx => this.signinApiServiceService.signin(ctx));
router.post('/signup-pending', ctx => this.signupApiServiceService.signupPending(ctx));
router.use(this.discordServerService.create().routes());
router.use(this.githubServerService.create().routes());
router.use(this.twitterServerService.create().routes());
router.get('/v1/instance/peers', async ctx => {
const instances = await this.instancesRepository.find({
select: ['host'],
});
ctx.body = instances.map(instance => instance.host);
});
router.post('/miauth/:session/check', async ctx => {
const token = await this.accessTokensRepository.findOneBy({
session: ctx.params.session,
});
if (token && token.session != null && !token.fetched) {
this.accessTokensRepository.update(token.id, {
fetched: true,
});
ctx.body = {
ok: true,
token: token.token,
user: await this.userEntityService.pack(token.userId, null, { detail: true }),
};
} else {
ctx.body = {
ok: false,
};
}
});
// Return 404 for unknown API
router.all('(.*)', async ctx => {
ctx.status = 404;
});
// Register router
apiServer.use(router.routes());
return apiServer;
}
}
|