Code Mentor is an AI-Powered Interactive coding playground that integrates LLM capabilities to provide practice, assessment, provide contextual help, and feedback for coding skills.
Before you begin, ensure you have met the following requirements:
- You have installed Node.js (v18.17.0 or higher).
- You have a Git client installed (v2.30.0 or higher).
- You have npm installed (v10.8.1 or higher).
- you have wrangler installed (v10.7.0 or higher)
git clone https://github.com/kassi-bertrand/code-mentor.gitTo run the frontend, enter the frontend folder, then install the packages with:
cd frontend
npm installThose steps are important to be able to test the Sign-In/Sign-Up functionalities.
-
For this, head over to Clerk, and create an account.
-
Create a new application, give it the name you want πββοΈπ.
-
Ensure that only
Emailandusernameare activated. You're selecting the option that will be made available to the user, when they try to authenticate on the website. On the side, you can immediately the form they'll be presented. -
Tap the
Create Applicationbutton. -
You'll be given two API keys
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEYandCLERK_SECRET_KEY. Copy their values into your.envfile (Create one if not already). For reference, an example environment fileenv.exampleis provided in thefrontendfolder.
Our backend consists of Cloudflare Workers. Each Worker has a different responsibility.
Read the documentation:
- Cloudflare Workers to learn about
Workersand how to create/use one yourself. - D1 database to learn how to setup a
D1database, and interact with it using a CloudflareWorker.
The steps described below are derived from this documentation βοΈ.
The Database Worker is responsible for handling database requests. It exposes different API routes and interact with a D1 database.
cd backend/database
npm installIn the backend/database folder run:
npm run generateIt generate the schema to be used when creating the database a couple of steps later. After running this command, you should see a new .sql file appear in the drizzle folder. You can open the file and see what's inside! It's just raw SQL commands. The file describes the schema of the database that will created later. πββοΈ
Inside your backend/database folder, create a new D1 database with the following command:
npx wrangler d1 create <database-name>This command will output something like this:
β
Successfully create DB '<database-name>'
[[d1_databases]]
binding = "DB"
database_name = "<database-name>"
database_id = "<unique-ID-for-your-database>"This output is the binding configuration needed in the next step. Keep it.
wrangler.toml is the configuration file used to customize the development and deployment setup for a Cloudflare Worker. The Database Worker is no exception. If you're following this tutorial for the first time, you do not have a wrangler.toml file, so a wrangler.example.toml is provided as an example. You can rename this file, or copy its content to a file named wrangler.toml.
Using the output of the previous step, assign database_name and database_id their corresponding values.
With the wrangler.toml configured properly, we can initialize the database to run and test locally, first. Bootstrap your new D1 database by running:
npx wrangler d1 execute <database-name> --local --file=<path-to-the-sql-file>npx wrangler devAfter deploying the Worker locally, the command will give you a URL (most likely localhost:8787) where your Worker is running. Assign the provided Worker URL to the NEXT_PUBLIC_DATABASE_WORKER_URL variable located in your frontend/.env file.
Choose a super duper secret password, and assign it to the NEXT_PUBLIC_WORKERS_KEY variable in the frontend/.env file. Assign the very same password to the AUTH_KEY variable located inside your backend/database/wrangler.toml.
The Storage Worker is a serverless function that exposes an interface for interacting with large amounts of unstructured data. Behind the scenes, this Worker rests on Cloudflare's R2 service, which is an AWS S3-compatible bucket.
The setup process of this worker is similar to the Database Worker. The process described here follows the tutorial on How to Use R2 from Workers.
Assuming you're at the root of the project, install the Worker's dependencies with:
cd backend/storage
npm installBefore proceeding, create a local R2 bucket if you haven't already. Assuming you're in the backend/storage folder issue the following command:
npx wrangler r2 bucket create <YOUR_BUCKET_NAME>For this tutorial use, use the name code-mentor-bucket.
To ensure your bucket was created, do:
npx wrangler r2 bucket listAssuming you're in the backend/storage folder, create a new file called wrangler.toml. There is already an example inside the wrangler.example.toml file, copy its content over to your new wrangler.toml file.
In your wrangler.toml, provide the values for the following variables
[[r2_buckets]]
binding = 'R2'
bucket_name = 'code-mentor-bucket'
# AUTH_KEY is the same value as in the Database Worker.
[vars]
AUTH_KEY = ''To deploy your Worker locally, run:
npx wrangler devNow, just like the Database Worker, this Worker will also be deployed.
Server is a NodeJS application which acts as a websocket server for the project.
Install dependencies with:
npm installrun the server with:
npx ts-node src/index.tsAfter that, visit file like:
frontend/.envbackend/database/.wrangler.tomlbackend/storage/.wrangler.tomlbackend/server/.env
Ensure that all the variables have their values assigned.
Launch the development server:
npm run devThis command will give you a URL (most likely localhost:3000) where CodeMentor is running. Copy and paste this URL in your web browser. You can try to register/login on the platform.
.
βββ backend
β βββ database
β βββ server
β βββ storage
βββ frontend
βββ app
βββ assets
βββ components
βββ lib
βββ public| Path | Description |
|---|---|
frontend |
The Next.js application for the frontend. |
backend/database |
API for interfacing with the D1 database (SQLite). |
Before proceeding, ensure that you're on the main branch, and have the latest changes with:
git checkout main
git pull origin mainThe first command will switch you to the main branch if you're not already on it. If you're already on the main branch, it will simply tell you that you're already there. The second updates your local repository with the latest changes from the remote repository. It fetches the latest changes from the main branch on GitHub and merges them into the local branch you currently on.
To implement your feature, bug fix, or documentation update, create a new branch and switch to it. When naming your branch, use a descriptive name that indicate the purpose of the changes. In this example, let's create a branch called feature/short-description and switch to it.
# Create a new branch and switch to the newly created branch
git checkout -b feature/short-descriptionExamples of Branch Names:
-
feature/add-login-page -
fix/issue-42-user-auth -
docs/update-readme -
refactor/code-cleanup
At this point, you're no longer on the main branch, but on the feature/short-description branch.
Edit the files and make your changes to implement your feature, bug fix, or documentation update, etc. Follow any existing coding standards and guidelines for the project.
Before committing, ensure your commit message follows the Conventional Commits format
Ensure that your commit messages follow the format: category(scope or module): message. The categories and their purposes are:
-
featorfeature: Introducing new features or code. -
fix: Fixing bugs (referencing an issue if applicable). -
refactor: Refactoring code that isnβt a feature or a fix. -
docs: Changes to documentation files. -
chore: Other changes that don't fit the above categories. -
If not listed, come up with a short but descriptive category name.
Here is an example to perform one commit on your branch:
# Add changes to the staging area
git add <files-you-modified>
# Example commit with a meaningful message following following format.
git commit -m "feat(login): add user authentication"Note: If you're making changes in more than two files, you do not want all those changes to be in just one commit. Instead, spread your changes across multiple commits with meaningful commit messages so that it's easy to follow your steps to implement/fix/update something.
So, far all the work you've conducted happened on your local machine. So, all the changes you made are only visible to you and are not present on GitHub. In this step, you're pushing your branch with your changes to GitHub.
Push your local branch to the repository on GitHub with:
# Push your branch to the main repository
git push origin feature/short-descriptionNOTE: Make sure you can run the code with your changes, before submitting a pull request.
After the previous step, you should see the branch you created on computer also appear our github. Now, you can create a new pull request from the branch you created (i.e. feature/short-description) into the main branch.
-
Go to the repository on GitHub.
-
Navigate to the "Pull Requests" tab.
-
Click on "New Pull Request".
-
Select your branch and provide a detailed description of the changes.
Ensure your pull request includes:
-
A clear and concise title.
-
A description of what the changes do.
-
Links to any relevant issues (if applicable).
-
Mention Kassi as reviewer to your pull request.
Once your pull request is submitted, team members will review your changes, and provide feedback. Be sure to check for comments and suggestions, and make any necessary updates to your branch.
Commands for updating the branch are:
# Pull the latest changes from the main branch, first
git pull origin main
# Merge the main branch into your feature branch
git checkout feature/short-description
git merge main
# Resolve any conflicts, add, and commit the changes
git add .
git commit -m "fix: resolve merge conflicts"
# Push the updated branch
git push origin feature/short-descriptionAfter your pull request is approved, it can be merged into the main branch. A team member with the necessary permissions will handle this.
Note: Once the pull request is merged, the branch should be deleted to keep the repository clean.
After your changes are merged, you should delete the local branch to avoid clutter.
# Switch to the main branch
git checkout main
# Pull the latest changes
git pull origin main
# Delete the branch locally
git branch -d feature/short-description
# Delete the branch from the main respository on GitHub
git push origin --delete feature/short-description