diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index aa44cde39..5028b5ba3 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -74,7 +74,7 @@ jobs: test-e2e: name: Run E2E tests - needs: [tag-pr, build-backend] + needs: [build-backend, build-frontend] uses: ./.github/workflows/test-on-kube.yml secrets: API_OVH_TOKEN: ${{ secrets.API_OVH_TOKEN }} diff --git a/.github/workflows/test-on-kube.yml b/.github/workflows/test-on-kube.yml index 131920cac..9b4eddf04 100644 --- a/.github/workflows/test-on-kube.yml +++ b/.github/workflows/test-on-kube.yml @@ -56,7 +56,6 @@ jobs: --set-string backend.secret.values.AWS_SECRET_ACCESS_KEY="${{ secrets.AWS_SECRET_ACCESS_KEY }}" \ --set-string backend.secret.values.X_OVH_TOKEN="${{ secrets.X_OVH_TOKEN }}" \ --set-string backend.secret.values.API_OVH_TOKEN="${{ secrets.API_OVH_TOKEN }}" - for i in $(kubectl get deploy -o name); do kubectl rollout status $i -w --timeout=130s; done - name: Display pod logs on failure if: failure() && steps.tests.outcome == 'failure' diff --git a/backend/src/main.py b/backend/src/main.py index 6c5ddfd30..d374ca7fc 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -9,8 +9,7 @@ from typing import Union import boto3 -from botocore.client import ClientError -from fastapi import BackgroundTasks, Cookie, FastAPI, File, Form, HTTPException, Request, Response, UploadFile +from fastapi import BackgroundTasks, Cookie, FastAPI, APIRouter, File, Form, HTTPException, Request, Response, UploadFile from fastapi.responses import PlainTextResponse from fastapi.middleware.cors import CORSMiddleware from gelfformatter import GelfFormatter @@ -139,6 +138,8 @@ def upload_image(content: bytes, image_key: str): # FastAPI Setup app = FastAPI() +router = APIRouter(prefix="/api") + origins = [ # allow requests from front-end "http://basegun.fr", "https://basegun.fr", @@ -198,17 +199,17 @@ def upload_image(content: bytes, image_key: str): #################### # ROUTES # #################### -@app.get("/", response_class=PlainTextResponse) +@router.get("/", response_class=PlainTextResponse) def home(): return "Basegun backend" -@app.get("/version", response_class=PlainTextResponse) +@router.get("/version", response_class=PlainTextResponse) def version(): return APP_VERSION -@app.get("/logs") +@router.get("/logs") def logs(): if "WORKSPACE" in os.environ and os.environ["WORKSPACE"] != "prod": with open(os.path.join(PATH_LOGS, "log.json"), "r") as f: @@ -220,7 +221,7 @@ def logs(): return PlainTextResponse("Forbidden") -@app.post("/upload") +@router.post("/upload") async def imageupload( request: Request, response: Response, @@ -279,7 +280,7 @@ async def imageupload( raise HTTPException(status_code=500, detail=str(e)) -@app.post("/identification-feedback") +@router.post("/identification-feedback") async def log_feedback(request: Request, user_id: Union[str, None] = Cookie(None)): res = await request.json() @@ -294,7 +295,7 @@ async def log_feedback(request: Request, user_id: Union[str, None] = Cookie(None return -@app.post("/tutorial-feedback") +@router.post("/tutorial-feedback") async def log_tutorial_feedback(request: Request, user_id: Union[str, None] = Cookie(None)): res = await request.json() @@ -309,7 +310,7 @@ async def log_tutorial_feedback(request: Request, user_id: Union[str, None] = Co return -@app.post("/identification-dummy") +@router.post("/identification-dummy") async def log_identification_dummy(request: Request, user_id: Union[str, None] = Cookie(None)): res = await request.json() @@ -322,4 +323,6 @@ async def log_identification_dummy(request: Request, user_id: Union[str, None] = extras_logging["bg_"+key] = res[key] logger.info("Identification dummy", extra=extras_logging) - return \ No newline at end of file + return + +app.include_router(router) \ No newline at end of file diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 4659144ae..ec828d9a1 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -39,12 +39,12 @@ def create_bucket(): class TestModel(unittest.TestCase): def test_home(self): """Checks that the route / is alive""" - response = client.get("/") + response = client.get("/api/") self.assertEqual(response.text, "Basegun backend") def test_version(self): """Checks that the route /version sends a version""" - response = client.get("/version") + response = client.get("/api/version") self.assertEqual(response.status_code, 200) def check_log_base(self, log): @@ -66,7 +66,7 @@ def test_upload(self): geoloc = "12.666,7.666" with open(path, 'rb') as f: - r = client.post("/upload", + r = client.post("/api/upload", files={"image": f}, data={"date": time.time(), "geolocation": geoloc}) self.assertEqual(r.status_code, 200) @@ -77,7 +77,7 @@ def test_upload(self): self.assertAlmostEqual(res["confidence"], 98.43, places=1) self.assertTrue(res["confidence_level"], "high") # checks that the result is written in logs - r = client.get("/logs") + r = client.get("/api/logs") self.assertEqual(r.status_code, 200) # checks the latest log with validates upload to object storage self.assertEqual(r.json()[0]["_bg_image_url"], r.json()[1]["_bg_image_url"]) @@ -98,11 +98,11 @@ def test_feedback_and_logs(self): label = "revolver" confidence_level = "high" image_url = "https://storage.gra.cloud.ovh.net/v1/test" - r = client.post("/identification-feedback", + r = client.post("/api/identification-feedback", json={"image_url": image_url, "feedback": True, "confidence": confidence, "label": label, "confidence_level": confidence_level}) self.assertEqual(r.status_code, 200) - r = client.get("/logs") + r = client.get("/api/logs") self.assertEqual(r.status_code, 200) log = r.json()[0] self.check_log_base(log) diff --git a/frontend/.env.development b/frontend/.env.development new file mode 100644 index 000000000..531964bd7 --- /dev/null +++ b/frontend/.env.development @@ -0,0 +1 @@ +VITE_API_HOST=http://localhost:5000 \ No newline at end of file diff --git a/frontend/.env.production b/frontend/.env.production new file mode 100644 index 000000000..fcefb4f3f --- /dev/null +++ b/frontend/.env.production @@ -0,0 +1 @@ +VITE_API_HOST=http://basegun.kubernetes.local \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 8dfbef8c6..46cd2c1f3 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -9,9 +9,7 @@ COPY ./cert/. /etc/ssl/certs/ COPY ./package.json ./package-lock.json ./ RUN npm ci --legacy-peer-deps -COPY src ./src -COPY public ./public -COPY vite.config.js index.html ./ +COPY . . FROM base as dev diff --git a/frontend/src/main.js b/frontend/src/main.js index 4946bc395..90cb73278 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -20,10 +20,11 @@ register() const pinia = createPinia() -axios.defaults.withCredentials = true - // the FastAPI backend -axios.defaults.baseURL = '/api/' +axios.defaults.baseURL = "http://basegun.kubernetes.local/api" +// if (import.meta.env.VITE_API_HOST) { +// axios.defaults.baseURL = import.meta.env.VITE_API_HOST + "/api" +// } const app = createApp(App) diff --git a/frontend/vite.config.js b/frontend/vite.config.js index 60e8385bd..51c397d79 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -5,8 +5,7 @@ import UnoCSS from 'unocss/vite' import transformerDirectives from '@unocss/transformer-directives' import transformerVariantGroup from '@unocss/transformer-variant-group' -const path = require('path') -const apiHost = process.env.API_HOST || 'basegun-backend' +const path = require("path"); // https://vitejs.dev/config/ export default defineConfig({ @@ -58,13 +57,6 @@ export default defineConfig({ }, }, server: { - host: true, - proxy: { - '^/api': { - target: `http://${apiHost}:5000`, - changeOrigin: true, - rewrite: (path) => path.replace(/^\/api/, ''), - }, - }, - }, + host: true + } })