Skip to content

Commit

Permalink
chore: 🎉 Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaVls committed Jun 2, 2021
0 parents commit c76e2a2
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
.env
.flaskenv
dist/
build/
.cache/
.pytest_cache/
.vscode
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Typefight
Typefight is a typing game where you get a random sentence, type it in, and see your score, as well as other people's score.

This app comes from my desire to learn about databases and how to interact with them. In this case, I'm using python's Flask framework for the backend.

## Features
The game randomly assigns the player a short sentence, which they must type (including characters like spaces, periods, commas, etc.). After which, the player gets the time it took them to type in that sentence, and how long it takes other players who've typed the same sentence.

## Tech stack
- HTML, CSS & Javascript
- Docker containers
- Postgresql 13
- Python 3.8
- Flask
- psycopg2
9 changes: 9 additions & 0 deletions database/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM postgres:13-alpine

# Setting timezone to Los Angeles
RUN apk add -U tzdata
RUN ["cp", "/usr/share/zoneinfo/America/Los_Angeles", "/etc/localtime"]

# Initializing database extensions and tables
COPY create-extension.sh /docker-entrypoint-initdb.d/
COPY initdb.sql /docker-entrypoint-initdb.d/
4 changes: 4 additions & 0 deletions database/create-extension.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" "$POSTGRES_DB" << EOF
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT * FROM pg_extension;
EOF
11 changes: 11 additions & 0 deletions database/initdb.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS highscores(
highscore_uid UUID PRIMARY KEY,
score NUMERIC(6, 2) NOT NULL CHECK (score > 0)
);

CREATE TABLE IF NOT EXISTS players(
player_uid UUID PRIMARY KEY,
player_name CHAR(3) UNIQUE NOT NULL,
highscore_uid UUID REFERENCES highscores(highscore_uid) ON DELETE CASCADE NOT NULL,
CONSTRAINT FK_Highscores UNIQUE (highscore_uid)
);
36 changes: 36 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: "3.8"
services:
web:
build:
context: ${PWD}/src
image: typefight-flask:1.0
container_name: typefight-flask
ports:
- 5000:5000
environment:
- FLASK_APP=${FLASK_MODULE}
- FLASK_ENV=${FLASK_ENV}
# Remove this in production
- FLASK_RUN_HOST=0.0.0.0
volumes:
- ${PWD}/src/typefight:/var/app/typefight
depends_on:
- database
database:
build:
context: ${PWD}/database
image: typefight-db:1.0
container_name: typefight-postgresql
ports:
- 5432:5432
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
- PGTZ=America/Los_Angeles
volumes:
- typefight-db:/var/lib/postgresql/data
volumes:
typefight-db:
driver: local
name: typefight-db
10 changes: 10 additions & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.9.5-slim-buster

WORKDIR /var/app/

COPY requirements.txt .
COPY typefight/ ./typefight/

RUN pip install --no-cache-dir -r requirements.txt

CMD ["flask", "run"]
2 changes: 2 additions & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
psycopg2-binary
16 changes: 16 additions & 0 deletions src/typefight/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
from flask import Flask

def create_app():
app = Flask(__name__, instance_relative_config=True)

try:
os.makedirs(app.instance_path)
except OSError:
pass

@app.route("/hello")
def hello():
return "Hello, world!"

return app

0 comments on commit c76e2a2

Please sign in to comment.