-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
61 lines (50 loc) · 1.64 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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from redis import Redis
from os import getenv
from threading import Thread, Lock
from concurrent.futures import ThreadPoolExecutor, as_completed
from shared_resources.datahub_synchronizer import data_aggregator, backend_synchronizer
from routers import (
channels
)
app = FastAPI()
app.include_router(channels.router, prefix='/channels')
# Connect to Redis
redis_host = getenv('REDIS_HOST', 'localhost')
redis_port = getenv('REDIS_PORT', '6379')
redis = Redis(host=redis_host, port=redis_port, db=0)
# Allow requests from everywhere
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
def on_startup():
# Start the data aggregator in a separate thread
aggregator_thread = Thread(target=data_aggregator)
aggregator_thread.daemon = True
aggregator_thread.start()
# Start the backend synchronizer in a separate thread
backend_channel_thread = Thread(target=backend_synchronizer)
backend_channel_thread.daemon = True
backend_channel_thread.start()
@app.get("/")
def root():
return {"message": "Hello, World!"}
@app.get("/counter")
def get_counter_route():
# Get the counter from Redis (default to 0 if not set)
counter = redis.get('counter')
if counter is None:
counter = 0
redis.set('counter', counter)
return {"counter": int(counter)}
@app.post("/increment")
def increment_counter_route():
# Increment the counter in Redis
counter = redis.incr('counter')
return {"counter": int(counter)}