Skip to content

Commit

Permalink
Merge pull request #2 from inab/feature/download-endpoint
Browse files Browse the repository at this point in the history
Feature/download endpoint
  • Loading branch information
EvaMart authored Oct 15, 2024
2 parents 800e234 + de6ab6c commit b9bd4a5
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 89 deletions.
115 changes: 49 additions & 66 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,66 +1,49 @@
name: CI/CD Pipeline

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.10.6

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
pip install -r requirements.txt
- name: Run tests
run: |
pytest -v app/tests
build:
runs-on: ubuntu-latest
needs: test

steps:
- uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ghcr.io/inab/observatory-api:latest
name: Build and Publish Docker on Tag

on:
# Trigger the workflow when a new tag is pushed
push:
tags:
- 'v*' # Match tags like v1.0.0, v2.1.0, etc.

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Build the Docker image with the Git tag
- name: Build and tag Docker image
run: |
TAG_NAME=${GITHUB_REF#refs/tags/} # Extract the tag name (e.g., v1.0.0)
docker build -t ghcr.io/inab/observatory-api:${TAG_NAME} .
docker tag ghcr.io/inab/observatory-api:${TAG_NAME} ghcr.io/inab/observatory-api:latest
# Push the Docker image to the registry
- name: Push Docker image
run: |
TAG_NAME=${GITHUB_REF#refs/tags/} # Extract the tag name (e.g., v1.0.0)
docker push ghcr.io/inab/observatory-api:${TAG_NAME}
docker push ghcr.io/inab/observatory-api:latest
31 changes: 31 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI/CD Pipeline

on:
push:
branches:
- '**'
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.10.6

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
pip install -r requirements.txt
- name: Run tests
run: |
pytest -v app/tests
2 changes: 2 additions & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2.0.0
2.1.0
12 changes: 7 additions & 5 deletions app/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import wraps
import warnings
import time
from pathlib import Path

from app.helpers.EDAM_forFE import EDAMDict

Expand Down Expand Up @@ -930,8 +931,9 @@ def prepare_sources_labels(tool):
return(tool)



################
# Database connection
################

def get_version():
# Get the absolute path to the VERSION file
version_path = Path(__file__).parent.parent.parent / "VERSION"

# Read the VERSION file
return version_path.read_text().strip()
32 changes: 32 additions & 0 deletions app/routes/downloads.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from fastapi import HTTPException, APIRouter
from pydantic import BaseModel
import requests

router = APIRouter()

class URLRequest(BaseModel):
url: str

@router.post("/download-content/", tags=["downloads"])
async def download_content(request: URLRequest):
"""
Download content from a specified URL.
This endpoint accepts a URL in the request body and returns the HTML content
of the specified URL. If the URL is invalid or the content cannot be downloaded,
it returns a 400 status code with an error message.
Args:
request (URLRequest): A JSON object containing the URL to be downloaded.
Returns:
dict: A dictionary containing the original URL and the downloaded content.
"""
try:
print(request.url)
response = requests.get(request.url)
response.raise_for_status()
except requests.exceptions.RequestException as e:
raise HTTPException(status_code=400, detail=f"Error downloading content: {e}")

return {"url": request.url, "content": response.text}
11 changes: 9 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.staticfiles import StaticFiles
from starlette.responses import FileResponse
from app.routes import edam, spdx, stats, metadata, fair_evaluation, search, tool
from app.routes import edam, spdx, stats, metadata, fair_evaluation, search, tool, downloads
from app.helpers.utils import get_version

tags_metadata = [
{
Expand All @@ -31,12 +32,17 @@
"name": "search",
"description": "Search related endpoints",
},
{
"name": "downloads",
"description": "Download related endpoints",
}
]

version = get_version()
app = FastAPI(
title="Software Observatory API",
description="This is the API for the Software Observatory at [OpenEBench](https://openebench.bsc.es)",
version="2.0.0",
version=version,
contact={
"name": "OpenEBench",
"url": "https://openebench.bsc.es/",
Expand Down Expand Up @@ -67,6 +73,7 @@

app.include_router(fair_evaluation.router, prefix="/fair")
app.include_router(tool.router, prefix="/tool")
app.include_router(downloads.router, prefix="/downloads")
app.include_router(search.router, prefix="")


Expand Down
12 changes: 12 additions & 0 deletions mongo-compose/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Ready-to-use database

To facilitate the testing of the Observatory API, a docker-compose to deploy and populate a full and ready-to-use database is available (`mongo-compose/docker-compose.yml`).

The components necessary for this deployment are `mongodb`, `mongo-total` and `mongo-seed`. The configuration of the latter two can be found in the `mongo-compose` directory.

To deploy the database:

```
sudo docker login registry.gitlab.bsc.es
sudo docker-compose up --remove-orphans --force-recreate --renew-anon-volumes
```
19 changes: 3 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,9 @@ python3 main.py

The API will be available at `http://localhost:3500`.

### Ready-to-use database
### Versioning

To facilitate the testing of the Observatory API, a docker-compose to deploy and populate a full and ready-to-use database is available (`mongo-compose/docker-compose.yml`).

The components necessary for this deployment are `mongodb`, `mongo-total` and `mongo-seed`. The configuration of the latter two can be found in the `mongo-compose` directory.

To deploy the database:

```
sudo docker login registry.gitlab.bsc.es
sudo docker-compose up --remove-orphans --force-recreate --renew-anon-volumes
```
After a pull request is merged, the version of the API should be updated. This is done by adding the new version to `VERSION` and then running the script `update_version.sh`. This script will update the version to the last version in `VERSION` file and commit the changes.

### Collections

Expand Down Expand Up @@ -79,11 +70,7 @@ This documentation is automatically generated by FastAPI and is based on the Ope

The API is deployed in the BSC's infrastructure. The deployment is done using docker and docker-compose.

To build the docker image:

```
sudo docker build -t observatory-api .
```
The docker image is built as part of the CI/CD pipeline. It is triggered by pushing a tag to the repository.

Notice that the API is accessible at a subdomain of the OpenEBench platform. The API is available at [https://observatory.openebench.bsc.es/api](https://observatory.openebench.bsc.es/api). In development, the API is available directly at `http://localhost:3500`.

Expand Down
43 changes: 43 additions & 0 deletions update_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Fail script if any command fails
set -e

# Ensure that we're on the main branch and up to date
git checkout main
git pull origin main

# Read the latest version from the VERSION file
NEW_VERSION=$(cat VERSION)

echo "Validating version format..."

# Check if the version follows Semantic Versioning (MAJOR.MINOR.PATCH)
if [[ ! "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "ERROR: The version '$NEW_VERSION' in the VERSION file does not follow Semantic Versioning (e.g., 1.0.0)."
exit 1
fi

# Check if the version needs a "v" prefix, and add it if not present
if [[ "$NEW_VERSION" != v* ]]; then
NEW_VERSION="v$NEW_VERSION"
echo "Prepending 'v' to version. New version is $NEW_VERSION"
fi

# Check if the version already has a Git tag
echo "Checking if version $NEW_VERSION already has a tag..."
if git rev-parse "$NEW_VERSION" >/dev/null 2>&1; then
echo "ERROR: Version $NEW_VERSION already has a tag!"
echo "It looks like you forgot to update the VERSION file."
echo "Please update the VERSION file to the new version."
exit 1
fi

# If no existing tag, proceed with committing and pushing the version update
echo "Version $NEW_VERSION does not have a tag. Proceeding with the update."

git tag "$NEW_VERSION"

git push origin main "$NEW_VERSION"

echo "Version updated to $NEW_VERSION and pushed to main branch."

0 comments on commit b9bd4a5

Please sign in to comment.