Browse Source

Prepare for new architecture

master
Dwayne Harris 5 years ago
parent
commit
d148dc786a
  1. 8
      package.json
  2. 4
      src/lib/media.ts
  3. 5
      src/plugins/api/index.ts
  4. 4
      src/plugins/api/media.ts
  5. 12
      src/server.ts
  6. 10
      tsconfig.json

8
package.json

@ -4,15 +4,15 @@
"version": "1.0.3",
"private": true,
"engines": {
"node": ">=10.0.0"
"node": ">=12.0.0"
},
"scripts": {
"start": "node dist/server.js",
"build": "tsc",
"prestart": "npm run build",
"build": "tsc",
"watch": "run-p watch-typescript watch-server",
"watch-server": "nodemon dist/server.js",
"watch-typescript": "tsc -w",
"watch": "run-p watch-typescript watch-server"
"watch-typescript": "tsc -w"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",

4
src/lib/media.ts

@ -5,6 +5,8 @@ import moment from 'moment'
import { MEDIA_PARTITION_KEY } from '../constants'
import { Media } from '../types/collections'
export const getBlobUrl = () => `https://${process.env.BLOB_STORAGE_ACCOUNT!}.blob.core.windows.net/${process.env.BLOB_STORAGE_CONTAINER!}`
export function generateSAS(permissions: string, expirationMinutes: number) {
const sharedKeyCredential = new StorageSharedKeyCredential(process.env.BLOB_STORAGE_ACCOUNT!, process.env.BLOB_STORAGE_ACCOUNT_KEY!)
@ -18,7 +20,7 @@ export function generateSAS(permissions: string, expirationMinutes: number) {
export async function deleteMedia(name: string) {
const blobServiceClient = new BlobServiceClient(
`https://${process.env.BLOB_STORAGE_ACCOUNT!}.blob.core.windows.net/${process.env.BLOB_STORAGE_CONTAINER!}/${name}`,
`${getBlobUrl()}/${name}`,
new StorageSharedKeyCredential(process.env.BLOB_STORAGE_ACCOUNT!, process.env.BLOB_STORAGE_ACCOUNT_KEY!)
)

5
src/plugins/api/index.ts

@ -93,11 +93,6 @@ const plugin: Plugin<Server, IncomingMessage, ServerResponse, PluginOptions> = a
version: process.env.NPM_PACKAGE_VERSION,
}
})
server.get('/.well-known/pki-validation/A26231F1EF6AC3FB8767F56CF5FDEEAC.txt', {}, async (request, reply) => {
reply.type('text/plain')
reply.send('50E0E6A3BA4AE16AD6B3D92302DA3860C511848E9EDC8EDF34B45594C6D25F7D comodoca.com 5e0587a07e5ee')
})
}
export default plugin

4
src/plugins/api/media.ts

@ -14,7 +14,7 @@ import { MEDIA_PARTITION_KEY } from '../../constants'
import { errorSchema } from '../../schemas'
import { containerFor, getItem } from '../../lib/database'
import { badRequestError, serverError } from '../../lib/errors'
import { deleteMedia, generateSAS } from '../../lib/media'
import { deleteMedia, generateSAS, getBlobUrl } from '../../lib/media'
import { createId } from '../../lib/utils'
import { Media } from '../../types/collections'
@ -30,6 +30,7 @@ function getSASRoute(server: FastifyInstance<Server, IncomingMessage, ServerResp
type: 'object',
properties: {
sas: { type: 'string' },
blobUrl: { type: 'string' },
id: { type: 'string' },
},
},
@ -40,6 +41,7 @@ function getSASRoute(server: FastifyInstance<Server, IncomingMessage, ServerResp
server.get<DefaultQuery, DefaultParams, DefaultHeaders, DefaultBody>('/v1/sas', options, async () => {
return {
sas: generateSAS('arcw', 5),
blobUrl: getBlobUrl(),
id: createId(),
}
})

12
src/server.ts

@ -1,7 +1,6 @@
import { config } from 'dotenv'
import fastify from 'fastify'
import helmet from 'fastify-helmet'
import cors from 'fastify-cors'
import swagger from 'fastify-swagger'
import api from './plugins/api'
@ -15,11 +14,6 @@ const server = fastify({
})
server.register(helmet)
server.register(cors, {
origin: true,
credentials: false,
maxAge: 2592000,
})
server.register(swagger, {
routePrefix: '/docs',
@ -39,7 +33,7 @@ server.register(swagger, {
{ name: 'user', description: 'User endpoints.' },
],
host: 'localhost',
schemes: ['http'],
schemes: ['http', 'https'],
},
})
@ -52,11 +46,11 @@ server.ready(err => {
const port = parseInt(process.env.PORT!, 10)
server.listen(port, '0.0.0.0', (err, address) => {
server.listen(port, (err, address) => {
if (err) {
server.log.error(err)
process.exit(1)
}
server.log.info(`✊🏾 Flexor listening at ${address}`)
server.log.info(`✊🏾 Flexor API listening at ${address}`)
})

10
tsconfig.json

@ -1,14 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"baseUrl": ".",
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"sourceMap": true,
"strict": true,
"target": "es6",
"paths": {
"*": [
"node_modules/*",

Loading…
Cancel
Save