Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added shared env parsing #44

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,22 @@
"dev": "tsc-watch --noClear --onSuccess \"node ./dist/index.js\""
},
"devDependencies": {
"@types/find-config": "^1.0.4",
"@types/node": "^22.1.0",
"tsc-watch": "^6.2.0",
"typescript": "^5.5.4"
},
"dependencies": {
"@anatine/zod-openapi": "^2.2.6",
"@aws-sdk/client-s3": "^3.623.0",
"@bull-board/api": "^5.21.3",
"@bull-board/fastify": "^5.21.3",
"@fastify/cors": "^9.0.1",
"@fastify/swagger": "^8.15.0",
"@fastify/swagger-ui": "^4.0.1",
"@mixwave/artisan": "workspace:*",
"@mixwave/shared": "workspace:*",
"@ts-rest/core": "^3.49.3",
"@ts-rest/fastify": "^3.49.3",
"@ts-rest/open-api": "^3.49.3",
"@types/qs": "^6.9.15",
"bullmq": "^5.12.0",
"dotenv": "^16.4.5",
"fastify": "^4.28.1",
"fastify-plugin": "^4.5.1",
"find-config": "^1.0.0",
"object-property-extractor": "^1.0.11",
"zod": "^3.23.8"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { initContract } from "@ts-rest/core";
import { streamSchema, inputSchema } from "@mixwave/shared/artisan";
import { streamSchema, inputSchema } from "@mixwave/shared/schema";
import * as z from "zod";
import { extendZodWithOpenApi } from "@anatine/zod-openapi";
import {
Expand Down
17 changes: 6 additions & 11 deletions packages/api/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { z } from "zod";
import findConfig from "find-config";
import { config } from "dotenv";
import { parseEnv } from "@mixwave/shared/env";

const configPath = findConfig("config.env");
if (configPath) {
config({ path: configPath });
}

const envSchema = z.object({
export const env = parseEnv({
// process
PORT: z.coerce.number().default(52001),
HOST: z.string().default("0.0.0.0"),

// config.env
REDIS_HOST: z.string(),
REDIS_PORT: z.coerce.number(),
PUBLIC_API_ENDPOINT: z.string(),
S3_ENDPOINT: z.string(),
S3_REGION: z.string(),
S3_ACCESS_KEY: z.string(),
S3_SECRET_KEY: z.string(),
S3_BUCKET: z.string(),
PUBLIC_API_ENDPOINT: z.string(),
});

export const env = envSchema.parse(process.env);
3 changes: 0 additions & 3 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Fastify from "fastify";
import cors from "@fastify/cors";
import { env } from "./env.js";
import { contract } from "./contract.js";
import { bullBoardPlugin } from "./plugins/bull-board.js";
import { initServer } from "@ts-rest/fastify";
import { addTranscodeJob, addPackageJob } from "@mixwave/artisan/producer";
import { getJobs, getJob, getJobLogs } from "./jobs.js";
Expand Down Expand Up @@ -72,8 +71,6 @@ async function buildServer(): Promise<FastifyInstance> {

app.register(s.plugin(router));

app.register(bullBoardPlugin);

return await app;
}

Expand Down
21 changes: 0 additions & 21 deletions packages/api/src/plugins/bull-board.ts

This file was deleted.

7 changes: 2 additions & 5 deletions packages/artisan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "@mixwave/artisan",
"type": "module",
"exports": {
"./producer": "./dist/producer/index.js"
"./producer": "./dist/producer.js"
},
"typesVersions": {
"*": {
"producer": [
"src/producer/index.ts"
"src/producer.ts"
]
}
},
Expand All @@ -16,7 +16,6 @@
"dev": "tsc-watch --noClear --onSuccess \"node ./dist/consumer/index.js\""
},
"devDependencies": {
"@types/find-config": "^1.0.4",
"@types/mime-types": "^2.1.4",
"@types/node": "^22.1.0",
"@types/parse-filepath": "^1.0.2",
Expand All @@ -27,10 +26,8 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.623.0",
"bullmq": "^5.12.0",
"dotenv": "^16.4.5",
"ffmpeg-static": "^5.2.0",
"ffmpeggy": "^3.0.1",
"find-config": "^1.0.0",
"glob": "^11.0.0",
"iso-language-codes": "^2.0.0",
"mime-types": "^2.1.35",
Expand Down
13 changes: 0 additions & 13 deletions packages/artisan/src/connection.ts

This file was deleted.

13 changes: 3 additions & 10 deletions packages/artisan/src/consumer/env.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { parseEnv } from "@mixwave/shared/env";
import { z } from "zod";
import findConfig from "find-config";
import { config } from "dotenv";

const configPath = findConfig("config.env");
if (configPath) {
config({ path: configPath });
}

const envSchema = z.object({
export const env = parseEnv({
// config.env
S3_ENDPOINT: z.string(),
S3_REGION: z.string(),
S3_ACCESS_KEY: z.string(),
Expand All @@ -16,5 +11,3 @@ const envSchema = z.object({
REDIS_HOST: z.string(),
REDIS_PORT: z.coerce.number(),
});

export const env = envSchema.parse(process.env);
7 changes: 6 additions & 1 deletion packages/artisan/src/consumer/job-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Job, Queue } from "bullmq";
import { connection } from "../connection.js";
import { env } from "./env.js";

const connection = {
host: env.REDIS_HOST,
port: env.REDIS_PORT,
};

export async function getFakeJob<T>(job: Job) {
if (!job.id) {
Expand Down
2 changes: 1 addition & 1 deletion packages/artisan/src/consumer/workers/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import parseFilePath from "parse-filepath";
import { FFmpeggy } from "ffmpeggy";
import ffmpegBin from "ffmpeg-static";
import type { Job } from "bullmq";
import type { Stream, Input } from "@mixwave/shared/artisan";
import type { Stream, Input } from "@mixwave/shared/schema";

if (!ffmpegBin) {
throw new Error("Cannot find ffmpeg bin");
Expand Down
2 changes: 1 addition & 1 deletion packages/artisan/src/consumer/workers/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { by639_2T } from "iso-language-codes";
import { copyFile, downloadFolder, uploadFolder } from "../s3.js";
import parseFilePath from "parse-filepath";
import * as z from "zod";
import { streamSchema } from "@mixwave/shared/artisan";
import { streamSchema } from "@mixwave/shared/schema";
import type { Job } from "bullmq";
import type { Code } from "iso-language-codes";

Expand Down
4 changes: 2 additions & 2 deletions packages/artisan/src/consumer/workers/transcode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getFakeJob } from "../job-helpers.js";
import { uploadJsonFile } from "../s3.js";
import { addPackageJob } from "../../producer/index.js";
import type { Stream } from "@mixwave/shared/artisan";
import { addPackageJob } from "../../producer.js";
import type { Stream } from "@mixwave/shared/schema";
import type { FfmpegResult } from "./ffmpeg.js";
import type { Job } from "bullmq";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Queue, FlowProducer, Job } from "bullmq";
import { connection } from "../connection.js";
import { randomUUID } from "crypto";
import type { Input, Stream } from "@mixwave/shared/artisan";
import type { TranscodeData } from "../consumer/workers/transcode.js";
import type { PackageData } from "../consumer/workers/package.js";
import type { FfmpegData } from "../consumer/workers/ffmpeg.js";
import type { Input, Stream } from "@mixwave/shared/schema";
import type { TranscodeData } from "./consumer/workers/transcode.js";
import type { PackageData } from "./consumer/workers/package.js";
import type { FfmpegData } from "./consumer/workers/ffmpeg.js";
import type { FlowChildJob } from "bullmq";

const connection = {
host: process.env.REDIS_HOST,
port: +process.env.REDIS_PORT!,
};

export const flowProducer = new FlowProducer({
connection,
});
Expand Down
8 changes: 2 additions & 6 deletions packages/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@
"@ts-rest/react-query": "^3.51.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
"lucide-react": "^0.424.0",
"monaco-editor": "^0.51.0",
"pretty-bytes": "^6.1.1",
"pretty-ms": "^9.1.0",
"react": "^18.3.1",
"react-day-picker": "8.10.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.52.2",
"react-router-dom": "^6.26.0",
Expand All @@ -49,18 +47,16 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@types/find-config": "^1.0.4",
"@types/node": "^22.1.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/react-syntax-highlighter": "^15.5.13",
"@vitejs/plugin-react": "^4.3.1",
"autoprefixer": "^10.4.20",
"dotenv": "^16.4.5",
"find-config": "^1.0.0",
"postcss": "^8.4.41",
"tailwindcss": "^3.4.7",
"typescript": "^5.2.2",
"vite": "^5.3.4"
"vite": "^5.3.4",
"@mixwave/shared": "workspace:*"
}
}
9 changes: 3 additions & 6 deletions packages/dashboard/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
import { fileURLToPath } from "url";
import findConfig from "find-config";
import { config } from "dotenv";

const configPath = findConfig("config.env");
if (configPath) {
config({ path: configPath });
}
// Use shared env, but below we set the envPrefix to ensure no
// secrets are being leaked to the client.
import "@mixwave/shared/env";

// https://vitejs.dev/config/
export default defineConfig({
Expand Down
13 changes: 10 additions & 3 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
"name": "@mixwave/shared",
"type": "module",
"exports": {
"./artisan": "./dist/artisan.js"
"./schema": "./dist/schema.js",
"./env": "./dist/env.js"
},
"typesVersions": {
"*": {
"artisan": [
"./src/artisan.ts"
"schema": [
"./src/schema.ts"
],
"env": [
"./src/env.ts"
]
}
},
Expand All @@ -17,10 +21,13 @@
},
"dependencies": {
"@anatine/zod-openapi": "^2.2.6",
"dotenv": "^16.4.5",
"find-config": "^1.0.0",
"iso-language-codes": "^2.0.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/find-config": "^1.0.4",
"tsc-watch": "^6.2.0",
"typescript": "^5.6.2"
}
Expand Down
13 changes: 13 additions & 0 deletions packages/shared/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { z } from "zod";
import findConfig from "find-config";
import { config } from "dotenv";
import type { ZodRawShape } from "zod";

const configPath = findConfig("config.env");
if (configPath) {
config({ path: configPath });
}

export function parseEnv<T extends ZodRawShape>(shape: T) {
return z.object(shape).parse(process.env);
}
File renamed without changes.
6 changes: 2 additions & 4 deletions packages/stitcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"test": "vitest run"
},
"devDependencies": {
"@types/find-config": "^1.0.4",
"@types/hh-mm-ss": "^1.2.3",
"@types/lru-cache": "^7.10.10",
"@types/luxon": "^3.4.2",
Expand All @@ -26,15 +25,14 @@
"@ts-rest/open-api": "^3.49.3",
"@xmldom/xmldom": "^0.8.10",
"dom-parser": "^1.1.5",
"dotenv": "^16.4.5",
"fastify": "^4.28.1",
"find-config": "^1.0.0",
"hh-mm-ss": "^1.2.0",
"lru-cache": "^11.0.1",
"luxon": "^3.5.0",
"redis": "^4.7.0",
"uuid": "^10.0.0",
"vast-client": "workspace:*",
"zod": "^3.23.8"
"zod": "^3.23.8",
"@mixwave/shared": "workspace:*"
}
}
15 changes: 5 additions & 10 deletions packages/stitcher/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { parseEnv } from "@mixwave/shared/env";
import { z } from "zod";
import findConfig from "find-config";
import { config } from "dotenv";

const configPath = findConfig("config.env");
if (configPath) {
config({ path: configPath });
}

const envSchema = z.object({
export const env = parseEnv({
// process
PORT: z.coerce.number().default(52002),
HOST: z.string().default("0.0.0.0"),

// config.env
REDIS_HOST: z.string(),
REDIS_PORT: z.coerce.number(),
PUBLIC_S3_ENDPOINT: z.string(),
PUBLIC_STITCHER_ENDPOINT: z.string(),
});

export const env = envSchema.parse(process.env);
Loading