Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(WIP): api migration #397

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions .github/workflows/api-unit-test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: api check + unit test

on:
Expand Down Expand Up @@ -33,14 +30,12 @@ jobs:

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -U pip poetry
pip install virtualenv
make install-deps

- name: Lint with flake8
run: |
make lint
- name: Lint with ruff
run: make lint

- name: Unit test
run: |
make test
run: make test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# for database as json files
api/ossdb/users/data/
api/ossdb/projects/data/
Session.vim

.history
.idea
.vscode
Expand All @@ -6,6 +11,7 @@
*venv
.secrets
.secrets-env
.env

#vim
.*sw*
Expand Down
15 changes: 15 additions & 0 deletions api/.env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DEBUG = true

# ~~~~~~ database stuff below ~~~~~~

DB_HOST = localhost
DB_PORT = 5432

# those vars are used by the init script of postgresql service
# ONLY FOR DEV
POSTGRES_USER = "user"
POSTGRES_PASSWORD = "pass"
POSTGRES_DB = "ossdb"

# Auth options
TESTING = true
5 changes: 0 additions & 5 deletions api/.flake8

This file was deleted.

21 changes: 12 additions & 9 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
FROM python:3.8-slim-buster
FROM python:3.10.11-slim-buster

RUN apt update && apt install git gcc -y
ENV PYHTONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD ./requirements.txt ./
RUN pip install -r requirements.txt
WORKDIR /src

COPY . /code
COPY pyproject.toml poetry.lock ./
RUN pip install -U pip poetry

CMD [ "python", "manage.py", "run" ]
RUN poetry config virtualenvs.create false && poetry install

COPY . .

ENTRYPOINT ["/bin/sh", "-c"]

# TODO: Use gunicorn for prod and add a gunicorn config file that can be overriden by a ConfigMap
CMD ["python3 manage.py"]
61 changes: 43 additions & 18 deletions api/Makefile
Original file line number Diff line number Diff line change
@@ -1,39 +1,64 @@
.DEFAULT_GOAL=help

CONFIG_FILE=./config.txt
SHELL := /bin/bash
VENVPATH=venv
PYTHON=$(VENVPATH)/bin/python3
PY3=python3.11
PYTHON=$(VENVPATH)/bin/$(PY3)

# some dev targets for the code quality
RUFF=$(PYTHON) -m ruff app tests *.py
ISORT=$(PYTHON) -m isort app tests *.py
MYPY=$(PYTHON) -m mypy app tests *.py

venv: $(VENVPATH)/bin/activate
$(VENVPATH)/bin/activate: requirements.txt
test -d $(VENVPATH) || virtualenv -p python3 $(VENVPATH); \
$(VENVPATH)/bin/activate: poetry.lock
test -d $(VENVPATH) || virtualenv -p $(PY3) $(VENVPATH); \
. $(VENVPATH)/bin/activate; \
pip install -r requirements.txt; \
poetry install; \
touch $(VENVPATH)/bin/activate;

$(CONFIG_FILE):
echo "adding config file..."
cp example.config.txt $(CONFIG_FILE)

##install-deps: setup your dev environment
install-deps: venv $(CONFIG_FILE)
##install: setup your dev environment
install: venv

##run: run the api locally
run: install-deps
GOOGLE_APPLICATION_CREDENTIALS=.secrets/service-account.json $(PYTHON) manage.py run
run: install
$(PYTHON) manage.py run

##format: Reformat project code.
format: venv
${RUFF} --fix
${ISORT}

##lint: Lint project code and check it with mypy
lint: venv
$(PYTHON) -m flake8 . --show-source --statistics
${RUFF}
${ISORT} --check-only --df
${MYPY}

##test: test your code
test: install-deps lint
test: install lint
$(PYTHON) -m pytest

##clean: remove the venv and residuals
clean:
rm -rf $(VENVPATH)
rm -rf *.pyc
rm -rf {*/__pycache__,*/*__pycache__,*/*/*__pycache__}

##help: show help
help : Makefile
@sed -n 's/^##//p' $<

.PHONY : help venv install-deps test lint
##lock: sync dependency tree between pyproject.toml and poetry.lock
lock: poetry.lock
poetry lock --no-update

##docker-build: build the api docker image
docker-build:
docker build -t oss-api:latest -f ./Dockerfile .

import-db:
# ....

compose-run:
docker-compose run api

.PHONY : help venv run install-deps test lint docker-build
7 changes: 5 additions & 2 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# Oss Cameroon WebSite Backend


## Requirements

- python (>2.7,<=3.8)
- poetry (>1.4 recommended)
- make

## How to install/launch

```bash
# starts postgres server
docker-compose up -d pgadmin

make run
# this will set your virtual env `make venv`
# this will install dependencies `make install-deps`
# this will install dependencies `make install`
# and then run the backend...
```

Expand Down
11 changes: 0 additions & 11 deletions api/api.ini

This file was deleted.

22 changes: 0 additions & 22 deletions api/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +0,0 @@
from flask_restplus import Api as TheAPI
from flask import Blueprint, url_for

from app.main.controller.github_controller import api as github
from app.main.controller.twitter_controller import api as twitter

blueprint = Blueprint('api', __name__)


class Api(TheAPI):
@property
def specs_url(self):
return url_for(self.endpoint('specs'), _external=True)


api = Api(blueprint,
title='CAPARLEDEV RESTPLUS API ',
version='1.0',
description='The Backend of the platform CaParleDev.')

api.add_namespace(github)
api.add_namespace(twitter)
26 changes: 19 additions & 7 deletions api/app/main/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
from flask import Flask
from flask_cors import CORS
from typing import Any

from app.main.config import config_by_name
from fastapi import FastAPI

from app.main.controller.github_controller import github_router

def create_app(config_name):
app = Flask(__name__)
CORS(app)
app.config.from_object(config_by_name[config_name])

async def status() -> dict[str, Any]:
return {
"status": "ok",
"version": "1.0",
"apis": [
{"path": "api/v1"}
]
}

def create_app():
app = FastAPI(title='OssCameroon API', version="0.1")
app.include_router(github_router)

# Add middleware/event_handler and everything else
app.get("/")(status)

return app
37 changes: 0 additions & 37 deletions api/app/main/config.py

This file was deleted.

Loading
Loading