Skip to content

Commit

Permalink
skelton
Browse files Browse the repository at this point in the history
Signed-off-by: Lu Ken <[email protected]>
  • Loading branch information
kenplusplus committed Dec 27, 2024
0 parents commit ab86f2a
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": "0.2",
"ignorepaths": [
".github/",
".git/"
],
"ignorewords": [],
"useGitignore": true,
"language": "en",
"import": [],
"dictionarydefinitions": [],
"dictionaries": [],
"words": [
"cnap",
"Dockerfile"
]
}
29 changes: 29 additions & 0 deletions .github/workflows/doclint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Document Scan

on:
push:
branches:
- main
paths:
- '**/*.md'
pull_request:
paths:
- '**/*.md'
workflow_dispatch:

permissions:
contents: read

jobs:
scan_doc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: streetsidesoftware/cspell-action@v2
with:
files: |
**/*.md
*.md
config: .github/cspell.json
verbose: true
incremental_files_only: false
33 changes: 33 additions & 0 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Pylint Scan

on:
pull_request:
paths:
- 'src/**/*.py'
push:
branches:
- main
paths:
- 'src/**/*.py'
workflow_dispatch:

jobs:
codescan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.11
- name: Install dependencies
run: |
pip3 install --upgrade pip
pip3 install pylint
for f in $(find -type f -name "requirements.txt"); do
pip3 install -r $f
done
- name: Analyzing the python code
run: |
set -ex
export PYTHONPATH=$PWD/src/tia/
find . -type f -name "*.py" | xargs pylint
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# GenTrade Server


## Quick Start

- Run from source

```shell
export PYTHONPATH=<repo_dir>/src

Check warning on line 9 in README.md

View workflow job for this annotation

GitHub Actions / scan_doc

Unknown word (PYTHONPATH)
uvicorn gentrade-server.main:app --reload

Check warning on line 10 in README.md

View workflow job for this annotation

GitHub Actions / scan_doc

Unknown word (uvicorn)

Check warning on line 10 in README.md

View workflow job for this annotation

GitHub Actions / scan_doc

Unknown word (gentrade)
```
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fastapi[standard]
uvicorn
Empty file added src/gentrade-server/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions src/gentrade-server/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from fastapi import Security, HTTPException, status
from fastapi.security import APIKeyHeader
from .db import check_api_key, get_user_from_api_key

api_key_header = APIKeyHeader(name="X-API-Key")

def get_user(api_key_header: str = Security(api_key_header)):
if check_api_key(api_key_header):
user = get_user_from_api_key(api_key_header)
return user
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing or invalid API key"
)
19 changes: 19 additions & 0 deletions src/gentrade-server/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
api_keys = {
"e54d4431-5dab-474e-b71a-0db1fcb9e659": "7oDYjo3d9r58EJKYi5x4E8",
"5f0c7127-3be9-4488-b801-c7b6415b45e9": "mUP7PpTHmFAkxcQLWKMY8t"
}

users = {
"7oDYjo3d9r58EJKYi5x4E8": {
"name": "Bob"
},
"mUP7PpTHmFAkxcQLWKMY8t": {
"name": "Alice"
},
}

def check_api_key(api_key: str):
return api_key in api_keys

def get_user_from_api_key(api_key: str):
return users[api_keys[api_key]]
15 changes: 15 additions & 0 deletions src/gentrade-server/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from fastapi import FastAPI, Depends
from .routers import secure, public
from .auth import get_user

app = FastAPI()

app.include_router(
public.router,
prefix="/api/v1/public"
)
app.include_router(
secure.router,
prefix="/api/v1/secure",
dependencies=[Depends(get_user)]
)
Empty file.
8 changes: 8 additions & 0 deletions src/gentrade-server/routers/public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from fastapi import APIRouter


router = APIRouter()

@router.get("/")
async def get_testroute():
return "OK"
8 changes: 8 additions & 0 deletions src/gentrade-server/routers/secure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from fastapi import APIRouter, Depends
from ..auth import get_user

router = APIRouter()

@router.get("/")
async def get_testroute(user: dict = Depends(get_user)):
return user

0 comments on commit ab86f2a

Please sign in to comment.