GithubHelp home page GithubHelp logo

hex-spell / snake-backend Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 78 KB

Kotlin Snake game high-scores microservice, with caching and rate limitiing

Kotlin 98.71% Dockerfile 1.29%
kotlin rest-api rest mysql sql rate-limiting caching redis snake-game

snake-backend's Introduction

Snake game high-scores

Learning Kotlin applications for backend

What it does

  • Saves user high-scores
  • Retrieves the best 10 of all time

Http api live demo

Example request:

curl --request GET \
  --url http://modularizar.com/snake-backend \
  --header 'Content-Type: application/json'

Endpoints

GET /

  • Returns array of { "id": Int, "username": String, "points": Int, "saved_at": String }

POST /

  • Receives { "username": String, "points": Int }
  • Returns new entity as { "id": Int, "username": String, "points": Int, "saved_at": String }

Environment variables (name, default value):

  • HOST: localhost
  • DB_HOST: localhost
  • DB_NAME: snake
  • DB_PORT: 3306
  • DB_USER: root
  • DB_PASSWORD: root
  • REDIS_HOST: localhost
  • REDIS_PORT: 6379

Additional features

  • Rate limiting by IP
  • cache
  • validation

Tools used:

  • Ktor for http server with routing
  • Ktorm as database ORM
  • MySQL database
  • Gson as json serializer
  • Redis with Jedis
  • Docker

Hosting

(Using docker-compose)

services:
  reverse_proxy:
      container_name: reverse_proxy
      image: nginx:1.17.10
      depends_on:
        # {...} my other services
        - snake-backend
      volumes:
        - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      ports:
        - "80:80"
  snake-backend:
      container_name: snake-backend
      build:
        context: ./snake-backend
      user: root
      depends_on:
        - mysql
        - redis
      environment:
        HOST: snake-backend
        DB_HOST: mysql
        DB_NAME: "snake"
        DB_PORT: "3306"
        DB_USER: "root"
        DB_PASSWORD: #shh
        REDIS_HOST: redis
        REDIS_PORT: "6379"
      restart: always
  
  mysql:
    image: mysql:8.0.13
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      # {...} super secret stuff, yknow
    volumes:
      - ./mysql-data:/var/lib/mysql
      - ./init:/docker-entrypoint-initdb.d
    ports:
      - "3306:3306"

  redis:
    image: "redis:alpine"

    command: redis-server

    ports:
      - "6379:6379"

    volumes:
     - ./redis-data:/var/lib/redis
     - ./redis.conf:/usr/local/etc/redis/redis.conf

Database init script

CREATE DATABASE IF NOT EXISTS `snake`;

use snake;

CREATE TABLE `players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL,
`points` int(11) NOT NULL,
`saved_at` datetime DEFAULT NOW(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

snake-backend's People

Contributors

hex-spell avatar

Stargazers

Roman avatar

Watchers

 avatar

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.