aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/misc.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/controllers/misc.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/controllers/misc.ts')
-rw-r--r--server/controllers/misc.ts210
1 files changed, 0 insertions, 210 deletions
diff --git a/server/controllers/misc.ts b/server/controllers/misc.ts
deleted file mode 100644
index a7dfc7867..000000000
--- a/server/controllers/misc.ts
+++ /dev/null
@@ -1,210 +0,0 @@
1import cors from 'cors'
2import express from 'express'
3import { CONFIG, isEmailEnabled } from '@server/initializers/config'
4import { serveIndexHTML } from '@server/lib/client-html'
5import { ServerConfigManager } from '@server/lib/server-config-manager'
6import { HttpStatusCode } from '@shared/models'
7import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo/nodeinfo.model'
8import { CONSTRAINTS_FIELDS, DEFAULT_THEME_NAME, PEERTUBE_VERSION, ROUTE_CACHE_LIFETIME } from '../initializers/constants'
9import { getThemeOrDefault } from '../lib/plugins/theme-utils'
10import { apiRateLimiter, asyncMiddleware } from '../middlewares'
11import { cacheRoute } from '../middlewares/cache/cache'
12import { UserModel } from '../models/user/user'
13import { VideoModel } from '../models/video/video'
14import { VideoCommentModel } from '../models/video/video-comment'
15
16const miscRouter = express.Router()
17
18miscRouter.use(cors())
19
20miscRouter.use('/nodeinfo/:version.json',
21 apiRateLimiter,
22 cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO),
23 asyncMiddleware(generateNodeinfo)
24)
25
26// robots.txt service
27miscRouter.get('/robots.txt',
28 apiRateLimiter,
29 cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS),
30 (_, res: express.Response) => {
31 res.type('text/plain')
32
33 return res.send(CONFIG.INSTANCE.ROBOTS)
34 }
35)
36
37miscRouter.all('/teapot',
38 apiRateLimiter,
39 getCup,
40 asyncMiddleware(serveIndexHTML)
41)
42
43// security.txt service
44miscRouter.get('/security.txt',
45 apiRateLimiter,
46 (_, res: express.Response) => {
47 return res.redirect(HttpStatusCode.MOVED_PERMANENTLY_301, '/.well-known/security.txt')
48 }
49)
50
51// ---------------------------------------------------------------------------
52
53export {
54 miscRouter
55}
56
57// ---------------------------------------------------------------------------
58
59async function generateNodeinfo (req: express.Request, res: express.Response) {
60 const { totalVideos } = await VideoModel.getStats()
61 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
62 const { totalUsers, totalMonthlyActiveUsers, totalHalfYearActiveUsers } = await UserModel.getStats()
63
64 if (!req.params.version || req.params.version !== '2.0') {
65 return res.fail({
66 status: HttpStatusCode.NOT_FOUND_404,
67 message: 'Nodeinfo schema version not handled'
68 })
69 }
70
71 const json = {
72 version: '2.0',
73 software: {
74 name: 'peertube',
75 version: PEERTUBE_VERSION
76 },
77 protocols: [
78 'activitypub'
79 ],
80 services: {
81 inbound: [],
82 outbound: [
83 'atom1.0',
84 'rss2.0'
85 ]
86 },
87 openRegistrations: CONFIG.SIGNUP.ENABLED,
88 usage: {
89 users: {
90 total: totalUsers,
91 activeMonth: totalMonthlyActiveUsers,
92 activeHalfyear: totalHalfYearActiveUsers
93 },
94 localPosts: totalVideos,
95 localComments: totalLocalVideoComments
96 },
97 metadata: {
98 taxonomy: {
99 postsName: 'Videos'
100 },
101 nodeName: CONFIG.INSTANCE.NAME,
102 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
103 nodeConfig: {
104 search: {
105 remoteUri: {
106 users: CONFIG.SEARCH.REMOTE_URI.USERS,
107 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
108 }
109 },
110 plugin: {
111 registered: ServerConfigManager.Instance.getRegisteredPlugins()
112 },
113 theme: {
114 registered: ServerConfigManager.Instance.getRegisteredThemes(),
115 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
116 },
117 email: {
118 enabled: isEmailEnabled()
119 },
120 contactForm: {
121 enabled: CONFIG.CONTACT_FORM.ENABLED
122 },
123 transcoding: {
124 hls: {
125 enabled: CONFIG.TRANSCODING.HLS.ENABLED
126 },
127 web_videos: {
128 enabled: CONFIG.TRANSCODING.WEB_VIDEOS.ENABLED
129 },
130 enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('vod')
131 },
132 live: {
133 enabled: CONFIG.LIVE.ENABLED,
134 transcoding: {
135 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
136 enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('live')
137 }
138 },
139 import: {
140 videos: {
141 http: {
142 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
143 },
144 torrent: {
145 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
146 }
147 }
148 },
149 autoBlacklist: {
150 videos: {
151 ofUsers: {
152 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
153 }
154 }
155 },
156 avatar: {
157 file: {
158 size: {
159 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
160 },
161 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
162 }
163 },
164 video: {
165 image: {
166 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
167 size: {
168 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
169 }
170 },
171 file: {
172 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
173 }
174 },
175 videoCaption: {
176 file: {
177 size: {
178 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
179 },
180 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
181 }
182 },
183 user: {
184 videoQuota: CONFIG.USER.VIDEO_QUOTA,
185 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
186 },
187 trending: {
188 videos: {
189 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
190 }
191 },
192 tracker: {
193 enabled: CONFIG.TRACKER.ENABLED
194 }
195 }
196 }
197 } as HttpNodeinfoDiasporaSoftwareNsSchema20
198
199 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
200 .send(json)
201 .end()
202}
203
204function getCup (req: express.Request, res: express.Response, next: express.NextFunction) {
205 res.status(HttpStatusCode.I_AM_A_TEAPOT_418)
206 res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1')
207 res.setHeader('Safe', 'if-sepia-awake')
208
209 return next()
210}