Skip to content

Commit

Permalink
feat: add queue and clean up (#286)
Browse files Browse the repository at this point in the history
* feat: add queue and clean up

* feat: add chapters generation and merge backend app with frontend

* chore: add chapter generation
  • Loading branch information
younes200 authored Oct 12, 2024
1 parent d86ec08 commit 0c5e776
Showing 83 changed files with 12,129 additions and 11,725 deletions.
1 change: 0 additions & 1 deletion .env.ci
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NODE_ENV=test

CI_TEST=true
API_URL=http://localhost:2021/trpc

# Cookie secret key. Generate something random and long.
DATABASE_URL=postgres://postgres:postgres@postgres:5432/celluloid
4 changes: 0 additions & 4 deletions .git-crypt/.gitattributes

This file was deleted.

Binary file not shown.
13 changes: 4 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
name: Docker Build
on:
workflow_dispatch:
inputs:
tag:
description: "Docker tag"
required: true
default: "latest"
push:
branches: [staging]

jobs:
build-containers:
@@ -32,12 +28,11 @@ jobs:
- name: Build
uses: docker/build-push-action@v3
with:
file: Dockerfile.compact
file: ./apps/frontend/Dockerfile
context: .
push: true
tags: ghcr.io/${{ github.repository }}/compact:${{ github.event.inputs.tag }}
tags: ghcr.io/${{ github.repository }}/celluloid:staging
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
COMMIT=${{ steps.vars.outputs.sha_short }}
API_URL=/trpc
14 changes: 4 additions & 10 deletions .github/workflows/test-ci.yml
Original file line number Diff line number Diff line change
@@ -2,9 +2,7 @@ name: E2E Tests

on:
push:
branches: [main]
pull_request:
branches: [main]
branches: [staging]
workflow_dispatch:

jobs:
@@ -20,10 +18,7 @@ jobs:
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
--health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
postgres:
image: postgres:14
env:
@@ -39,12 +34,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v2


- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
node-version: 20
cache: "yarn"
- run: yarn install --frozen-lockfile

- name: Build apps
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -49,3 +49,5 @@ node_modules/
/playwright/.cache/


.cache
sandbox
Binary file modified .yarn/install-state.gz
Binary file not shown.
541 changes: 0 additions & 541 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

This file was deleted.

28 changes: 0 additions & 28 deletions .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs

This file was deleted.

873 changes: 0 additions & 873 deletions .yarn/releases/yarn-3.4.1.cjs

This file was deleted.

10 changes: 2 additions & 8 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
compressionLevel: mixed

enableGlobalCache: true

enableTelemetry: false
@@ -13,11 +15,3 @@ logFilters:
level: discard

nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"

yarnPath: .yarn/releases/yarn-3.4.1.cjs
15 changes: 8 additions & 7 deletions apps/admin/package.json
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
"node": ">=16"
},
"scripts": {
"dev": "dotenv -e ../../.env -- tsup --watch --silent --onSuccess 'node dist/index.js'",
"dev-admin": "dotenv -e ../../.env -- tsup --watch --silent --onSuccess 'node dist/index.js'",
"build": "tsup",
"start": "node dist"
},
@@ -18,25 +18,26 @@
"@adminjs/import-export": "^3.0.0",
"@adminjs/prisma": "^5.0.1",
"@adminjs/themes": "^1.0.1",
"@celluloid/passport": "*",
"@celluloid/prisma": "*",
"@celluloid/utils": "*",
"@celluloid/passport": "workspace:*",
"@celluloid/prisma": "workspace:*",
"@celluloid/utils": "workspace:*",
"@tiptap/extension-text-style": "^2.8.0",
"adminjs": "^7.2.2",
"cors": "^2.8.5",
"express": "^4.19.2",
"express-formidable": "^1.2.0",
"express-session": "^1.17.3"
},
"devDependencies": {
"@celluloid/config": "*",
"@celluloid/config": "workspace:*",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/node": "^18.14.2",
"copyfiles": "^2.4.1",
"del-cli": "^5.0.0",
"dotenv-cli": "^7.3.0",
"np": "^7.7.0",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
"tsup": "^8.3.0",
"typescript": "^5.6.2"
}
}
10 changes: 0 additions & 10 deletions apps/admin/src/components.ts

This file was deleted.

47 changes: 22 additions & 25 deletions apps/admin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { createSession, passport } from '@celluloid/passport';
import { UserRole } from '@celluloid/prisma';
import { createSession, passport } from "@celluloid/passport";
import { UserRole } from "@celluloid/prisma";
import cors from "cors";
import express, { NextFunction, Request, Response } from "express";
import express, {
type NextFunction,
type Request,
type Response,
} from "express";
import path from "path";
import * as url from 'url'
import * as url from "url";

import getAdminRouter from "./server.js";


const PORT = process.env.PORT || 4000

const PORT = process.env.PORT || 4000;

const start = async () => {
const app = express();
app.enable('trust proxy');
app.enable("trust proxy");
app.use(cors({ credentials: true, origin: true }));
app.use(createSession());
app.use(passport.authenticate("session"));

// Define the CORS middleware function
const corsMiddleware = (req: Request, res: Response, next: NextFunction) => {
// Set the CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
// Call the next middleware function in the chain
next();
};
@@ -32,28 +34,23 @@ const start = async () => {
app.use(corsMiddleware);

const adminRouter = await getAdminRouter({
rootPath: "/admin"
rootPath: "/admin",
});

const isAuthenticated = function (req, res, next) {
if (req.user && req.user.role == UserRole.Admin)
return next();
res.redirect('/')
}

const isAuthenticated = (req, res, next) => {
if (req.user && req.user.role === UserRole.Admin) return next();
res.redirect("/");
};

app.use('/admin', isAuthenticated, adminRouter);
app.use("/admin", isAuthenticated, adminRouter);

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

app.use("/admin", express.static(path.join(__dirname, "../public")));

app.listen(PORT, () => {
console.log(
`AdminJS started on localhost:${PORT}`
);
console.log(`AdminJS started on localhost:${PORT}`);
});
};

start()

start();
6 changes: 0 additions & 6 deletions apps/admin/src/my-input.tsx

This file was deleted.

41 changes: 17 additions & 24 deletions apps/admin/src/passport.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import { prisma, type User, UserRole } from "@celluloid/prisma";
import bcrypt from "bcryptjs";
import passport from "passport";
import { Strategy as LocalStrategy } from "passport-local";

import { prisma, User, UserRole } from "@celluloid/prisma"
import bcrypt from 'bcryptjs';
import passport from 'passport';
import {
Strategy as LocalStrategy,
} from "passport-local";

export enum SigninStrategy {
LOGIN = "login",
TEACHER_SIGNUP = "teacher-signup",
STUDENT_SIGNUP = "student-signup",
}


passport.serializeUser((user: User, done) => {
done(null, user.id)
done(null, user.id);
});

passport.deserializeUser(async (id: string, done) => {
const user = await prisma.user.findUnique({ where: { id } })
const user = await prisma.user.findUnique({ where: { id } });
if (user) {
return done(null, user);
} else {
console.error(
`Deserialize user failed: user with id` + ` ${id} does not exist`
`Deserialize user failed: user with id` + ` ${id} does not exist`,
);
return done(new Error("InvalidUser"));
}
});

passport.use(
new LocalStrategy(async (username: string, password: string, done) => {
const user = await prisma.user.findUnique({ where: { username: username } })
const user = await prisma.user.findUnique({
where: { username: username },
});
if (!user) {
return done(new Error("InvalidUser"));
}
@@ -42,36 +36,35 @@ passport.use(
return done(new Error("UserNotConfirmed"));
}
return done(null, user);

}),
);


const loginStrategy = new LocalStrategy(
{ usernameField: "login" },
async (login, password, done) => {

const user = await prisma.user.findUnique({
where: {
OR: [{ email: login }, { username: login, }]
}
OR: [{ email: login }, { username: login }],
},
});

if (!user) {
return Promise.resolve(done(new Error("InvalidUser")));
}
if (!bcrypt.compareSync(password, user.password)) {
console.error(`Login failed for user ${user.username}: incorrect password`);
console.error(
`Login failed for user ${user.username}: incorrect password`,
);
return Promise.resolve(done(new Error("InvalidUser")));
}
if (!user.confirmed && user.role !== UserRole.Student) {
console.error(`Login failed: ${user.username} is not confirmed`);
return Promise.resolve(done(new Error("UserNotConfirmed")));
}
return Promise.resolve(done(null, user));
}
},
);

passport.use(SigninStrategy.LOGIN, loginStrategy);
passport.use("login", loginStrategy);

export default passport;
17 changes: 3 additions & 14 deletions apps/admin/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import AdminJSExpress from "@adminjs/express";
import importExportFeature from "@adminjs/import-export";
import { Database, getModelByName, Resource } from '@adminjs/prisma';
import { dark, light, noSidebar } from '@adminjs/themes'
import PrismaModule, { prisma } from '@celluloid/prisma';
import AdminJS, { AdminJSOptions, DEFAULT_PATHS, ThemeConfig } from "adminjs";
import AdminJS, { type AdminJSOptions, type ThemeConfig } from "adminjs";

import { componentLoader, Components } from './components.js'

export const overrides: ThemeConfig['overrides'] = {
colors: {
@@ -60,12 +58,8 @@ const getAdminRouter = (options: Partial<AdminJSOptions> = {}) => {
assets: {
styles: ['/admin/assets/styles/override.css'],
},
dashboard: {
component: Components.MyInput,
},
defaultTheme: noSidebar.id,
availableThemes: [dark, light, noSidebar],
componentLoader,
resources: [
{
resource: { model: getModelByName('User', PrismaModule), client: prisma },
@@ -158,12 +152,7 @@ const getAdminRouter = (options: Partial<AdminJSOptions> = {}) => {
},
},
},
},
features: [
importExportFeature({
componentLoader,
}),
],
}
},
{
resource: { model: getModelByName('Comment', PrismaModule), client: prisma },
@@ -194,7 +183,7 @@ const getAdminRouter = (options: Partial<AdminJSOptions> = {}) => {


const admin = new AdminJS(adminOptions);
if (process.env.NODE_ENV == "developement")
if (process.env.NODE_ENV === "developement")
admin.watch()
return AdminJSExpress.buildRouter(admin);
};
Loading

0 comments on commit 0c5e776

Please sign in to comment.