mirror of
https://git.collinwebdesigns.de/oscar.krause/fastapi-dls.git
synced 2025-08-26 22:47:00 +08:00
Compare commits
4 Commits
637dc03eda
...
c6ca1cafb9
Author | SHA1 | Date | |
---|---|---|---|
|
c6ca1cafb9 | ||
|
607cca7655 | ||
|
6f9b6b9e5c | ||
|
d22e93020c |
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Minimal Delegated License Service (DLS).
|
Minimal Delegated License Service (DLS).
|
||||||
|
|
||||||
> [!note]
|
> [!note] Compatibility
|
||||||
> Compatibility tested with official NLS 2.0.1, 2.1.0, 3.1.0, 3.3.1, 3.4.0. For Driver compatibility
|
> Compatibility tested with official NLS 2.0.1, 2.1.0, 3.1.0, 3.3.1, 3.4.0. For Driver compatibility
|
||||||
> see [compatibility matrix](#vgpu-software-compatibility-matrix).
|
> see [compatibility matrix](#vgpu-software-compatibility-matrix).
|
||||||
|
|
||||||
@ -402,6 +402,9 @@ Continue [here](#unraid-guest) for docker guest setup.
|
|||||||
|
|
||||||
Tanks to [@mrzenc](https://github.com/mrzenc) for [fastapi-dls-nixos](https://github.com/mrzenc/fastapi-dls-nixos).
|
Tanks to [@mrzenc](https://github.com/mrzenc) for [fastapi-dls-nixos](https://github.com/mrzenc/fastapi-dls-nixos).
|
||||||
|
|
||||||
|
> [!note] Native NixOS-Package
|
||||||
|
> There is a [pull request](https://github.com/NixOS/nixpkgs/pull/358647) which adds fastapi-dls into nixpkgs.
|
||||||
|
|
||||||
## Let's Encrypt Certificate (optional)
|
## Let's Encrypt Certificate (optional)
|
||||||
|
|
||||||
If you're using installation via docker, you can use `traefik`. Please refer to their documentation.
|
If you're using installation via docker, you can use `traefik`. Please refer to their documentation.
|
||||||
|
27
app/main.py
27
app/main.py
@ -1,11 +1,12 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import os.path
|
||||||
from base64 import b64encode as b64enc
|
from base64 import b64encode as b64enc
|
||||||
from calendar import timegm
|
from calendar import timegm
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from datetime import datetime, timedelta, UTC
|
from datetime import datetime, timedelta, UTC
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
from json import loads as json_loads
|
from json import loads as json_loads
|
||||||
from os import getenv as env
|
from os import getenv as env, listdir
|
||||||
from os.path import join, dirname
|
from os.path import join, dirname
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ from dateutil.relativedelta import relativedelta
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.requests import Request
|
from fastapi.requests import Request
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
from jose import jws, jwk, jwt, JWTError
|
from jose import jws, jwk, jwt, JWTError
|
||||||
from jose.constants import ALGORITHMS
|
from jose.constants import ALGORITHMS
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
@ -50,6 +52,7 @@ LEASE_RENEWAL_PERIOD = float(env('LEASE_RENEWAL_PERIOD', 0.15))
|
|||||||
LEASE_RENEWAL_DELTA = timedelta(days=int(env('LEASE_EXPIRE_DAYS', 90)), hours=int(env('LEASE_EXPIRE_HOURS', 0)))
|
LEASE_RENEWAL_DELTA = timedelta(days=int(env('LEASE_EXPIRE_DAYS', 90)), hours=int(env('LEASE_EXPIRE_HOURS', 0)))
|
||||||
CLIENT_TOKEN_EXPIRE_DELTA = relativedelta(years=12)
|
CLIENT_TOKEN_EXPIRE_DELTA = relativedelta(years=12)
|
||||||
CORS_ORIGINS = str(env('CORS_ORIGINS', '')).split(',') if (env('CORS_ORIGINS')) else [f'https://{DLS_URL}']
|
CORS_ORIGINS = str(env('CORS_ORIGINS', '')).split(',') if (env('CORS_ORIGINS')) else [f'https://{DLS_URL}']
|
||||||
|
DRIVERS_DIR = env('DRIVERS_DIR', None)
|
||||||
|
|
||||||
jwt_encode_key = jwk.construct(INSTANCE_KEY_RSA.pem(), algorithm=ALGORITHMS.RS256)
|
jwt_encode_key = jwk.construct(INSTANCE_KEY_RSA.pem(), algorithm=ALGORITHMS.RS256)
|
||||||
jwt_decode_key = jwk.construct(INSTANCE_KEY_PUB.pem(), algorithm=ALGORITHMS.RS256)
|
jwt_decode_key = jwk.construct(INSTANCE_KEY_PUB.pem(), algorithm=ALGORITHMS.RS256)
|
||||||
@ -88,6 +91,9 @@ async def lifespan(_: FastAPI):
|
|||||||
config = dict(openapi_url=None, docs_url=None, redoc_url=None) # dict(openapi_url='/-/openapi.json', docs_url='/-/docs', redoc_url='/-/redoc')
|
config = dict(openapi_url=None, docs_url=None, redoc_url=None) # dict(openapi_url='/-/openapi.json', docs_url='/-/docs', redoc_url='/-/redoc')
|
||||||
app = FastAPI(title='FastAPI-DLS', description='Minimal Delegated License Service (DLS).', version=VERSION, lifespan=lifespan, **config)
|
app = FastAPI(title='FastAPI-DLS', description='Minimal Delegated License Service (DLS).', version=VERSION, lifespan=lifespan, **config)
|
||||||
|
|
||||||
|
if DRIVERS_DIR is not None:
|
||||||
|
app.mount('/-/static-drivers', StaticFiles(directory=str(DRIVERS_DIR), html=False), name='drivers')
|
||||||
|
|
||||||
app.debug = DEBUG
|
app.debug = DEBUG
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
@ -186,6 +192,25 @@ async def _manage(request: Request):
|
|||||||
return HTMLr(response)
|
return HTMLr(response)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get('/-/drivers/{directory:path}', summary='* List drivers directory')
|
||||||
|
async def _drivers(request: Request, directory: str | None):
|
||||||
|
if DRIVERS_DIR is None:
|
||||||
|
return Response(status_code=404, content=f'Variable "DRIVERS_DIR" not set.')
|
||||||
|
|
||||||
|
path = os.path.join(DRIVERS_DIR, directory)
|
||||||
|
|
||||||
|
if not os.path.exists(path) and not os.path.isfile(path):
|
||||||
|
return Response(status_code=404, content=f'Resource "{path}" not found!')
|
||||||
|
|
||||||
|
content = [{
|
||||||
|
"type": "file" if os.path.isfile(f'{path}/{_}') else "folder" if os.path.isdir(f'{path}/{_}') else "unknown",
|
||||||
|
"name": _,
|
||||||
|
"link": f'/-/static-drivers/{directory}{_}',
|
||||||
|
} for _ in listdir(path)]
|
||||||
|
|
||||||
|
return JSONr({"directory": path, "content": content})
|
||||||
|
|
||||||
|
|
||||||
@app.get('/-/origins', summary='* Origins')
|
@app.get('/-/origins', summary='* Origins')
|
||||||
async def _origins(request: Request, leases: bool = False):
|
async def _origins(request: Request, leases: bool = False):
|
||||||
session = sessionmaker(bind=db)()
|
session = sessionmaker(bind=db)()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user