Skip to content

Commit

Permalink
Add websocket to api and clean up files
Browse files Browse the repository at this point in the history
  • Loading branch information
mariemeSall committed Jan 24, 2024
1 parent e7d2466 commit 8c87275
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.venv
venv
pyvenv.cfg
kafka-3.6.0-src.tgz
# See http://help.github.com/ignore-files/ for more about ignoring files.
Expand Down
5 changes: 0 additions & 5 deletions backend/BDD/crud.py

This file was deleted.

3 changes: 0 additions & 3 deletions backend/api/coordonnees.py

This file was deleted.

17 changes: 0 additions & 17 deletions backend/api/routeur.py

This file was deleted.

22 changes: 16 additions & 6 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from fastapi import FastAPI
from fastapi import Depends, FastAPI, WebSocket
from datetime import datetime
from api.routeur import router as api_routeur
from fastapi_sqlalchemy import DBSessionMiddleware, db
from BDD.models import Coordonnee
from BDD.schema import Coordonnee as Model_coordonnee

import os
from dotenv import load_dotenv

date_format = '%Y-%m-%d %H:%M:%S'

load_dotenv('.env')

ws_connections = []

def get_connections() :
return ws_connections

app = FastAPI()

# to avoid csrftokenError
app.add_middleware(DBSessionMiddleware, db_url=os.environ['DATABASE_URL'])
app.include_router(api_routeur)


@app.get("/")
Expand All @@ -30,8 +31,17 @@ async def get_coordonnees(ip):
return coor

@app.post('/coordonnees')
async def add_coordonnees(coor: Model_coordonnee):
db_coor = Coordonnee(latitude = coor.latitude, longitude = coor.longitude, ip = coor.ip, date = datetime.strptime("2024-01-17 17:08:0", date_format))
async def add_coordonnees(coor: Model_coordonnee, connections= Depends(get_connections)):
db_coor = Coordonnee(latitude = coor.latitude, longitude = coor.longitude, ip = coor.ip, date = datetime.strptime(coor.date, date_format))
db.session.add(db_coor)
db.session.commit()
await connections[0].send_json({'latitude': coor.latitude, 'longitude':coor.longitude, 'ip': coor.ip, 'date': coor.date})
return True

@app.websocket('/ws/{ip}')
async def websocket_endpoint(websocket: WebSocket, ip:int, connections = Depends(get_connections)):
await websocket.accept()
connections.append(websocket)
while True:
data = await websocket.receive_json()
await websocket.send_json(data)
1 change: 0 additions & 1 deletion bin/python

This file was deleted.

1 change: 0 additions & 1 deletion bin/python3

This file was deleted.

1 change: 0 additions & 1 deletion lib64

This file was deleted.

6 changes: 6 additions & 0 deletions requirement copy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#insatallation postgresql
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql

16 changes: 10 additions & 6 deletions requirement.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#insatallation postgresql
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql

confluent_kafka
fastapi
fastapi-sqlalchemy
pydantic
alembic
psycopg2-binary
uvicorn
python-dotenv
requests
websockets

0 comments on commit 8c87275

Please sign in to comment.