Code Monkey home page Code Monkey logo

go-leetcode's Introduction

go-leetcode

Check out the working demo here!

A clone of Leetcode written in Go and SvelteKit. I wanted to see if I could reverse-engineer a popular web app with complicated internals. The goal of this side project was to learn how to use Go for backend development, SvelteKit and TailwindCSS for scaffolding the frontend, learn a new database, and understand how Docker and Docker Compose works.

Languages and Tools Used:

  • Go
  • SvelteKit
  • TailwindCSS
  • PostgreSQL
  • Docker, Docker Compose

Authentication Workflow

Services Description

  • There are four services: frontend, backend, coderunner, and database.

    • All services are served inside their own Docker containers. These containers are spun up using Docker Compose.
  • frontend service renders these pages:

    • Index page to welcome users.
    • Sign Up / Login page for user authentication.
    • Problems page to list and filter available problems.
    • Problem page that shows the problem description, solution, discussion and submissions, along with an online editor where users can write their code.
  • backend service is an API server that handles user requests from the frontend and then responds with a corresponding JSON.

    • main.go handles incoming requests and relays them to the corresponding controller function in controllers.go. It is also responsible for loading the .env file and connecting to the database.
    • auth.go holds the functions necessary for user authentication, which includes handling CORS requests, generating JWTs, handling user sign ins and logins, and handling silent refreshes.
    • controllers.go holds all the controller functions.
    • middlewares.go holds the middlewares that wraps certain controller functions.
    • models.go holds structs that mirror the database schema.
  • coderunner service is responsible for running user code and testing it against numerous test cases.

    • It is separated as its own service for SoC (separation of concern) purposes.
    • main.go handles incoming requests and relays them to the corresponding controller function in controllers.go. It is also responsible for connecting to the database.
    • controllers.go holds all the controller functions.
    • /cpp, /java, /js, /py are directories that hold the user code written in those specific langauges. It also holds the test results. The user code file and test result file share the same UUID, which the service uses to differentiate between different users' code.
    • lang holds code for language support. This mainly includes methods responsible for generating and compiling user code files.
    • models.go holds structs that mirror the database schema.
    • utils.go holds utility functions.
  • database service holds the PostgreSQL database that stores all the data for the web app.

App Workflow

  • User writes a code within the online editor provided by the frontend.
  • The code is then sent to the backend once the user presses the submit button. The data is transferred via REST API.
  • The API gateway receives the code and checks what problem the user is solving. Then it queries the database for the appropriate template code and testcases.
  • The backend then hands it over to a separate "code-run" container which provides an isolated environment for code to be run in.
  • The code-run container generates files with the user code and template code inside it, then compiles/runs it to compare outputs with the testcase.
    • It handles concurrent runs by generating unique file names using Google's uuid package.
  • Once the run completes, the output will either be a compile error, runtime error, or a successful run. The output is sent back to the API gateway.
  • The result is marshalled into JSON and is sent back to the frontend, which renders it as a Javascript alert.
  • User authentication is a token-based auth system using JWT.

go-leetcode's People

Contributors

jpoly1219 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

go-leetcode's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.