Skip to content

Commit

Permalink
Merge pull request #4 from TrentonBowserFanClub/dev/move-bazel-api-v2
Browse files Browse the repository at this point in the history
Moving bazel API to v2, remaking v1
  • Loading branch information
NathanDuPont authored Mar 30, 2024
2 parents 1df0e57 + b8356d1 commit 311938b
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 129 deletions.
318 changes: 224 additions & 94 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
asyncio
fastapi
uvicorn[standard]
jinja2
opencv-python
uvicorn[standard]
websockets
123 changes: 122 additions & 1 deletion requirements_lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ anyio==4.3.0 \
# via
# starlette
# watchfiles
asyncio==3.4.3 \
--hash=sha256:83360ff8bc97980e4ff25c964c7bd3923d333d177aa4f7fb736b019f26c7cb41 \
--hash=sha256:b62c9157d36187eca799c378e572c969f0da87cd5fc42ca372d92cdb06e7e1de \
--hash=sha256:c46a87b48213d7464f22d9a497b9eef8c1928b68320a2fa94240f969f6fec08c \
--hash=sha256:c4d18b22701821de07bd6aea8b53d21449ec0ec5680645e5317062ea21817d2d
# via -r requirements.in
click==8.1.7 \
--hash=sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28 \
--hash=sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de
Expand Down Expand Up @@ -68,6 +74,119 @@ idna==3.6 \
--hash=sha256:9ecdbbd083b06798ae1e86adcbfe8ab1479cf864e4ee30fe4e46a003d12491ca \
--hash=sha256:c05567e9c24a6b9faaa835c4821bad0590fbb9d5779e7caa6e1cc4978e7eb24f
# via anyio
jinja2==3.1.3 \
--hash=sha256:7d6d50dd97d52cbc355597bd845fabfbac3f551e1f99619e39a35ce8c370b5fa \
--hash=sha256:ac8bd6544d4bb2c9792bf3a159e80bba8fda7f07e81bc3aed565432d5925ba90
# via -r requirements.in
markupsafe==2.1.5 \
--hash=sha256:00e046b6dd71aa03a41079792f8473dc494d564611a8f89bbbd7cb93295ebdcf \
--hash=sha256:075202fa5b72c86ad32dc7d0b56024ebdbcf2048c0ba09f1cde31bfdd57bcfff \
--hash=sha256:0e397ac966fdf721b2c528cf028494e86172b4feba51d65f81ffd65c63798f3f \
--hash=sha256:17b950fccb810b3293638215058e432159d2b71005c74371d784862b7e4683f3 \
--hash=sha256:1f3fbcb7ef1f16e48246f704ab79d79da8a46891e2da03f8783a5b6fa41a9532 \
--hash=sha256:2174c595a0d73a3080ca3257b40096db99799265e1c27cc5a610743acd86d62f \
--hash=sha256:2b7c57a4dfc4f16f7142221afe5ba4e093e09e728ca65c51f5620c9aaeb9a617 \
--hash=sha256:2d2d793e36e230fd32babe143b04cec8a8b3eb8a3122d2aceb4a371e6b09b8df \
--hash=sha256:30b600cf0a7ac9234b2638fbc0fb6158ba5bdcdf46aeb631ead21248b9affbc4 \
--hash=sha256:397081c1a0bfb5124355710fe79478cdbeb39626492b15d399526ae53422b906 \
--hash=sha256:3a57fdd7ce31c7ff06cdfbf31dafa96cc533c21e443d57f5b1ecc6cdc668ec7f \
--hash=sha256:3c6b973f22eb18a789b1460b4b91bf04ae3f0c4234a0a6aa6b0a92f6f7b951d4 \
--hash=sha256:3e53af139f8579a6d5f7b76549125f0d94d7e630761a2111bc431fd820e163b8 \
--hash=sha256:4096e9de5c6fdf43fb4f04c26fb114f61ef0bf2e5604b6ee3019d51b69e8c371 \
--hash=sha256:4275d846e41ecefa46e2015117a9f491e57a71ddd59bbead77e904dc02b1bed2 \
--hash=sha256:4c31f53cdae6ecfa91a77820e8b151dba54ab528ba65dfd235c80b086d68a465 \
--hash=sha256:4f11aa001c540f62c6166c7726f71f7573b52c68c31f014c25cc7901deea0b52 \
--hash=sha256:5049256f536511ee3f7e1b3f87d1d1209d327e818e6ae1365e8653d7e3abb6a6 \
--hash=sha256:58c98fee265677f63a4385256a6d7683ab1832f3ddd1e66fe948d5880c21a169 \
--hash=sha256:598e3276b64aff0e7b3451b72e94fa3c238d452e7ddcd893c3ab324717456bad \
--hash=sha256:5b7b716f97b52c5a14bffdf688f971b2d5ef4029127f1ad7a513973cfd818df2 \
--hash=sha256:5dedb4db619ba5a2787a94d877bc8ffc0566f92a01c0ef214865e54ecc9ee5e0 \
--hash=sha256:619bc166c4f2de5caa5a633b8b7326fbe98e0ccbfacabd87268a2b15ff73a029 \
--hash=sha256:629ddd2ca402ae6dbedfceeba9c46d5f7b2a61d9749597d4307f943ef198fc1f \
--hash=sha256:656f7526c69fac7f600bd1f400991cc282b417d17539a1b228617081106feb4a \
--hash=sha256:6ec585f69cec0aa07d945b20805be741395e28ac1627333b1c5b0105962ffced \
--hash=sha256:72b6be590cc35924b02c78ef34b467da4ba07e4e0f0454a2c5907f473fc50ce5 \
--hash=sha256:7502934a33b54030eaf1194c21c692a534196063db72176b0c4028e140f8f32c \
--hash=sha256:7a68b554d356a91cce1236aa7682dc01df0edba8d043fd1ce607c49dd3c1edcf \
--hash=sha256:7b2e5a267c855eea6b4283940daa6e88a285f5f2a67f2220203786dfa59b37e9 \
--hash=sha256:823b65d8706e32ad2df51ed89496147a42a2a6e01c13cfb6ffb8b1e92bc910bb \
--hash=sha256:8590b4ae07a35970728874632fed7bd57b26b0102df2d2b233b6d9d82f6c62ad \
--hash=sha256:8dd717634f5a044f860435c1d8c16a270ddf0ef8588d4887037c5028b859b0c3 \
--hash=sha256:8dec4936e9c3100156f8a2dc89c4b88d5c435175ff03413b443469c7c8c5f4d1 \
--hash=sha256:97cafb1f3cbcd3fd2b6fbfb99ae11cdb14deea0736fc2b0952ee177f2b813a46 \
--hash=sha256:a17a92de5231666cfbe003f0e4b9b3a7ae3afb1ec2845aadc2bacc93ff85febc \
--hash=sha256:a549b9c31bec33820e885335b451286e2969a2d9e24879f83fe904a5ce59d70a \
--hash=sha256:ac07bad82163452a6884fe8fa0963fb98c2346ba78d779ec06bd7a6262132aee \
--hash=sha256:ae2ad8ae6ebee9d2d94b17fb62763125f3f374c25618198f40cbb8b525411900 \
--hash=sha256:b91c037585eba9095565a3556f611e3cbfaa42ca1e865f7b8015fe5c7336d5a5 \
--hash=sha256:bc1667f8b83f48511b94671e0e441401371dfd0f0a795c7daa4a3cd1dde55bea \
--hash=sha256:bec0a414d016ac1a18862a519e54b2fd0fc8bbfd6890376898a6c0891dd82e9f \
--hash=sha256:bf50cd79a75d181c9181df03572cdce0fbb75cc353bc350712073108cba98de5 \
--hash=sha256:bff1b4290a66b490a2f4719358c0cdcd9bafb6b8f061e45c7a2460866bf50c2e \
--hash=sha256:c061bb86a71b42465156a3ee7bd58c8c2ceacdbeb95d05a99893e08b8467359a \
--hash=sha256:c8b29db45f8fe46ad280a7294f5c3ec36dbac9491f2d1c17345be8e69cc5928f \
--hash=sha256:ce409136744f6521e39fd8e2a24c53fa18ad67aa5bc7c2cf83645cce5b5c4e50 \
--hash=sha256:d050b3361367a06d752db6ead6e7edeb0009be66bc3bae0ee9d97fb326badc2a \
--hash=sha256:d283d37a890ba4c1ae73ffadf8046435c76e7bc2247bbb63c00bd1a709c6544b \
--hash=sha256:d9fad5155d72433c921b782e58892377c44bd6252b5af2f67f16b194987338a4 \
--hash=sha256:daa4ee5a243f0f20d528d939d06670a298dd39b1ad5f8a72a4275124a7819eff \
--hash=sha256:db0b55e0f3cc0be60c1f19efdde9a637c32740486004f20d1cff53c3c0ece4d2 \
--hash=sha256:e61659ba32cf2cf1481e575d0462554625196a1f2fc06a1c777d3f48e8865d46 \
--hash=sha256:ea3d8a3d18833cf4304cd2fc9cbb1efe188ca9b5efef2bdac7adc20594a0e46b \
--hash=sha256:ec6a563cff360b50eed26f13adc43e61bc0c04d94b8be985e6fb24b81f6dcfdf \
--hash=sha256:f5dfb42c4604dddc8e4305050aa6deb084540643ed5804d7455b5df8fe16f5e5 \
--hash=sha256:fa173ec60341d6bb97a89f5ea19c85c5643c1e7dedebc22f5181eb73573142c5 \
--hash=sha256:fa9db3f79de01457b03d4f01b34cf91bc0048eb2c3846ff26f66687c2f6d16ab \
--hash=sha256:fce659a462a1be54d2ffcacea5e3ba2d74daa74f30f5f143fe0c58636e355fdd \
--hash=sha256:ffee1f21e5ef0d712f9033568f8344d5da8cc2869dbd08d87c84656e6a2d2f68
# via jinja2
numpy==1.26.4 \
--hash=sha256:03a8c78d01d9781b28a6989f6fa1bb2c4f2d51201cf99d3dd875df6fbd96b23b \
--hash=sha256:08beddf13648eb95f8d867350f6a018a4be2e5ad54c8d8caed89ebca558b2818 \
--hash=sha256:1af303d6b2210eb850fcf03064d364652b7120803a0b872f5211f5234b399f20 \
--hash=sha256:1dda2e7b4ec9dd512f84935c5f126c8bd8b9f2fc001e9f54af255e8c5f16b0e0 \
--hash=sha256:2a02aba9ed12e4ac4eb3ea9421c420301a0c6460d9830d74a9df87efa4912010 \
--hash=sha256:2e4ee3380d6de9c9ec04745830fd9e2eccb3e6cf790d39d7b98ffd19b0dd754a \
--hash=sha256:3373d5d70a5fe74a2c1bb6d2cfd9609ecf686d47a2d7b1d37a8f3b6bf6003aea \
--hash=sha256:47711010ad8555514b434df65f7d7b076bb8261df1ca9bb78f53d3b2db02e95c \
--hash=sha256:4c66707fabe114439db9068ee468c26bbdf909cac0fb58686a42a24de1760c71 \
--hash=sha256:50193e430acfc1346175fcbdaa28ffec49947a06918b7b92130744e81e640110 \
--hash=sha256:52b8b60467cd7dd1e9ed082188b4e6bb35aa5cdd01777621a1658910745b90be \
--hash=sha256:60dedbb91afcbfdc9bc0b1f3f402804070deed7392c23eb7a7f07fa857868e8a \
--hash=sha256:62b8e4b1e28009ef2846b4c7852046736bab361f7aeadeb6a5b89ebec3c7055a \
--hash=sha256:666dbfb6ec68962c033a450943ded891bed2d54e6755e35e5835d63f4f6931d5 \
--hash=sha256:675d61ffbfa78604709862923189bad94014bef562cc35cf61d3a07bba02a7ed \
--hash=sha256:679b0076f67ecc0138fd2ede3a8fd196dddc2ad3254069bcb9faf9a79b1cebcd \
--hash=sha256:7349ab0fa0c429c82442a27a9673fc802ffdb7c7775fad780226cb234965e53c \
--hash=sha256:7ab55401287bfec946ced39700c053796e7cc0e3acbef09993a9ad2adba6ca6e \
--hash=sha256:7e50d0a0cc3189f9cb0aeb3a6a6af18c16f59f004b866cd2be1c14b36134a4a0 \
--hash=sha256:95a7476c59002f2f6c590b9b7b998306fba6a5aa646b1e22ddfeaf8f78c3a29c \
--hash=sha256:96ff0b2ad353d8f990b63294c8986f1ec3cb19d749234014f4e7eb0112ceba5a \
--hash=sha256:9fad7dcb1aac3c7f0584a5a8133e3a43eeb2fe127f47e3632d43d677c66c102b \
--hash=sha256:9ff0f4f29c51e2803569d7a51c2304de5554655a60c5d776e35b4a41413830d0 \
--hash=sha256:a354325ee03388678242a4d7ebcd08b5c727033fcff3b2f536aea978e15ee9e6 \
--hash=sha256:a4abb4f9001ad2858e7ac189089c42178fcce737e4169dc61321660f1a96c7d2 \
--hash=sha256:ab47dbe5cc8210f55aa58e4805fe224dac469cde56b9f731a4c098b91917159a \
--hash=sha256:afedb719a9dcfc7eaf2287b839d8198e06dcd4cb5d276a3df279231138e83d30 \
--hash=sha256:b3ce300f3644fb06443ee2222c2201dd3a89ea6040541412b8fa189341847218 \
--hash=sha256:b97fe8060236edf3662adfc2c633f56a08ae30560c56310562cb4f95500022d5 \
--hash=sha256:bfe25acf8b437eb2a8b2d49d443800a5f18508cd811fea3181723922a8a82b07 \
--hash=sha256:cd25bcecc4974d09257ffcd1f098ee778f7834c3ad767fe5db785be9a4aa9cb2 \
--hash=sha256:d209d8969599b27ad20994c8e41936ee0964e6da07478d6c35016bc386b66ad4 \
--hash=sha256:d5241e0a80d808d70546c697135da2c613f30e28251ff8307eb72ba696945764 \
--hash=sha256:edd8b5fe47dab091176d21bb6de568acdd906d1887a4584a15a9a96a1dca06ef \
--hash=sha256:f870204a840a60da0b12273ef34f7051e98c3b5961b61b0c2c1be6dfd64fbcd3 \
--hash=sha256:ffa75af20b44f8dba823498024771d5ac50620e6915abac414251bd971b4529f
# via opencv-python
opencv-python==4.9.0.80 \
--hash=sha256:1a9f0e6267de3a1a1db0c54213d022c7c8b5b9ca4b580e80bdc58516c922c9e1 \
--hash=sha256:3f16f08e02b2a2da44259c7cc712e779eff1dd8b55fdb0323e8cab09548086c0 \
--hash=sha256:71dfb9555ccccdd77305fc3dcca5897fbf0cf28b297c51ee55e079c065d812a3 \
--hash=sha256:7b34a52e9da36dda8c151c6394aed602e4b17fa041df0b9f5b93ae10b0fcca2a \
--hash=sha256:7e5f7aa4486651a6ebfa8ed4b594b65bd2d2f41beeb4241a3e4b1b85acbbbadb \
--hash=sha256:dcf000c36dd1651118a2462257e3a9e76db789a78432e1f303c7bac54f63ef6c \
--hash=sha256:e4088cab82b66a3b37ffc452976b14a3c599269c247895ae9ceb4066d8188a57
# via -r requirements.in
pydantic==2.6.4 \
--hash=sha256:b1704e0847db01817624a6b86766967f552dd9dbf3afba4004409f908dcc84e6 \
--hash=sha256:cc46fce86607580867bdc3361ad462bab9c222ef042d3da86f2fb333e1d916c5
Expand Down Expand Up @@ -412,4 +531,6 @@ websockets==12.0 \
--hash=sha256:f764ba54e33daf20e167915edc443b6f88956f37fb606449b4a5b10ba42235a5 \
--hash=sha256:fc4e7fa5414512b481a2483775a8e8be7803a35b30ca805afa4998a84f9fd9e8 \
--hash=sha256:ffefa1374cd508d633646d51a8e9277763a9b78ae71324183693959cf94635a7
# via uvicorn
# via
# -r requirements.in
# uvicorn
46 changes: 18 additions & 28 deletions src/api/v1/README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,40 @@
# TrentonBot API
# API v1

This API allows for interfacing between the webapp and the vehicle code over ROS 2.
This version of the API uses a local venv to install packages, and only runs locally.

# Contributing
## Creating a venv

## Testing Code

You can test this code locally by running the command:
Use the following command to create your venv:

```
bazel run //src/api/v1:main
python3.11 -m venv venv
```

This will start up the API using uvicorn, running on port 8000 unless the port is in use.

If you want to use args with the app, you can run the following:
From here, you can source your venv with the following commands.
Windows, probably:

```
bazel run //src/api/v1:main -- <ARGS>
./venv/bin/Activate.ps1
```

The extra `--` allows us to skip over the _bazel_ arguments, and start passing arguments directly to our process.

**NOTE**: Right now, certain args such as `--reload` don't work with bazel, as built files are copied into a `bazel-out` directory instead of using the files directly in the repo. This means, when running with `--reload`, uvicorn watches the foles in the cached build directory and NOT the files that you'd be editing in the repo. Looking for a workaround in this.

## Building a Docker Image Locally

The docker image can be built and published locally using the command:
Linux:

```
bazel run //src/api/v1:tarball
. venv/bin/activate
```

This will publish the image to your local docker images based on the provided tag:
## Installing needed packages

You can install packages required for the application using the command:

```
INFO: Running command line: bazel-bin/src/api/v1/tarball.sh
f4ba9898d2a2: Loading layer 17.56kB/17.56kB
The image trentonbot_api_v1:latest already exists, renaming the old one with ID sha256:ccb7026cdc6bcb8f6a46a86b8e363581f33e5168f5cab5cde88f77cd8e0539b5 to empty string
Loaded image: trentonbot_api_v1:latest
pip install -r requirements.txt
```

In this case, if you want to run the image locally, you can used the loaded image above to run:
## Saving pip requirements

Run the following command to update the `requirements.txt` file, which is needed whenever you install a new package:

```
docker run -p 8000:8000 trentonbot_api_v1:latest
pip freeze > requirements.txt
```

This will spin up the docker container locally, and allow you to test from it.
22 changes: 22 additions & 0 deletions src/api/v1/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
annotated-types==0.6.0
anyio==4.3.0
click==8.1.7
fastapi==0.110.0
h11==0.14.0
httptools==0.6.1
idna==3.6
Jinja2==3.1.3
MarkupSafe==2.1.5
numpy==1.26.4
opencv-python==4.9.0.80
pydantic==2.6.4
pydantic_core==2.16.3
python-dotenv==1.0.1
PyYAML==6.0.1
sniffio==1.3.1
starlette==0.36.3
typing_extensions==4.10.0
uvicorn==0.29.0
uvloop==0.19.0
watchfiles==0.21.0
websockets==12.0
13 changes: 8 additions & 5 deletions src/api/v1/BUILD.bazel → src/api/v2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ py_library(
srcs = ["api.py"],
deps = [
"@pypi//fastapi:pkg",
"@pypi//jinja2:pkg",
"@pypi//opencv_python:pkg",
"@pypi//websockets:pkg",
],
)

Expand All @@ -17,7 +20,7 @@ py_binary(
name = "main",
srcs = ["main.py"],
args = [
"src.api.v1.api:app",
"src.api.v2.api:app",
"--host",
"0.0.0.0",
],
Expand All @@ -36,11 +39,11 @@ py_oci_image(
base = "@py3.11",
binary = ":main",
cmd = [
"src.api.v1.api:app",
"src.api.v2.api:app",
"--host",
"0.0.0.0",
],
entrypoint = ["src/api/v1/main"],
entrypoint = ["src/api/v2/main"],
exposed_ports = ["8000"],
)

Expand All @@ -50,13 +53,13 @@ py_oci_image(
oci_tarball(
name = "tarball",
image = ":image",
repo_tags = ["trentonbot_api_v1:latest"],
repo_tags = ["trentonbot_api_v2:latest"],
visibility = ["//visibility:public"],
)

oci_push(
name = "push",
image = ":image",
remote_tags = ["v0.0.1"],
repository = "trentonbot_api_v1",
repository = "trentonbot_api_v2",
)
50 changes: 50 additions & 0 deletions src/api/v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# TrentonBot API

This API allows for interfacing between the webapp and the vehicle code over ROS 2.

# Contributing

## Testing Code

You can test this code locally by running the command:

```
bazel run //src/api/v2:main
```

This will start up the API using uvicorn, running on port 8000 unless the port is in use.

If you want to use args with the app, you can run the following:

```
bazel run //src/api/v2:main -- <ARGS>
```

The extra `--` allows us to skip over the _bazel_ arguments, and start passing arguments directly to our process.

**NOTE**: Right now, certain args such as `--reload` don't work with bazel, as built files are copied into a `bazel-out` directory instead of using the files directly in the repo. This means, when running with `--reload`, uvicorn watches the foles in the cached build directory and NOT the files that you'd be editing in the repo. Looking for a workaround in this.

## Building a Docker Image Locally

The docker image can be built and published locally using the command:

```
bazel run //src/api/v2:tarball
```

This will publish the image to your local docker images based on the provided tag:

```
INFO: Running command line: bazel-bin/src/api/v2/tarball.sh
f4ba9898d2a2: Loading layer 17.56kB/17.56kB
The image trentonbot_api_v2:latest already exists, renaming the old one with ID sha256:ccb7026cdc6bcb8f6a46a86b8e363581f33e5168f5cab5cde88f77cd8e0539b5 to empty string
Loaded image: trentonbot_api_v2:latest
```

In this case, if you want to run the image locally, you can used the loaded image above to run:

```
docker run -p 8000:8000 trentonbot_api_v2:latest
```

This will spin up the docker container locally, and allow you to test from it.
38 changes: 38 additions & 0 deletions src/api/v2/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Union
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from websockets.exceptions import ConnectionClosed
from fastapi.templating import Jinja2Templates
import asyncio
import cv2

app = FastAPI()
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
templates = Jinja2Templates(directory="templates")


@app.get("/")
def read_root():
return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}


# https://stackoverflow.com/a/70626324
# Not actually sure if this works with bazel
@app.websocket("/ws")
async def get_stream(websocket: WebSocket):
await websocket.accept()
try:
while True:
success, frame = camera.read()
if not success:
break
else:
ret, buffer = cv2.imencode(".jpg", frame)
await websocket.send_bytes(buffer.tobytes())
await asyncio.sleep(0.03)
except (WebSocketDisconnect, ConnectionClosed):
print("Client disconnected")
5 changes: 5 additions & 0 deletions src/api/v2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import uvicorn
import sys

if __name__ == "__main__":
sys.exit(uvicorn.main())

0 comments on commit 311938b

Please sign in to comment.