[ABANDONED] API server for Flexor social network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import { CosmosClient } from '@azure/cosmos'
  2. import { Plugin } from 'fastify'
  3. import trim from 'lodash/trim'
  4. import { Server, IncomingMessage, ServerResponse } from 'http'
  5. import { JWT } from '../../lib/crypto'
  6. import { tokenFromHeader } from '../../lib/http'
  7. import apps from './apps'
  8. import authentication from './authentication'
  9. import groups from './groups'
  10. import media from './media'
  11. import posts from './posts'
  12. import users from './users'
  13. import { PluginOptions, HttpError } from '../../types'
  14. interface Database {
  15. client: CosmosClient
  16. }
  17. interface UserInfo {
  18. id: string
  19. }
  20. declare module "fastify" {
  21. interface FastifyInstance {
  22. database?: Database
  23. }
  24. interface FastifyRequest{
  25. viewer?: UserInfo
  26. }
  27. }
  28. const plugin: Plugin<Server, IncomingMessage, ServerResponse, PluginOptions> = async server => {
  29. const database: Database = {
  30. client: new CosmosClient({
  31. endpoint: process.env.DATABASE_ENDPOINT!,
  32. key: process.env.DATABASE_PRIMARY_KEY,
  33. }),
  34. }
  35. server.decorate('database', database)
  36. server.decorateRequest('viewer', null)
  37. server.addHook('preHandler', async req => {
  38. const token = tokenFromHeader(req.headers.authorization)
  39. if (!token) return
  40. try {
  41. const data: JWT.JWTData = await JWT.verify(token)
  42. if (!data.sub) throw new Error('Invalid token')
  43. req.viewer = {
  44. id: data.sub
  45. }
  46. } catch (err) {
  47. req.log.error('Unable to decode token')
  48. req.log.error(err)
  49. }
  50. })
  51. server.setErrorHandler(function(error, request, reply) {
  52. request.log.error(error)
  53. const response: HttpError = {
  54. message: error.message,
  55. }
  56. if (error.validation) {
  57. response.errors = error.validation.map(e => {
  58. return {
  59. field: trim(e.dataPath, '.'),
  60. message: e.message,
  61. }
  62. })
  63. }
  64. reply.send(response)
  65. })
  66. server.register(apps)
  67. server.register(authentication)
  68. server.register(groups)
  69. server.register(media)
  70. server.register(posts)
  71. server.register(users)
  72. server.get('/', {}, async () => {
  73. return {
  74. app: 'flexor',
  75. version: process.env.NPM_PACKAGE_VERSION,
  76. }
  77. })
  78. }
  79. export default plugin