Skip to content

Commit

Permalink
refactor: move yjs out of api into an app (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihebi authored Aug 28, 2023
1 parent e1a7151 commit 5815b40
Show file tree
Hide file tree
Showing 20 changed files with 418 additions and 8,275 deletions.
11 changes: 0 additions & 11 deletions apps/api/fixup

This file was deleted.

8,209 changes: 0 additions & 8,209 deletions apps/api/package-lock.json

This file was deleted.

43 changes: 1 addition & 42 deletions apps/api/src/resolver_runtime.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
import { ApolloClient, InMemoryCache, gql } from "@apollo/client/core";

// chooes between docker and k8s spawners
import {
spawnRuntime as spawnRuntime_docker,
killRuntime as killRuntime_docker,
infoRuntime as infoRuntime_docker,
loopKillInactiveRoutes as loopKillInactiveRoutes_docker,
initRoutes as initRoutes_docker,
} from "./spawner-docker";
import {
spawnRuntime as spawnRuntime_k8s,
killRuntime as killRuntime_k8s,
infoRuntime as infoRuntime_k8s,
loopKillInactiveRoutes as loopKillInactiveRoutes_k8s,
initRoutes as initRoutes_k8s,
} from "./spawner-k8s";

const apollo_client = new ApolloClient({
cache: new InMemoryCache({}),
uri: process.env.PROXY_API_URL,
Expand Down Expand Up @@ -65,31 +49,6 @@ async function listAllRuntimes(_, {}, { userId }) {
export default {
Query: {
listAllRuntimes,

...(process.env.RUNTIME_SPAWNER === "k8s"
? {
infoRuntime: infoRuntime_k8s,
}
: {
infoRuntime: infoRuntime_docker,
}),
},
Mutation: {
...(process.env.RUNTIME_SPAWNER === "k8s"
? {
spawnRuntime: spawnRuntime_k8s,
killRuntime: killRuntime_k8s,
}
: {
spawnRuntime: spawnRuntime_docker,
killRuntime: killRuntime_docker,
}),
},
Mutation: {},
};

export const initRoutes =
process.env.RUNTIME_SPAWNER !== "k8s" ? initRoutes_docker : initRoutes_k8s;
export const loopKillInactiveRoutes =
process.env.RUNTIME_SPAWNER !== "k8s"
? loopKillInactiveRoutes_docker
: loopKillInactiveRoutes_k8s;
4 changes: 0 additions & 4 deletions apps/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {

import { typeDefs } from "./typedefs";
import { resolvers } from "./resolver";
import { initRoutes, loopKillInactiveRoutes } from "./resolver_runtime";

interface TokenInterface {
id: string;
Expand Down Expand Up @@ -51,9 +50,6 @@ async function startServer() {
await apollo.start();
apollo.applyMiddleware({ app: expapp });

await initRoutes();
loopKillInactiveRoutes();

const port = process.env.PORT || 4000;
http_server.listen({ port }, () => {
console.log(`🚀 Server ready at http://localhost:${port}`);
Expand Down
3 changes: 0 additions & 3 deletions apps/api/src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export const typeDefs = gql`
getDashboardRepos: [Repo]
activeSessions: [String]
listAllRuntimes: [RuntimeInfo]
infoRuntime(sessionId: String!): RuntimeInfo
}
type Mutation {
Expand All @@ -132,8 +131,6 @@ export const typeDefs = gql`
clearUser: Boolean
clearRepo: Boolean
clearPod: Boolean
spawnRuntime(sessionId: String!): Boolean
killRuntime(sessionId: String!): Boolean
updateVisibility(repoId: String!, isPublic: Boolean!): Boolean
addCollaborator(repoId: String!, email: String!): Boolean
deleteCollaborator(repoId: String!, collaboratorId: String!): Boolean
Expand Down
3 changes: 3 additions & 0 deletions apps/yjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
conns/conn-*.json
prisma/dev.db
61 changes: 61 additions & 0 deletions apps/yjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "yjs",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"build": "tsc",
"start": "node build/yjs-server.js",
"dev": "ts-node-dev src/yjs-server.ts",
"test": "jest --config jest.config.js"
},
"dependencies": {
"@apollo/client": "^3.7.1",
"@codepod/prisma": "workspace:*",
"@kubernetes/client-node": "^0.17.1",
"@prisma/client": "4.3.1",
"apollo-server": "^3.5.0",
"apollo-server-core": "^3.10.3",
"apollo-server-express": "3.10.2",
"bcryptjs": "^2.4.3",
"dockerode": "^3.3.1",
"google-auth-library": "^8.7.0",
"graphql": "16.6.0",
"jest": "^29.0.3",
"jsdom": "^22.1.0",
"jsonwebtoken": "^8.5.1",
"lib0": "^0.2.83",
"lodash": "^4.17.21",
"nanoid": "^3.0.0",
"nanoid-dictionary": "^4.3.0",
"prisma": "4.3.1",
"prosemirror-model": "^1.19.3",
"prosemirror-view": "^1.31.7",
"reactflow": "^11.7.4",
"stompjs": "^2.3.3",
"uuid": "^9.0.0",
"ws": "^8.2.3",
"y-prosemirror": "^1.2.1",
"y-protocols": "^1.0.5",
"yjs": "^13.6.7",
"zeromq": "^6.0.0-beta.6"
},
"devDependencies": {
"@jest/globals": "^29.6.4",
"@types/bcryptjs": "^2.4.2",
"@types/dockerode": "^3.3.11",
"@types/express": "^4.17.14",
"@types/jest": "^29.0.2",
"@types/jsdom": "^21.1.1",
"@types/jsonwebtoken": "^8.5.9",
"@types/node": "^18.11.2",
"@types/stompjs": "^2.3.5",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.3",
"copyfiles": "^2.4.1",
"nodemon": "^2.0.15",
"ts-jest": "^29.0.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.4.4",
"watch": "^1.0.2"
}
}
File renamed without changes.
29 changes: 29 additions & 0 deletions apps/yjs/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// chooes between docker and k8s spawners
import {
spawnRuntime as spawnRuntime_docker,
killRuntime as killRuntime_docker,
infoRuntime as infoRuntime_docker,
loopKillInactiveRoutes as loopKillInactiveRoutes_docker,
initRoutes as initRoutes_docker,
} from "./spawner-docker";
import {
spawnRuntime as spawnRuntime_k8s,
killRuntime as killRuntime_k8s,
infoRuntime as infoRuntime_k8s,
loopKillInactiveRoutes as loopKillInactiveRoutes_k8s,
initRoutes as initRoutes_k8s,
} from "./spawner-k8s";

export const spawnRuntime =
process.env.RUNTIME_SPAWNER === "k8s"
? spawnRuntime_k8s
: spawnRuntime_docker;
export const killRuntime =
process.env.RUNTIME_SPAWNER === "k8s" ? killRuntime_k8s : killRuntime_docker;

export const initRoutes =
process.env.RUNTIME_SPAWNER !== "k8s" ? initRoutes_docker : initRoutes_k8s;
export const loopKillInactiveRoutes =
process.env.RUNTIME_SPAWNER !== "k8s"
? loopKillInactiveRoutes_docker
: loopKillInactiveRoutes_k8s;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions apps/api/src/yjs-runtime.ts → apps/yjs/src/yjs-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Y from "yjs";
import WebSocket from "ws";

import runtimeResolver from "./resolver_runtime";
import { spawnRuntime, killRuntime } from "./runtime";

type PodResult = {
exec_count?: number;
Expand Down Expand Up @@ -216,7 +216,7 @@ export function setupObserversToRuntime(ydoc: Y.Doc, repoId: string) {
runtimeMap.get(runtimeId)
);
const sessionId = runtimeId;
const created = await runtimeResolver.Mutation.spawnRuntime(null, {
const created = await spawnRuntime(null, {
sessionId,
});
if (!created) {
Expand Down Expand Up @@ -244,7 +244,7 @@ export function setupObserversToRuntime(ydoc: Y.Doc, repoId: string) {
// kill the runtime
const socket = runtime2socket.get(runtimeId);
socket?.close();
await runtimeResolver.Mutation.killRuntime(null, {
await killRuntime(null, {
sessionId: runtimeId,
});
runtime2socket.delete(runtimeId);
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/yjs-server.ts → apps/yjs/src/yjs-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { getYDoc, setupWSConnection } from "./yjs-setupWS";
import prisma from "@codepod/prisma";
import { connectSocket, runtime2socket } from "./yjs-runtime";

import { initRoutes, loopKillInactiveRoutes } from "./runtime";

interface TokenInterface {
id: string;
}
Expand Down Expand Up @@ -102,6 +104,9 @@ async function startServer() {
return;
});

await initRoutes();
loopKillInactiveRoutes();

const port = process.env.PORT || 4233;
http_server.listen({ port }, () => {
console.log(`🚀 Server ready at http://localhost:${port}`);
Expand Down
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions apps/yjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"module": "commonjs",
"outDir": "./build",
"target": "esnext",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"lib": ["esnext"],
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"isolatedModules": true,
"noImplicitAny": false
},
"ts-node": {
// these options are overrides used only by ts-node
// same as the --compilerOptions flag and the TS_NODE_COMPILER_OPTIONS environment variable
"compilerOptions": {
"module": "commonjs"
}
},
"include": ["src"]
}
6 changes: 3 additions & 3 deletions compose/dev/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ services:
image: node:18
restart: always
command: sh -c "corepack enable && pnpm dlx prisma studio"
working_dir: /codepod/apps/api
working_dir: /codepod/packages/prisma
volumes:
- ../..:/codepod
environment:
Expand Down Expand Up @@ -77,12 +77,12 @@ services:

yjs-server:
image: node:18
working_dir: /codepod/apps/api
working_dir: /codepod/apps/yjs
volumes:
- ../..:/codepod
- pnpm-store:/codepod/.pnpm-store
- /var/run/docker.sock:/var/run/docker.sock
command: sh -c "corepack enable && pnpm dev:yjs"
command: sh -c "corepack enable && pnpm dev"
environment:
DATABASE_URL: "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?schema=public"
JWT_SECRET: ${JWT_SECRET}
Expand Down
Loading

0 comments on commit 5815b40

Please sign in to comment.