Skip to content

Commit

Permalink
working api and clean and fix porfolio empty (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebou12 authored Sep 23, 2023
1 parent 620a6cf commit e310d1e
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 183 deletions.
29 changes: 23 additions & 6 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
# syntax=docker/dockerfile:1
# Use a specific version for the base image
FROM python:3.12.0rc3-slim AS build-env

# Set working directory
WORKDIR /api

# Install build utilities and pip dependencies
RUN apt-get update -y && \
apt-get install -y build-essential && \
pip install --upgrade pip

# Copy requirements file and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy only the necessary code files and resources
COPY api/ ./api/

# Actual runtime image
FROM python:3.12.0rc3-slim

# Set working directory
WORKDIR /api

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt --no-cache-dir

COPY api/ .
# Copy from build environment
COPY --from=build-env /api /api

CMD ["unicorn", "marketwatch:app", "--bind", "0.0.0.0:5000", "--workers", "4", "--threads", "2"]
# Command to run the application
CMD ["unicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
16 changes: 16 additions & 0 deletions api/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
fastapi = "*"
uvicorn = "*"
httpx = "*"
jinja2 = "*"
python-jose = "*"
pydantic = "*"
marketwatch = "*"

[requires]
python_version = "3.9"
1 change: 0 additions & 1 deletion api/Procfile

This file was deleted.

File renamed without changes.
74 changes: 0 additions & 74 deletions api/api/app.py

This file was deleted.

29 changes: 0 additions & 29 deletions api/api/templates/leaderboard.html.j2

This file was deleted.

30 changes: 0 additions & 30 deletions api/app.json

This file was deleted.

64 changes: 64 additions & 0 deletions api/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from fastapi import FastAPI, Request, Header, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from jose import JWTError, jwt
from datetime import datetime, timedelta
from cryptography.hazmat.primitives import asymmetric, hashes, serialization
from cryptography.hazmat.backends import default_backend
import base64
import os

# import sys
# sys.path.append('..')
# from marketwatch import MarketWatch
from marketwatch import MarketWatch

# Initialize FastAPI and Jinja2
app = FastAPI(docs_url="/docs", redoc_url=None)
templates=Jinja2Templates(directory="templates")

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

security = HTTPBasic()

async def get_current_user(credentials: HTTPBasicCredentials = Depends(security)):
try:
mw = MarketWatch(credentials.username, credentials.password)
# Optionally, you could check some property or call some method on mw to ensure it's valid
return mw
except Exception as e:
# Log the error here if you want
raise HTTPException(status_code=401, detail="MarketWatch validation failed")

@app.get("/game/{game_id}", response_class=HTMLResponse)
async def game(request: Request, game_id: str, mw: MarketWatch = Depends(get_current_user)):
data = mw.get_game(game_id=game_id)
return templates.TemplateResponse("game.html.j2", {"request": request, "data": data})

@app.get("/portfolio/{game_id}", response_class=HTMLResponse)
async def portfolio(request: Request, game_id: str, mw: MarketWatch = Depends(get_current_user)):
data = mw.get_portfolio(game_id=game_id)
return templates.TemplateResponse("portfolio.html.j2", {"request": request, "data": data})

@app.get("/leaderboard/{game_id}", response_class=HTMLResponse)
async def leaderboard(request: Request, game_id: str, mw: MarketWatch = Depends(get_current_user)):
data = mw.get_leaderboard(game_id=game_id)
return templates.TemplateResponse("leaderboard.html.j2", {"request": request, "data": data})

@app.get("/card/{game_id}", response_class=HTMLResponse)
async def card(request: Request, game_id: str, mw: MarketWatch = Depends(get_current_user)):
data = mw.get_game(game_id=game_id)
return templates.TemplateResponse("card.html.j2", {"request": request, "data": data})

@app.get("/watchlist/{watchlist_id}", response_class=HTMLResponse)
async def watchlists(request: Request, watchlist_id: str, mw: MarketWatch = Depends(get_current_user)):
data = mw.get_watchlist(watchlist_id=watchlist_id)
return templates.TemplateResponse("watchlists.html.j2", {"request": request, "data": data})
2 changes: 1 addition & 1 deletion api/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ services:
volumes:
- ./api:/api
volumes:
persistent:
api:
5 changes: 4 additions & 1 deletion api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ fastapi
uvicorn
httpx
marketwatch
jinja2
jinja2
python-jose
pydantic
marketwatch
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<li>Cash borrowed: {{ data.cash_borrowed }}</li>
</ul>
</div>

</div>
</body>
</html>
47 changes: 47 additions & 0 deletions api/templates/leaderboard.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ data.game_id }} Leaderboard</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-black">
<div class="container mx-auto px-4 py-8">
<div class="bg-black rounded-lg overflow-hidden shadow-lg">
<div class="px-6 py-8">
<h1 class="text-3xl font-bold text-white">{{ data.game_id }} Leaderboard</h1>
<table class="table-auto w-full mt-8">
<thead>
<tr>
<th class="px-4 py-2 text-white">Rank</th>
<th class="px-4 py-2 text-white">Player</th>
<th class="px-4 py-2 text-white">Portfolio Value</th>
<th class="px-4 py-2 text-white">Gain Percentage</th>
<th class="px-4 py-2 text-white">Transactions</th>
<th class="px-4 py-2 text-white">Gain</th>
</tr>
</thead>
<tbody>
{% if data.players|length > 0 %}
{% for player in data.players %}
<tr>
<td class="border px-4 py-2 text-white">{{ player.rank }}</td>
<td class="border px-4 py-2 text-white"><a href="{{ player.player_url }}" class="text-blue-500 hover:underline">{{ player.player }}</a></td>
<td class="border px-4 py-2 text-white">{{ player.portfolio_value }}</td>
<td class="border px-4 py-2 text-white">{{ player.gain_percentage }}</td>
<td class="border px-4 py-2 text-white">{{ player.transactions }}</td>
<td class="border px-4 py-2 text-white">{{ player.gain }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td class="border px-4 py-2 text-center text-white" colspan="6">No portfolio data available.</td>
</tr>
{% endif %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ data.game_id }} Leaderboard</title>
<title>{{ data.game_id }} Portfolio</title>
<!-- Include Tailwind CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-900">
<div class="container mx-auto px-4 py-8">
<div class="bg-gray-800 rounded-lg shadow-lg overflow-hidden">
<h1 class="text-3xl font-bold text-white bg-gray-900 px-4 py-2">{{ data.game_id }} Leaderboard</h1>
<h1 class="text-3xl font-bold text-white bg-gray-900 px-4 py-2">{{ data.game_id }} Portfolio</h1>
<table class="table-auto w-full mt-8">
<thead>
<tr>
Expand All @@ -22,16 +22,22 @@
</tr>
</thead>
<tbody>
{% for player in data.players %}
{% if data.players|length > 0 %}
{% for player in data.players %}
<tr>
<td class="border px-4 py-2">{{ player.rank }}</td>
<td class="border px-4 py-2"><a href="{{ player.player_url }}" class="text-blue-500 hover:underline">{{ player.player }}</a></td>
<td class="border px-4 py-2">{{ player.portfolio_value }}</td>
<td class="border px-4 py-2">{{ player.gain_percentage }}</td>
<td class="border px-4 py-2">{{ player.transactions }}</td>
<td class="border px-4 py-2">{{ player.gain }}</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td class="border px-4 py-2">{{ player.rank }}</td>
<td class="border px-4 py-2"><a href="{{ player.player_url }}" class="text-blue-500 hover:underline">{{ player.player }}</a></td>
<td class="border px-4 py-2">{{ player.portfolio_value }}</td>
<td class="border px-4 py-2">{{ player.gain_percentage }}</td>
<td class="border px-4 py-2">{{ player.transactions }}</td>
<td class="border px-4 py-2">{{ player.gain }}</td>
<td class="border px-4 py-2 text-center" colspan="6">No portfolio data available.</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
</div>
Expand Down
Loading

0 comments on commit e310d1e

Please sign in to comment.