Skip to content
Extraits de code Groupes Projets
Valider d13f66e7 rédigé par Guillaume Schurck's avatar Guillaume Schurck
Parcourir les fichiers

add redis cache and fix firefox post requests

parent fdeb185c
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -16,10 +16,6 @@ services:
image: "redis:alpine"
ports:
- "6379:6379"
redis-client:
build: ./redis
depends_on:
- redis
sg-backend:
build: ./sg-backend
command: sh -c "/wait && uvicorn main:app --reload --host 0.0.0.0"
......
Fichier déplacé
import redis
r = redis.Redis(host='redis', port=6379, db=0)
r = redis.Redis(host='redis-test', port=6379, db=0)
r.set('foo', 'bar')
result = r.get('foo')
print(result)
\ No newline at end of file
Fichier déplacé
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from redis import asyncio as aioredis
class RedisCache:
def __init__(self):
redis = aioredis.from_url("redis://redis", encoding="utf8", decode_responses=True)
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
@staticmethod
async def clear_softwares_cache():
await FastAPICache.clear(key="software")
await FastAPICache.clear(key="software-graph")
\ No newline at end of file
......@@ -3,9 +3,11 @@ import os
import random
from fastapi import FastAPI
from fastapi_cache.decorator import cache
from pydantic import BaseModel
from starlette.middleware.cors import CORSMiddleware
from cache import RedisCache
from db import Neo4j
......@@ -29,40 +31,29 @@ app = FastAPI()
origins = [
os.getenv("FRONTEND_URL"),
"http://localhost",
"http://localhost:4173",
"http://localhost:5173",
]
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}
@app.get("/graph")
async def graph():
results = driver.get_graph_data()
return json.loads(results[0]['data'])
@app.get("/software")
@cache(expire=60, key_builder=lambda *args, **kwargs: "software")
async def software():
results = driver.get_softwares()
return {"softwares": json.loads(json.dumps(results))}
@app.get("/software-graph")
@cache(expire=60, key_builder=lambda *args, **kwargs: "software-graph")
async def software():
nodes = driver.get_nodes()
jnodes = json.loads(json.dumps(nodes))
......@@ -106,10 +97,27 @@ async def software_search(search: SoftwareSearch):
@app.post("/software-insert")
async def software_insert(insert_data: SoftwareInsert):
res = driver.insert_software(insert_data)
await RedisCache.clear_softwares_cache()
return res
@app.post("/relation-insert")
async def software_insert(insert_data: RelationInsert):
res = driver.relation_insert(insert_data)
await RedisCache.clear_softwares_cache()
return res
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.on_event("startup")
async def startup():
RedisCache()
# @app.get("/graph")
# async def graph():
# results = driver.get_graph_data()
# return json.loads(results[0]['data'])
\ No newline at end of file
......@@ -11,7 +11,7 @@
onMount(async () => {
const relationAddButton = document.getElementById("relationAddButton");
relationAddButton.addEventListener("click", async () => {
relationAddButton.addEventListener("click", async (e) => {
const response = await fetch(BACKEND_URL + "/relation-insert", {
method: "POST",
headers: {
......@@ -27,6 +27,7 @@
result = await response.json();
node = result.node;
}
e.preventDefault();
});
await fetch(BACKEND_URL + "/software")
......
......@@ -13,8 +13,9 @@
const softwareName = document.getElementById("softwareName");
const newNodeButton = document.getElementById("newNodeButton");
newNodeButton.addEventListener("click", () => {
newNodeButton.addEventListener("click", (e) => {
postSoftwareInsert();
e.preventDefault();
});
async function postSoftwareInsert() {
......
......@@ -5,17 +5,13 @@
import RelationInsert from "../components/RelationInsert.svelte";
onMount(() => {
fetch("http://localhost:8000/add-node")
.then(response => response.json())
.then(data => {
// Retrieve some useful DOM elements:
const link = document.getElementById("sigma") as HTMLElement;
const home = document.getElementById("home") as HTMLElement;
const add = document.getElementById("add") as HTMLElement;
add.classList.add("active");
home.classList.remove("active");
link.classList.remove("active");
})
});
</script>
<div id="app-base">
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter