-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into edit-manufacturer-#65
- Loading branch information
Showing
31 changed files
with
3,413 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
FROM python:3.10-alpine3.18 | ||
|
||
WORKDIR /inventory-management-system-api-run | ||
|
||
COPY README.md pyproject.toml ./ | ||
# Copy inventory_management_system_api source files | ||
COPY inventory_management_system_api/ inventory_management_system_api/ | ||
|
||
RUN set -eux; \ | ||
\ | ||
# Install pip dependencies \ | ||
python -m pip install --no-cache-dir .; \ | ||
\ | ||
# Create loging.ini from its .example file \ | ||
cp inventory_management_system_api/logging.example.ini inventory_management_system_api/logging.ini; \ | ||
\ | ||
# Create a non-root user to run as \ | ||
addgroup -S inventory-management-system-api; \ | ||
adduser -S -D -G inventory-management-system-api -H -h /inventory-management-system-api-run inventory-management-system-api; \ | ||
\ | ||
# Create a log file \ | ||
touch inventory-management-system-api.log; \ | ||
# Change ownership of log file - app will need to write to it | ||
chown -R inventory-management-system-api:inventory-management-system-api inventory-management-system-api.log; | ||
|
||
USER inventory-management-system-api | ||
|
||
ENV API__TITLE="Inventory Management System API" | ||
ENV API__DESCRIPTION="This is the API for the Inventory Management System" | ||
ENV DATABASE__PROTOCOL=mongodb | ||
ENV DATABASE__USERNAME=root | ||
ENV DATABASE__PASSWORD=example | ||
ENV DATABASE__HOSTNAME=localhost | ||
ENV DATABASE__PORT=27017 | ||
ENV DATABASE__NAME=ims | ||
|
||
CMD ["uvicorn", "inventory_management_system_api.main:app", "--host", "0.0.0.0", "--port", "8000"] | ||
EXPOSE 8000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,65 +2,80 @@ | |
|
||
## How to Run | ||
|
||
This is a Python microservice created using FastAPI and requires a MongoDB instance to run against. If you are using | ||
Docker to run the application, the `docker-compose.yml file` has already been configured to start a MongoDB instance | ||
that can be accessed at `localhost:27017` using `root` as the username and `example` as the password. | ||
This is a Python microservice created using FastAPI and requires a MongoDB instance to run against. | ||
|
||
### Prerequisites | ||
- Docker installed (if you want to run the microservice inside Docker) | ||
- Python 3.10 (or above) and MongoDB 6.0 installed on your machine if you are not using Docker | ||
- [MongoDB Compass](https://www.mongodb.com/products/compass) installed (if you want to interact with the database using a GUI) | ||
- This repository cloned | ||
|
||
### Docker Setup | ||
The easiest way to run the application with Docker for local development is using the `docker-compose.yml` file. It is | ||
configured to spin up a MongoDB instance that can be accessed at `localhost:27017` using `root` as the username and | ||
`example` as the password. It also starts the application in a reload mode using the `Dockerfile`. Use the `Dockerfile` | ||
or `Dockerfile.prod` to run just the application itself in a container. The former is for local development and must | ||
not be used in production. | ||
|
||
1. Ensure that Docker is installed and running on your machine. | ||
2. Clone the repository and navigate to the project directory: | ||
```bash | ||
git clone [email protected]:ral-facilities/inventory-management-system-api.git | ||
cd inventory-management-system-api | ||
3. Create a `logging.ini` file. | ||
Ensure that Docker is installed and running on your machine before proceeding. | ||
|
||
#### Using Docker Compose File | ||
1. Create a `logging.ini` file from the example in the root of the project directory: | ||
```bash | ||
cp logging.example.ini logging.ini | ||
``` | ||
|
||
4. Build and start the Docker containers: | ||
2. Build and start the Docker containers: | ||
```bash | ||
docker-compose up | ||
``` | ||
The microservice should now be running inside Docker at http://localhost:8000. The Swagger UI can be accessed | ||
at http://localhost:8000/docs. | ||
The microservice should now be running inside Docker at http://localhost:8000 and its Swagger UI could be accessed | ||
at http://localhost:8000/docs. A MongoDB instance should also be running at http://localhost:27017. | ||
|
||
### Local Setup | ||
#### Using Dockerfiles | ||
|
||
1. Build an image using either the `Dockerfile` or `Dockerfile.prod` from the root of the project directory: | ||
```bash | ||
docker build -f Dockerfile.prod -t ims_api_image . | ||
``` | ||
|
||
1. Clone the repository and navigate to the project directory: | ||
2. Start the container using the image built and map it to port `8000` locally: | ||
```bash | ||
git clone [email protected]:ral-facilities/inventory-management-system-api.git | ||
cd inventory-management-system-api | ||
docker run -p 8000:8000 --name ims_api_container ims_api_image | ||
``` | ||
2. Create a Python virtual environment and activate it: | ||
or with values for the environment variables: | ||
```bash | ||
docker run -p 8000:8000 --name ims_api_container --env DATABASE__NAME=test-ims ims_api_image | ||
``` | ||
The microservice should now be running inside Docker at http://localhost:8000 and its Swagger UI could be accessed | ||
at http://localhost:8000/docs. | ||
|
||
### Local Setup | ||
|
||
Ensure that MongoDB is installed and running on your machine before proceeding. | ||
|
||
1. Create a Python virtual environment and activate it in the root of the project directory: | ||
```bash | ||
python -m venv venv | ||
source venv/bin/activate | ||
``` | ||
3. Install the required dependencies using pip: | ||
2. Install the required dependencies using pip: | ||
```bash | ||
pip install .[dev] | ||
``` | ||
4. Create a `.env` file using the `.env.example` file and modify the values inside accordingly. | ||
5. Create a `logging.ini` file using the `logging.example.ini` file and modify it accordingly. | ||
6. Ensure that MongoDB is running locally. If it's not installed, you can follow the official MongoDB installation guide | ||
for your operating system. | ||
7. Start the microservice using Uvicorn from the project directory: | ||
3. Create a `.env` file using the `.env.example` file and modify the values inside accordingly. | ||
4. Create a `logging.ini` file using the `logging.example.ini` file and modify it accordingly. | ||
5. Start the microservice using Uvicorn: | ||
```bash | ||
uvicorn inventory_management_system_api.main:app --log-config inventory_management_system_api/logging.ini --reload | ||
``` | ||
The microservice should now be running locally at http://localhost:8000. The Swagger UI can be accessed | ||
at http://localhost:8000/docs. | ||
8. To run the unit tests, run : | ||
6. To run the unit tests, run : | ||
```bash | ||
pytest test/unit/ | ||
``` | ||
9. To run the e2e tests, ensure that MongoDB is running locally and run: | ||
7. To run the e2e tests, run: | ||
```bash | ||
DATABASE__NAME="test-ims" pytest test/e2e/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Module for defining the database models for representing a System | ||
""" | ||
|
||
from typing import Optional | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from inventory_management_system_api.models.custom_object_id_data_types import CustomObjectIdField, StringObjectIdField | ||
|
||
|
||
class SystemIn(BaseModel): | ||
""" | ||
Input database model for a System | ||
""" | ||
|
||
name: str | ||
location: str | ||
owner: str | ||
importance: str | ||
description: str | ||
|
||
# Used for uniqueness checks (sanitised name) | ||
code: str | ||
# These two are purely for front end navigation | ||
path: str | ||
parent_path: str | ||
|
||
parent_id: Optional[CustomObjectIdField] = None | ||
|
||
|
||
class SystemOut(SystemIn): | ||
""" | ||
Output database model for a System | ||
""" | ||
|
||
id: StringObjectIdField = Field(alias="_id") | ||
parent_id: Optional[StringObjectIdField] = None | ||
|
||
# Required just for unit tests | ||
class Config: | ||
# pylint: disable=C0115 | ||
allow_population_by_field_name = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.