CodeGuessr is a web application that tests your programming language knowledge. It provides code snippets of varying difficulty and programming languages, and uses AI to validate your answers.
- Random Code Snippets: Request a random code snippet based on your desired programming language and difficulty level.
- AI-Powered Validation: Submit your answer and let the AI determine if it's correct.
- Reveal Answer: Don't know the answer? You can request the solution directly.
- User Authentication: Securely sign up and sign in to track your progress.
- Go: The core programming language for the API.
- PostgreSQL: The primary database for storing user data and tasks.
- Redis: Used for caching and session management.
- Docker: For containerizing and running the application.
- Google Generative AI: For AI-powered answer validation.
- Swagger: For API documentation.
- Docker
- Docker Compose
- Go
- Make
- Gemini API Key
The CodeGuessr API uses the Google Generative AI for AI-powered answer validation. To run the application, you need to set up the following environment variables:
GEMINI_MODEL: Specifies the Gemini model to use (e.g.,gemini-pro).GEMINI_API_KEY: Your API key for accessing the Google Generative AI.
How to obtain a Gemini API Key:
- Go to the Google AI Studio to create an API key.
- Follow the instructions to generate a new API key.
How to get Google SMTP Server Credentials (Gmail):
Google's SMTP server can be used with your Gmail account. However, due to Google's security measures, you might need to take specific steps:
-
Enable 2-Step Verification (2FA) for your Google Account:
- Go to your Google Account Security page.
- Under "How you sign in to Google," find "2-Step Verification" and turn it on if it's not already. This is a prerequisite for generating App Passwords.
-
Generate an App Password:
- Once 2-Step Verification is enabled, return to the Google Account Security page.
- Under "How you sign in to Google," find "App passwords."
- Click "Generate new app password" (you might need to select "Mail" and "Other (Custom name)" for the device).
- Google will generate a 16-character password. This is the password you should use for
SMTP_SERVER_PASSWORD. - Use your gmail as
SMTP_SERVER_USERNAME - Do NOT use your regular Google account password for
SMTP_SERVER_PASSWORD, especially if 2-Step Verification is enabled. App Passwords are designed for this purpose and are more secure.
Setting up Environment Variables Locally:
- Create a file named
.envin the root directory of the project. - Copy the contents from the
.env.examplefile into your newly created.envfile. - Replace empty variables with your actual data you've obtained in the previous steps.
-
Clone the repository:
git clone https://github.com/olxandr/CodeGuessr.git cd CodeGuessr -
Build the Docker containers:
make build
-
Start the application:
make up
The API will be running at http://localhost:4335.
API documentation is generated using Swagger. To view the documentation, start the application and navigate to http://localhost:4335/swagger/index.html.
To generate or update the Swagger documentation, run:
make swagThe Makefile provides several commands to manage the application:
| Command | Description |
|---|---|
make up |
Start the application in detached mode. |
make down |
Stop the application and remove the containers. |
make build |
Build the Docker containers. |
make logs |
View the logs from the running containers. |
make restart |
Restart the application. |
make clean |
Stop and remove containers, networks, images, and volumes. |
make swag |
Generate or update the Swagger documentation. |
The project follows a standard Go project layout:
cmd/api: Contains the main application entry point, server setup, and API handlers.cmd/migration: Contains the database migration tool.internal: Contains the core business logic of the application, separated into different packages:ai: Handles the interaction with the Google Generative AI API.cache: Implements caching using Redis.model: Defines the data structures used throughout the application.repository: Manages the interaction with the database.service: Contains the business logic for different services (e.g., authentication, guessing).
go.mod: Lists the project's dependencies.Makefile: Contains the commands for managing the application.docker-compose.yml: Defines the services, networks, and volumes for the Docker application.Dockerfile: Defines the environment and dependencies for the Go application.
