Skip to content
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
136 changes: 85 additions & 51 deletions client/package-lock.json

Large diffs are not rendered by default.

19 changes: 8 additions & 11 deletions client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
server: {
port: 3000,
open: true,
proxy: {
'/api': {
target: 'http://localhost:3001', // Your backend port
'/graphql': {
target: 'http://localhost:3001/',
changeOrigin: true,
secure: false,
},
},
},
});
})
2 changes: 1 addition & 1 deletion server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"main": "index.js",
"scripts": {
"start": "node dist/server.js",
"dev": "nodemon --watch src --exec ts-node src/server.ts",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon index.ts",
"watch": "nodemon dist/server.js"
},
"keywords": [],
Expand All @@ -20,7 +20,7 @@
"cors": "^2.8.5",
"dotenv": "^16.5.0",
"express": "^5.1.0",
"graphql": "^16.10.0",
"graphql": "^16.11.0",
"jsonwebtoken": "^9.0.2",
"multer": "^1.4.5-lts.2",
"openai": "^4.96.0"
Expand Down
29 changes: 0 additions & 29 deletions server/seed.ts

This file was deleted.

1 change: 1 addition & 0 deletions server/src/config/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ const db = async (): Promise<typeof mongoose.connection> => {
};

export default db;

8 changes: 8 additions & 0 deletions server/src/schemas/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ const typeDefs = gql`
choices: [String!]!
answer: String!
}
type Character {
_id: ID
name: String!
picture: String!
voice: String!
}

type Query {
users: [User]
user(username: String!): User
me: User
generateQuestion(track: String!, level: String!, minion: String!): Question
characters: [Character]!

}

type Mutation {
Expand Down
101 changes: 45 additions & 56 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,54 @@
import express, { type Request, type Response } from 'express';

import express, { Application, Request, Response } from 'express';
import dotenv from 'dotenv';
import { OpenAI } from 'openai';

import path from 'path';
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import typeDefs from './schemas/typeDefs';
import resolvers from './schemas/resolvers';
import { authenticateToken } from './utils/auth';


// Extend the Request interface to include the user property
declare global {
namespace Express {
interface Request {
user?: any; // Replace 'any' with the appropriate type for your user object
}
}
}
// import { expressMiddleware } from '@apollo/server/express4';
import { typeDefs, resolvers } from './schemas/index';
import db from './config/connections';
// import { authenticateToken } from './utils/auth.js';

dotenv.config();

const app = express();
const PORT = process.env.PORT || 3001;
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });
const app: Application = express();
const PORT = process.env.PORT || 3001;
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY! });

// Create a new instance of an Apollo server with the GraphQL schema
const startApolloServer = async () => {
const server = new ApolloServer({
typeDefs,
resolvers,
});
const server = new ApolloServer({
typeDefs,
resolvers,
});

const startApolloServer = async () => {
await server.start();
await db;
const startApolloServer = async () => {
await server.start();
await db();

app.use(express.static('public')); // serve generated mp3 file
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

app.use('/graphql', expressMiddleware(server as any,
{
context: authenticateToken as any
}
));
// if we're in production, serve client/build as static assets
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '../client/dist')));

app.get('*', (_req: Request, res: Response) => {
res.sendFile(path.join(__dirname, '../client/dist/index.html'));
});
}
app.use(express.urlencoded({ extended: false }));
app.use(express.json());

// Start the server
app.listen(PORT, () => {
console.log(`✅ Server is running on http://localhost:${PORT}`);
console.log(`✅ GraphQL endpoint is available at http://localhost:${PORT}/graphql`);
});
};
// Apollo Server v4 middleware with correct typing
const authenticateToken = async ({ req }: { req: Request }) => {
const token = (req.headers.authorization as string | undefined)?.split(' ')[1];
let user = null;

if (token) {
try {
const { verifyToken } = require('./utils/auth');
user = verifyToken(token);
} catch (err) {
console.error('Token verification failed:', err);
}
}
// Serve static files in production
app.use(express.static(path.join(__dirname, '../client/dist')));
app.get('*', (_req: Request, res: Response) => {
res.sendFile(path.join(__dirname, '../client/dist/index.html'));
});
}

// TTS Route for Dr. Dan
// POST /api/tts - OpenAI text-to-speech
app.post('/api/tts', async (req: Request, res: Response) => {
const { text } = req.body;

try {
const speech = await openai.audio.speech.create({
model: 'tts-1',
Expand All @@ -83,8 +66,14 @@ dotenv.config();
});

// Root route
app.get('/', (req: Request, res: Response) => {
app.get('/', (_req: Request, res: Response) => {
res.send('🎙️ Codezilla server is up!');
});

startApolloServer();
app.listen(PORT, () => {
console.log(`✅ Server is running on http://localhost:${PORT}`);
console.log(`✅ GraphQL endpoint is available at http://localhost:${PORT}/graphql`);
});
};

startApolloServer();
2 changes: 1 addition & 1 deletion server/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const authMiddleware = ({ req }: { req: any }) => {
};

export const authenticateToken = async ({ req }: { req: Request }) => {
const authHeader = req.headers.authorization;
const authHeader = req.headers.get('authorization');
let user = null;
console.log('AUTH HEADER', authHeader);

Expand Down
Loading