-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working api and clean and fix porfolio empty (#95)
- Loading branch information
1 parent
620a6cf
commit e310d1e
Showing
19 changed files
with
230 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ services: | |
volumes: | ||
- ./api:/api | ||
volumes: | ||
persistent: | ||
api: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ fastapi | |
uvicorn | ||
httpx | ||
marketwatch | ||
jinja2 | ||
jinja2 | ||
python-jose | ||
pydantic | ||
marketwatch |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,6 @@ | |
<li>Cash borrowed: {{ data.cash_borrowed }}</li> | ||
</ul> | ||
</div> | ||
|
||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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> | ||
|
Oops, something went wrong.