-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
82 lines (63 loc) · 2.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import logging
from typing import Optional, List, Dict, Tuple
from uuid import UUID
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import uvicorn
from event_sourced_bank.bank_system import EventSourcedBank
logging.basicConfig(level=logging.INFO)
# Set up the web server environment
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
# ...and start the bank system
bank = EventSourcedBank()
bank.start()
account_svc = bank.get_account_service()
ledger_svc = bank.get_ledger_service()
transaction_log_svc = bank.get_transaction_log_service()
def get_bank_state() -> Tuple:
ac_ids = account_svc.get_all_account_ids()
accounts = [{"index": idx, "id": id, "balance": account_svc.get_balance(id)} for idx, id in enumerate(ac_ids)]
balance = ledger_svc.get_balance()
transaction_count = ledger_svc.get_transaction_count()
transaction_log = list(reversed(transaction_log_svc.get_transactions()))
return accounts, balance, transaction_count, transaction_log
def render_bank_state(request: Request, template="bank_state.html"):
# For testing: uncomment the following to delay response, in order to
# ensure the UI shows the "in progress" spinner whilst waiting on response.
# import time
# time.sleep(1.0)
accounts, balance, transaction_count, transaction_log = get_bank_state()
return templates.TemplateResponse(template,
{"request": request,
"accounts": accounts,
"balance": balance,
"transaction_count": transaction_count,
"transaction_log": transaction_log})
@app.get("/", response_class=HTMLResponse)
def index(request: Request):
return render_bank_state(request, "index.html")
@app.post("/create-account", response_class=HTMLResponse)
def create_account(request: Request):
account_svc.create_account()
return render_bank_state(request)
@app.post("/credit-account", response_class=HTMLResponse)
def credit_account(request: Request,
account: str = Form(""),
amount: int = Form(0)):
account_svc.credit_account(UUID(account), amount)
return render_bank_state(request)
@app.post("/debit-account", response_class=HTMLResponse)
def debit_account(request: Request,
account: str = Form(""),
amount: int = Form(0)):
account_svc.debit_account(UUID(account), amount)
return render_bank_state(request)
if __name__ == "__main__":
logging.info("opening the bank")
uvicorn.run("main:app", host='0.0.0.0', port=8000, reload=True)
bank.stop()
logging.info("bank closed")