Skip to content

A REST API that provides coding challenges of chosen language and difficulty level, prompting users to explain the code before revealing correct answers.

License

Notifications You must be signed in to change notification settings

olxandr/CodeGuessr

Repository files navigation

CodeGuessr API

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.

CodeGuessr Demo

Features

  • 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.

Tech Stack

  • 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.

Getting Started

Prerequisites

Environment Variables

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:

  1. Go to the Google AI Studio to create an API key.
  2. 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:

  1. 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.
  2. 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:

  1. Create a file named .env in the root directory of the project.
  2. Copy the contents from the .env.example file into your newly created .env file.
  3. Replace empty variables with your actual data you've obtained in the previous steps.

Installation

  1. Clone the repository:

    git clone https://github.com/olxandr/CodeGuessr.git
    cd CodeGuessr
  2. Build the Docker containers:

    make build
  3. Start the application:

    make up

The API will be running at http://localhost:4335.

API Documentation

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 swag

Available Commands

The 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.

Project Structure

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.

About

A REST API that provides coding challenges of chosen language and difficulty level, prompting users to explain the code before revealing correct answers.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages