Skip to content

Commit

Permalink
Expand to stub endpoints, basic static bearer token auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinna Kiisuo committed Aug 21, 2023
1 parent 6d58ac2 commit 70f676f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 88
52 changes: 47 additions & 5 deletions borg_lockservice/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
from absl import flags
import os, sys
import os
import sys
import uvicorn

from fastapi import FastAPI
from typing import Annotated

from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import HTTPBearer


FLAGS = flags.FLAGS
app = FastAPI()
auth = HTTPBearer()

PREFIX = "BORG_LOCKSERVICE"

flags.DEFINE_string(
"token",
os.getenv(f"{PREFIX}_TOKEN", None),
"Bearer token required to access the API.",
)

flags.DEFINE_string(
"host",
os.getenv(f"{PREFIX}_HOST", "0.0.0.0"),
Expand All @@ -28,16 +40,46 @@
)


FLAGS(sys.argv)
BEARER_TOKEN = FLAGS.token


@app.get("/")
async def root():
return {
'message': f"uwu :3",
"message": PREFIX,
}


@app.get("/lock/{repo}")
async def lock(
repo: str, token: Annotated[str, Depends(auth)], timeout_minutes: int = 60
):
if token.credentials == BEARER_TOKEN:
return {"message": f"Totally locked {repo} for {timeout_minutes}m"}
else:
raise HTTPException(status_code=403)


@app.get("/unlock/{repo}")
async def unlock(repo: str):
return {"message": "Not yet implemented"}


@app.get("/status/{repo}")
async def status(repo: str):
return {"message": "Not yet implemented"}


@app.get("/list")
async def list_locks():
return {"message": "Not yet implemented"}


def run():
FLAGS(sys.argv)
uvicorn.run("borg_lockservice:app", host=FLAGS.host, port=FLAGS.port, reload=FLAGS.dev)
uvicorn.run(
"borg_lockservice:app", host=FLAGS.host, port=FLAGS.port, reload=FLAGS.dev
)


if __name__ == "__main__":
Expand Down

0 comments on commit 70f676f

Please sign in to comment.