Skip to content

Commit

Permalink
Merge dev into main - v0.2.0
Browse files Browse the repository at this point in the history
Merge dev into main - v0.2.0
  • Loading branch information
MLBZ521 committed Nov 18, 2022
2 parents edd4506 + cd8a337 commit 3bdd34e
Show file tree
Hide file tree
Showing 83 changed files with 3,474 additions and 3,498 deletions.
137 changes: 137 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# .gitignore itself
# .gitignore

# Visual Studio Code
.vscode

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-shm
db.sqlite3-wal
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
File renamed without changes.
115 changes: 115 additions & 0 deletions PkgBot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/local/autopkg/python

import multiprocessing
import sys

# import asyncio
import secure
import uvicorn

from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware

from tortoise.contrib.fastapi import register_tortoise

from pkgbot import config, settings

config = config.load_config(cli_args=tuple(sys.argv[1:]))

from pkgbot.utilities import common as utility
from pkgbot.db import models
from pkgbot import api


log = utility.log

app = FastAPI(
title="PkgBot API",
description="A framework to manage software packaging, testing, and promoting from a "
"development to production environment.",
version="0.2.0",
openapi_tags=settings.api.tags_metadata,
docs_url="/api"
)

app.mount("/static", StaticFiles(directory="/Library/AutoPkg/PkgBot/pkgbot/static"), name="static")
app.include_router(api.views.router)
app.include_router(api.auth.router)
app.include_router(api.autopkg.router)
app.include_router(api.package.router)
app.include_router(api.recipe.router)
app.include_router(api.bot.router)
app.include_router(api.build_msg.router)
app.include_router(api.send_msg.router)
app.include_router(api.user.router)

register_tortoise(
app,
config = settings.db.TORTOISE_CONFIG,
generate_schemas = True,
add_exception_handlers = True
)

# Add an exception handler to the app instance
# Used for the login/auth logic for the HTTP views
app.add_exception_handler(api.auth.NotAuthenticatedException, api.auth.exc_handler)
api.auth.login_manager.useRequest(app)

if config.PkgBot.get("enable_ssl"):

# Enforces that all incoming requests must be https.
app.add_middleware(HTTPSRedirectMiddleware)
server = secure.Server().set("Secure")
hsts = secure.StrictTransportSecurity().include_subdomains().preload().max_age(2592000)
cache_value = secure.CacheControl().must_revalidate()
secure_headers = secure.Secure(
server=server,
# csp=csp,
hsts=hsts,
# referrer=referrer,
# permissions=permissions_value,
cache=cache_value,
)

@app.middleware("http")
async def set_secure_headers(request, call_next):
response = await call_next(request)
secure_headers.framework.fastapi(response)
return response


async def number_of_workers():
number_of_threads = (multiprocessing.cpu_count() * 2) - 1
log.debug(f"Number of workers: {number_of_threads}")
return number_of_threads


@app.on_event("startup")
async def startup_event():

pkgbot_admins = config.PkgBot.get("Admins")

for admin in pkgbot_admins:
user_object = models.PkgBotAdmin_In(
username = admin,
slack_id = pkgbot_admins.get(admin),
full_admin = True
)
await api.user.create_or_update_user(user_object)


if __name__ == "__main__":

uvicorn.run(
"PkgBot:app",
reload = config.PkgBot.get("keep_alive"),
host = config.PkgBot.get("host"),
port = config.PkgBot.get("port"),
log_config = config.PkgBot.get("log_config"),
log_level = config.PkgBot.get("uvicorn_log_level"),
# workers = asyncio.run(number_of_workers()),
ssl_keyfile = config.PkgBot.get("ssl_keyfile"),
ssl_certfile = config.PkgBot.get("ssl_certfile")
)
Loading

0 comments on commit 3bdd34e

Please sign in to comment.