Skip to content

Commit

Permalink
feat: add worker and re-use packages
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Jul 1, 2024
1 parent fec403c commit 53c10c5
Show file tree
Hide file tree
Showing 121 changed files with 2,375 additions and 394 deletions.
3 changes: 3 additions & 0 deletions apps/web/.env.example → .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=null
NEXT_PUBLIC_STRIPE_STARTER_PLAN_PRICE_ID=null
NEXT_PUBLIC_STRIPE_PRO_PLAN_PRICE_ID=null
NEXT_PUBLIC_ABBY_PROJECT_ID=null

WORKER_USERNAME=worker
WORKER_PASSWORD=worker
10 changes: 5 additions & 5 deletions apps/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable


FROM base AS builder
RUN apk add --no-cache libc6-compat
RUN apk update
Expand All @@ -26,8 +27,9 @@ COPY --from=builder /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
RUN pnpm install

# Build the project
ENV SKIP_ENV_VALIDATION=1
COPY --from=builder /app/out/full/ .
RUN pnpm turbo run db:generate --filter=web
RUN pnpm turbo run db:generate
RUN pnpm turbo run build --filter=web...

FROM base AS runner
Expand All @@ -46,10 +48,8 @@ COPY --from=installer /app/apps/web/package.json .
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/prisma ./apps/web/prisma


COPY --from=installer --chown=nextjs:nodejs /app/packages/db/prisma ./apps/web/prisma

# Set your entrypoint script as the entrypoint
# CMD ["cd apps/web && npm run start:docker"]
CMD ["sh", "-cx", "cd ./apps/web && npm run start:docker"]
CMD node apps/web/server.js
2 changes: 1 addition & 1 deletion apps/web/emails/invite.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Project } from "@prisma/client";
import { Project } from "@tryabby/db";
import { Button } from "@react-email/button";
import { Container } from "@react-email/container";
import { Head } from "@react-email/head";
Expand Down
10 changes: 6 additions & 4 deletions apps/web/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ const config = {
output: "standalone",
swcMinify: true,
pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx"],
transpilePackages: ["lodash-es"],
transpilePackages: [
"lodash-es",
"@tryabby/db",
"@tryabby/redis",
"@tryabby/queue",
],
i18n: {
locales: ["en"],
defaultLocale: "en",
},
experimental: {
instrumentationHook: true,
},
};

export default withPlausibleProxy()(
Expand Down
21 changes: 7 additions & 14 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
"private": true,
"scripts": {
"build": "next build",
"build:prod": "npx prisma migrate deploy && npm run build",
"postbuild": "next-sitemap",
"dev": "next dev",
"postinstall": "pnpm run db:generate",
"db:generate": "prisma generate",
"dev": "pnpm with-env next dev",
"lint": "next lint",
"start": "next start",
"seed:events": "ts-node --compiler-options {\\\"module\\\":\\\"CommonJS\\\"} prisma/seedEvents.ts",
"db:migrate": "prisma migrate dev",
"generate:coupons": "pnpm ts-node -r tsconfig-paths/register --compiler-options {\\\"module\\\":\\\"CommonJS\\\"} prisma/generateCoupons.ts",
"mailhog:up": "docker-compose -f docker-compose.mailhog.yaml up",
"mailhog:down": "docker-compose -f docker-compose.mailhog.yaml down",
"test": "vitest",
"start:docker": "npx prisma migrate deploy || { echo 'Migration failed, exiting'; exit 1; } && node server.js"
"start:docker": "pnpn turbo run db:migrate-prod || { echo 'Migration failed, exiting'; exit 1; } && node server.js",
"with-env": "dotenv -e ../../.env -c --"
},
"dependencies": {
"@code-hike/mdx": "0.9.0",
Expand All @@ -34,9 +29,6 @@
"@monaco-editor/react": "^4.5.1",
"@next-auth/prisma-adapter": "1.0.5",
"@next/mdx": "14.0.4",
"@planetscale/database": "^1.11.0",
"@prisma/adapter-planetscale": "5.6.0",
"@prisma/client": "5.6.0",
"@radix-ui/react-avatar": "^1.0.3",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
Expand Down Expand Up @@ -73,10 +65,13 @@
"@trpc/react-query": "^10.19.1",
"@trpc/server": "^10.19.1",
"@tryabby/core": "workspace:*",
"@tryabby/db": "workspace:*",
"@tryabby/devtools": "workspace:*",
"@tryabby/next": "workspace:*",
"@tryabby/queue": "workspace:*",
"@tryabby/redis": "workspace:*",
"@tryabby/services": "workspace:*",
"@types/stripe-v3": "^3.1.28",
"bullmq": "^5.8.2",
"chart.js": "^4.2.1",
"class-variance-authority": "^0.4.0",
"clsx": "^1.2.1",
Expand All @@ -86,7 +81,6 @@
"framer-motion": "^10.12.7",
"hono": "^4.2.7",
"immer": "^9.0.21",
"ioredis": "^5.4.1",
"isbot": "^3.6.8",
"lodash-es": "^4.17.21",
"logsnag": "^0.1.6",
Expand Down Expand Up @@ -142,7 +136,6 @@
"postcss": "^8.4.21",
"prettier": "^2.8.7",
"prettier-plugin-tailwindcss": "^0.1.13",
"prisma": "5.6.0",
"tailwindcss": "^3.3.1",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/prisma/generateCoupons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient } from "@prisma/client";
import { PrismaClient } from "@tryabby/db";
import fs from "node:fs/promises";
import path from "node:path";

Expand Down
2 changes: 1 addition & 1 deletion apps/web/prisma/seedEvents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient, Prisma } from "@prisma/client";
import { PrismaClient, Prisma } from "@tryabby/db";
import { AbbyEventType } from "@tryabby/core";

const prisma = new PrismaClient();
Expand Down
24 changes: 12 additions & 12 deletions apps/web/src/api/routes/legacy_project_data.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { testClient } from "hono/testing";
import { makeLegacyProjectDataRoute } from "./legacy_project_data";
import { jobManager } from "server/queue/Manager";
import { FeatureFlag, FeatureFlagValue, Option, Test } from "@prisma/client";
import { Decimal } from "@prisma/client/runtime/library";
import { afterDataRequestQueue } from "@tryabby/queue";
import { FeatureFlag, FeatureFlagValue, Option, Test } from "@tryabby/db";
import { add } from "lodash-es";

vi.mock("../../env/server.mjs", () => ({
env: {},
}));

vi.mock("server/queue/Manager", () => ({
jobManager: {
emit: vi.fn().mockResolvedValue(null),
vi.mock("@tryabby/queue", () => ({
afterDataRequestQueue: {
add: vi.fn().mockResolvedValue(null),
},
}));

Expand Down Expand Up @@ -40,16 +40,16 @@ vi.mock("server/db/client", () => ({
updatedAt: new Date(),
options: [
{
chance: { toNumber: () => 0.25 } as Decimal,
chance: { toNumber: () => 0.25 } as any,
},
{
chance: { toNumber: () => 0.25 } as Decimal,
chance: { toNumber: () => 0.25 } as any,
},
{
chance: { toNumber: () => 0.25 } as Decimal,
chance: { toNumber: () => 0.25 } as any,
},
{
chance: { toNumber: () => 0.25 } as Decimal,
chance: { toNumber: () => 0.25 } as any,
},
],
},
Expand Down Expand Up @@ -102,8 +102,8 @@ describe("Get Config", () => {
expect(data.flags?.[0]?.name).toBe("First Flag");
expect(data.flags?.[0]?.isEnabled).toBe(true);

expect(vi.mocked(jobManager.emit)).toHaveBeenCalledTimes(1);
expect(vi.mocked(jobManager.emit)).toHaveBeenCalledWith(
expect(vi.mocked(afterDataRequestQueue.add)).toHaveBeenCalledTimes(1);
expect(vi.mocked(afterDataRequestQueue.add)).toHaveBeenCalledWith(
"after-data-request",
expect.objectContaining({})
);
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/api/routes/legacy_project_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { transformFlagValue } from "lib/flags";
import createCache from "server/common/memory-cache";

import { z } from "zod";
import { afterDataRequestQueue } from "server/queue/queues";
import { afterDataRequestQueue } from "@tryabby/queue";

const configCache = createCache<string, LegacyAbbyDataResponse>({
name: "legacyConfigCache",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/api/routes/v1_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { zValidator } from "@hono/zod-validator";
import { prisma } from "server/db/client";
import { hashString } from "utils/apiKey";
import * as ConfigService from "server/services/ConfigService";
import { ApiKey } from "@prisma/client";
import { ApiKey } from "@tryabby/db";
import { abbyConfigSchema } from "@tryabby/core";

const apiKeyMiddleware: MiddlewareHandler<{
Expand Down
5 changes: 1 addition & 4 deletions apps/web/src/api/routes/v1_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import { abbyEventSchema, AbbyEventType } from "@tryabby/core";
import { Hono } from "hono";

import isbot from "isbot";
import { EventService } from "server/services/EventService";
import { RequestCache } from "server/services/RequestCache";
import { RequestService } from "server/services/RequestService";
import { checkRateLimit } from "server/common/ratelimit";
import { eventQueue } from "server/queue/queues";
import { eventQueue } from "@tryabby/queue";

export function makeEventRoute() {
const app = new Hono().post(
Expand Down
27 changes: 13 additions & 14 deletions apps/web/src/api/routes/v1_project_data.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { testClient } from "hono/testing";
import { makeProjectDataRoute } from "./v1_project_data";
import { jobManager } from "server/queue/Manager";
import { FeatureFlag, FeatureFlagValue, Option, Test } from "@prisma/client";
import { Decimal } from "@prisma/client/runtime/library";
import { afterDataRequestQueue } from "@tryabby/queue";
import { FeatureFlag, FeatureFlagValue, Option, Test } from "@tryabby/db";

vi.mock("../../env/server.mjs", () => ({
env: {},
}));

vi.mock("server/queue/Manager", () => ({
jobManager: {
emit: vi.fn().mockResolvedValue(null),
vi.mock("@tryabby/queue", () => ({
afterDataRequestQueue: {
add: vi.fn().mockResolvedValue(null),
},
}));

Expand Down Expand Up @@ -50,16 +49,16 @@ vi.mock("server/db/client", () => ({
updatedAt: new Date(),
options: [
{
chance: { toNumber: () => 0.25 } as Decimal,
chance: { toNumber: () => 0.25 } as any,
},
{
chance: { toNumber: () => 0.25 } as Decimal,
chance: { toNumber: () => 0.25 } as any,
},
{
chance: { toNumber: () => 0.25 } as Decimal,
chance: { toNumber: () => 0.25 } as any,
},
{
chance: { toNumber: () => 0.25 } as Decimal,
chance: { toNumber: () => 0.25 } as any,
},
],
},
Expand Down Expand Up @@ -116,8 +115,8 @@ describe("Get Config", () => {
expect(data.remoteConfig?.[0]?.name).toBe("First Config");
expect(data.remoteConfig?.[0]?.value).toBe(2);

expect(vi.mocked(jobManager.emit)).toHaveBeenCalledTimes(1);
expect(vi.mocked(jobManager.emit)).toHaveBeenCalledWith(
expect(vi.mocked(afterDataRequestQueue.add)).toHaveBeenCalledTimes(1);
expect(vi.mocked(afterDataRequestQueue.add)).toHaveBeenCalledWith(
"after-data-request",
expect.objectContaining({})
);
Expand All @@ -143,8 +142,8 @@ describe("Get Config Script", () => {
'"window.__abby_data__ = {\\"tests\\":[{\\"name\\":\\"First Test\\",\\"weights\\":[0.25,0.25,0.25,0.25]}],\\"flags\\":[{\\"name\\":\\"First Flag\\",\\"value\\":true}],\\"remoteConfig\\":[{\\"name\\":\\"First Config\\",\\"value\\":2}]}"'
);

expect(vi.mocked(jobManager.emit)).toHaveBeenCalledTimes(1);
expect(vi.mocked(jobManager.emit)).toHaveBeenCalledWith(
expect(vi.mocked(afterDataRequestQueue.add)).toHaveBeenCalledTimes(1);
expect(vi.mocked(afterDataRequestQueue.add)).toHaveBeenCalledWith(
"after-data-request",
expect.objectContaining({})
);
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/api/routes/v1_project_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ABBY_WINDOW_KEY, AbbyDataResponse } from "@tryabby/core";
import { z } from "zod";
import { transformFlagValue } from "lib/flags";
import { ConfigCache } from "server/common/config-cache";
import { afterDataRequestQueue } from "server/queue/queues";
import { afterDataRequestQueue } from "@tryabby/queue";

export const X_ABBY_CACHE_HEADER = "X-Abby-Cache";

Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/AddFeatureFlagModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FeatureFlagType } from "@prisma/client";
import { FeatureFlagType } from "@tryabby/db";
import { TRPCClientError } from "@trpc/client";
import { TRPC_ERROR_CODES_BY_KEY } from "@trpc/server/rpc";
import { getFlagTypeClassName, transformDBFlagTypeToclient } from "lib/flags";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/FeatureFlag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import dayjs from "dayjs";
import { FaHistory } from "react-icons/fa";
import { match, P } from "ts-pattern";

import { FeatureFlagHistory, FeatureFlagType } from "@prisma/client";
import { FeatureFlagHistory, FeatureFlagType } from "@tryabby/db";
import { Tooltip, TooltipContent, TooltipTrigger } from "components/Tooltip";
import relativeTime from "dayjs/plugin/relativeTime";
import { useState } from "react";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/FlagIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FeatureFlagType } from "@prisma/client";
import { FeatureFlagType } from "@tryabby/db";
import {
Baseline,
Hash,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/FlagPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FeatureFlagType } from "@prisma/client";
import { FeatureFlagType } from "@tryabby/db";
import { InferQueryResult } from "@trpc/react-query/dist/utils/inferReactQueryProcedure";
import { AddFeatureFlagModal } from "components/AddFeatureFlagModal";
import { CreateEnvironmentModal } from "components/CreateEnvironmentModal";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/RemoveUserModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from "@prisma/client";
import { User } from "@tryabby/db";
import { useProjectId } from "lib/hooks/useProjectId";
import { toast } from "react-hot-toast";
import { trpc } from "utils/trpc";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Test/Metrics.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prisma, Event } from "@prisma/client";
import { Prisma, Event } from "@tryabby/db";
import {
Chart as ChartJS,
BarElement,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Test/Section.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event, Test } from "@prisma/client";
import { Event, Test } from "@tryabby/db";
import { ReactNode, useId, useState } from "react";
import { AbbyEventType } from "@tryabby/core";
import { Serves } from "./Serves";
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/Test/Serves.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from "@prisma/client";
import { Event } from "@tryabby/db";
import {
Chart as ChartJS,
BarElement,
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/env/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const serverSchema = z.object({
GOOGLE_CLIENT_ID: z.string().optional(),
GOOGLE_CLIENT_SECRET: z.string().optional(),
HASHING_SECRET: z.string().min(1),
USE_PLANETSCALE: z.enum(["true", "false"]).default("false"),
});

/**
Expand Down
21 changes: 0 additions & 21 deletions apps/web/src/instrumentation.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/web/src/lib/flags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FeatureFlag, FeatureFlagType } from "@prisma/client";
import { FeatureFlag, FeatureFlagType } from "@tryabby/db";
import {
assertUnreachable,
DEFAULT_FEATURE_FLAG_VALUE,
Expand Down
Loading

0 comments on commit 53c10c5

Please sign in to comment.