Skip to content

Commit

Permalink
Polish here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
toresbe committed Jul 27, 2023
1 parent 316bed1 commit 1272ed1
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ yarn cli create-mock-data orgs 20

### Proxy

In dev mode, toches will proxy requests coming in on /api/videos/upload to localhost:1080.
In dev mode, toches will proxy requests coming in on /api/videos/upload to UPLOAD_RECEIVER_PROXY ("http://localhost:8003").

In production mode, this is handled by our traefik ingress controller.

Expand Down
6 changes: 3 additions & 3 deletions cli/src/create-mock-data/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export const videoCommand = new Command("videos")
console.info(`Generating "${video.title}"`)
await execAsync(`fk media generate -o /tmp/file.mp4 -s 5 -t "${video.title}"`)

const { stdout } = await execAsync(`fk media upload -f /tmp/file.mp4`)
const media_id = Number(stdout.trim())
const { stdout: mediaId } = await execAsync(`fk media upload -f /tmp/file.mp4`)

const [{ id }] = await db
.insert({ ...video, media_id })
.insert({ ...video, media_id: parseInt(mediaId.trim()) })
.into("videos")
.returning("id")

console.info(`Created with ID "${id}"`)
}

Expand Down
32 changes: 1 addition & 31 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,4 @@ services:
environment:
- DATABASE_URL=postgres://postgres:fk@db/fk
- FK_API_KEY=1234
command: ["sh", "-c", "sleep 10s; yarn makeMockData"]

s3-backend:
image: scireum/s3-ninja
ports:
- "9000:9000"

media-processor:
image: frikanalen/media-processor
environment:
- FK_API_KEY=1234
- FK_API=http://localhost:8080
- AWS_ENDPOINT=http://localhost:9000
- AWS_REGION=no-where-1
- AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE # Don't be fear, these are s3ninja defaults :)
- AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
- REDIS_URL=redis://redis:6379
ports:
- "8001:8000"

redis:
image: "redis:alpine"
ports:
- "6379:6379"

redis-gui:
image: rediscommander/redis-commander
environment:
- REDIS_HOSTS=local:redis:6379
ports:
- "8083:8081"
command: ["sh", "-c", "sleep 10s; yarn makeMockData"]
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"test": "jest --runInBand --detectOpenHandles --forceExit --coverageDirectory=\"./coverage\"",
"build": "tsc",
"start": "node build/index.js",
"start": "node --enable-source-maps build/index.js",
"dev": "ts-node-dev src/index.ts",
"build-cli": "tsc --project ./cli/tsconfig.json",
"cli": "node ./cli/build/index.js",
Expand All @@ -31,7 +31,6 @@
"knex": "^2.4.2",
"koa": "^2.14.2",
"koa-bodyparser": "^4.4.0",
"koa-logger": "^3.2.1",
"koa-session": "^6.4.0",
"koa2-swagger-ui": "^5.8.0",
"libxmljs2": "^0.32.0",
Expand Down
5 changes: 2 additions & 3 deletions src/modules/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { useSession } from "../auth/middleware/useSession"
import { IS_PROD } from "./constants"
import { handleError } from "./middleware/handleError"
import { sendCORSHeaders } from "./middleware/sendCORSHeaders"
import logger from "koa-logger"
import { router } from "./router"
import { apolloServer } from "../graphql/server"
import { log } from "./log"
import { log, requestLogger } from "./log"

const app = new Koa()

app.proxy = IS_PROD
app.use(logger())
app.use(requestLogger())
app.use(handleError())
app.use(bodyParser())
app.use(useSession(app))
Expand Down
46 changes: 46 additions & 0 deletions src/modules/core/log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
import { Logger } from "tslog"
import { Middleware } from "koa"

export const log = new Logger()

export const requestLogger = (): Middleware => async (context, next) => {
const startTime = Date.now()
const foo = await next()
const endTime = Date.now()
const duration = endTime - startTime

const { method, url, status, body, length } = context

// noinspection MagicNumberJS
if (status >= 500) {
log.error(`${method} ${url} ${status} ${duration}ms`, {
duration,
status,
method,
url,
length,
parameters: context.request.body,
errorBody: body?.message,
})
} else {
// noinspection MagicNumberJS
if (status >= 400) {
log.warn(`${method} ${url} ${status} ${duration}ms`, {
duration,
status,
method,
url,
length,
parameters: context.request.body,
errorBody: body?.message,
})
} else {
log.info(`${method} ${url} ${status} ${duration}ms`, {
duration,
status,
method,
url,
length,
})
}
}

return foo
}
5 changes: 2 additions & 3 deletions src/modules/video/schemas/videoSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { boolean } from "yup"
import { InferType, number, object, string } from "yup"
import { boolean, InferType, number, object, string } from "yup"
import { validateCategoryIds } from "../../category/helpers/validateCategoryIds"
import { validateMediaId } from "../helpers/validateMediaId"

Expand Down Expand Up @@ -29,7 +28,7 @@ import { validateMediaId } from "../helpers/validateMediaId"
export const videoSchema = object({
title: string().required().max(255),
description: string(),
categories: validateCategoryIds().required(),
categories: validateCategoryIds().default([]),
mediaId: validateMediaId(number().required()),
jukeboxable: boolean().default(false),
})
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"skipLibCheck": true,
"target": "ESNext",
"module": "commonjs",
"sourceMap": true,
"strict": true,
"outDir": "build",
},
Expand Down
24 changes: 2 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2587,7 +2587,7 @@ busboy@^1.6.0:
dependencies:
streamsearch "^1.1.0"

bytes@3.1.2, bytes@^3.1.0:
bytes@3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5"
integrity sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==
Expand Down Expand Up @@ -2650,7 +2650,7 @@ capital-case@^1.0.4:
tslib "^2.0.3"
upper-case-first "^2.0.2"

chalk@^2.0.0, chalk@^2.4.2:
chalk@^2.0.0:
version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
Expand Down Expand Up @@ -3822,11 +3822,6 @@ human-signals@^2.1.0:
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==

humanize-number@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/humanize-number/-/humanize-number-0.0.2.tgz#11c0af6a471643633588588048f1799541489c18"
integrity sha512-un3ZAcNQGI7RzaWGZzQDH47HETM4Wrj6z6E4TId8Yeq9w5ZKUVB1nrT2jwFheTUjEmqcgTjXDc959jum+ai1kQ==

iconv-lite@0.4.24, iconv-lite@^0.4.24:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down Expand Up @@ -4618,16 +4613,6 @@ koa-convert@^2.0.0:
co "^4.6.0"
koa-compose "^4.1.0"

koa-logger@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/koa-logger/-/koa-logger-3.2.1.tgz#ab9db879526db3837cc9ce4fd983c025b1689f22"
integrity sha512-MjlznhLLKy9+kG8nAXKJLM0/ClsQp/Or2vI3a5rbSQmgl8IJBQO0KI5FA70BvW+hqjtxjp49SpH2E7okS6NmHg==
dependencies:
bytes "^3.1.0"
chalk "^2.4.2"
humanize-number "0.0.2"
passthrough-counter "^1.0.0"

koa-session@^6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/koa-session/-/koa-session-6.4.0.tgz#f17c6f1844b37114192aa23a0ccf4f58c3042e96"
Expand Down Expand Up @@ -5286,11 +5271,6 @@ pascal-case@^3.1.2:
no-case "^3.0.4"
tslib "^2.0.3"

passthrough-counter@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/passthrough-counter/-/passthrough-counter-1.0.0.tgz#1967d9e66da572b5c023c787db112a387ab166fa"
integrity sha512-Wy8PXTLqPAN0oEgBrlnsXPMww3SYJ44tQ8aVrGAI4h4JZYCS0oYqsPqtPR8OhJpv6qFbpbB7XAn0liKV7EXubA==

path-case@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/path-case/-/path-case-3.0.4.tgz#9168645334eb942658375c56f80b4c0cb5f82c6f"
Expand Down

0 comments on commit 1272ed1

Please sign in to comment.