mirror of
https://github.com/standardnotes/server
synced 2026-10-20 03:03:38 -04:00
* fix(api-gateway): reduce exports * wip controllers * fix: imports of controllers * fix(api-gateway): rename http service interface to proxy interface * wip: self-registering services and controllers * wip: add registering controller method bindings and services in container * feat: merge two services together * wip: resolving endpoints to direct code calls * wip: bind controller container to a singleton * fix: controller binding to instantiate and self-register on controller container * fix: move signout endpoint to auth controller * wip: define inversify controllers in the controller container * fix(auth): bind inversify controllers to controller container * fix(auth): linter issues * fix(auth): specs * fix(auth): inversify controllers bindings * wip: endpoint resolving * wip: add endpoint for more auth controllers * wip: add sessions controller endpoint resolvings * wip: add subscription invites endpoint resolvings * wip: add subscription tokens endpoint resolvings * wip: add all binding for auth server controllers * wip: fix migrations path * fix: configure default env vars and ci setup
103 lines
3.1 KiB
TypeScript
103 lines
3.1 KiB
TypeScript
import 'reflect-metadata'
|
|
|
|
import { ControllerContainer, ServiceContainer } from '@standardnotes/domain-core'
|
|
import { Service as ApiGatewayService, TYPES as ApiGatewayTYPES } from '@standardnotes/api-gateway'
|
|
import { Service as AuthService } from '@standardnotes/auth-server'
|
|
import { Container } from 'inversify'
|
|
import { InversifyExpressServer } from 'inversify-express-utils'
|
|
import helmet from 'helmet'
|
|
import * as cors from 'cors'
|
|
import { text, json, Request, Response, NextFunction } from 'express'
|
|
import * as winston from 'winston'
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
const robots = require('express-robots-txt')
|
|
|
|
import { Env } from '../src/Bootstrap/Env'
|
|
|
|
const startServer = async (): Promise<void> => {
|
|
const controllerContainer = new ControllerContainer()
|
|
const serviceContainer = new ServiceContainer()
|
|
|
|
const apiGatewayService = new ApiGatewayService(serviceContainer, controllerContainer)
|
|
const authService = new AuthService(serviceContainer, controllerContainer)
|
|
|
|
const container = Container.merge(
|
|
(await apiGatewayService.getContainer()) as Container,
|
|
(await authService.getContainer()) as Container,
|
|
)
|
|
|
|
const env: Env = new Env()
|
|
env.load()
|
|
|
|
const server = new InversifyExpressServer(container)
|
|
|
|
server.setConfig((app) => {
|
|
/* eslint-disable */
|
|
app.use(helmet({
|
|
contentSecurityPolicy: {
|
|
directives: {
|
|
defaultSrc: ["https: 'self'"],
|
|
baseUri: ["'self'"],
|
|
childSrc: ["*", "blob:"],
|
|
connectSrc: ["*"],
|
|
fontSrc: ["*", "'self'"],
|
|
formAction: ["'self'"],
|
|
frameAncestors: ["*", "*.standardnotes.org", "*.standardnotes.com"],
|
|
frameSrc: ["*", "blob:"],
|
|
imgSrc: ["'self'", "*", "data:"],
|
|
manifestSrc: ["'self'"],
|
|
mediaSrc: ["'self'"],
|
|
objectSrc: ["'self'"],
|
|
scriptSrc: ["'self'"],
|
|
styleSrc: ["'self'"]
|
|
}
|
|
}
|
|
}))
|
|
/* eslint-enable */
|
|
app.use(json({ limit: '50mb' }))
|
|
app.use(
|
|
text({
|
|
type: ['text/plain', 'application/x-www-form-urlencoded', 'application/x-www-form-urlencoded; charset=utf-8'],
|
|
}),
|
|
)
|
|
app.use(cors())
|
|
app.use(
|
|
robots({
|
|
UserAgent: '*',
|
|
Disallow: '/',
|
|
}),
|
|
)
|
|
})
|
|
|
|
const logger: winston.Logger = container.get(ApiGatewayTYPES.Logger)
|
|
|
|
server.setErrorConfig((app) => {
|
|
app.use((error: Record<string, unknown>, _request: Request, response: Response, _next: NextFunction) => {
|
|
logger.error(error.stack)
|
|
|
|
response.status(500).send({
|
|
error: {
|
|
message:
|
|
"Unfortunately, we couldn't handle your request. Please try again or contact our support if the error persists.",
|
|
},
|
|
})
|
|
})
|
|
})
|
|
|
|
const serverInstance = server.build()
|
|
|
|
serverInstance.listen(env.get('PORT', true) ? +env.get('PORT', true) : 3000)
|
|
|
|
logger.info(`Server started on port ${process.env.PORT}`)
|
|
}
|
|
|
|
Promise.resolve(startServer())
|
|
.then(() => {
|
|
// eslint-disable-next-line no-console
|
|
console.log('Server started')
|
|
})
|
|
.catch((error) => {
|
|
// eslint-disable-next-line no-console
|
|
console.log(`Could not start server: ${error.message}`)
|
|
})
|