📝 Reflect latest changes in getting started docs
This commit is contained in:
parent
82fe75b0c7
commit
3165243755
1 changed files with 43 additions and 11 deletions
54
README.md
54
README.md
|
|
@ -1,31 +1,33 @@
|
||||||
# OhMyAPI
|
# OhMyAPI
|
||||||
|
|
||||||
> Django-flavored application scaffolding and management layer around FastAPI+TortoiseORM.
|
> Think: Micro-Django, but API-first, less clunky and 100% async.
|
||||||
|
|
||||||
OhMyAPI is a Django-flavored web-application scaffolding framework and management layer.
|
OhMyAPI is a Django-flavored web-application scaffolding framework and management layer.
|
||||||
Built around FastAPI and TortoiseORM, it 100% async.
|
Built around FastAPI and TortoiseORM, it is 100% async.
|
||||||
It is *blazingly* fast and comes with batteries included.
|
|
||||||
|
|
||||||
Features:
|
It is ***blazingly fast***, ***fun*** to use and comes with ***batteries included***!
|
||||||
|
|
||||||
|
**Features**
|
||||||
|
|
||||||
- Django-like project-layout and -structure
|
- Django-like project-layout and -structure
|
||||||
- Django-like prject-level settings.py
|
- Django-like prject-level settings.py
|
||||||
- Django-like models via TortoiseORM
|
- Django-like models via TortoiseORM
|
||||||
- Django-like `Model.Meta` class for model configuration
|
- Django-like `Model.Meta` class for model configuration
|
||||||
- Easily convert your models to `pydantic` models via `Model.Schema`
|
- Easily convert your query results to `pydantic` models via `Model.Schema`
|
||||||
- Django-like migrations (makemigrations & migrate) via Aerich
|
- Django-like migrations (makemigrations & migrate) via Aerich
|
||||||
- Django-like CLI for interfacing with your projects (startproject, startapp, shell, serve, etc)
|
- Django-like CLI tooling (`startproject`, `startapp`, `shell`, `serve`, etc)
|
||||||
- Various optional builtin apps
|
- Various optional builtin apps you can hook into your project
|
||||||
- Highly configurable and customizable
|
- Highly configurable and customizable
|
||||||
- 100% async
|
- 100% async
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
**Creating a Project**
|
**Creating a Project**
|
||||||
|
|
||||||
```
|
```
|
||||||
pip install ohmyapi
|
pip install ohmyapi
|
||||||
pip install 'ohmyapi[auth]' # optionally add PyJWT and all necessary crypto deps
|
|
||||||
ohmyapi startproject myproject
|
ohmyapi startproject myproject
|
||||||
cd myproject
|
cd myproject
|
||||||
```
|
```
|
||||||
|
|
@ -56,7 +58,7 @@ Create a new app by:
|
||||||
ohmyapi startapp myapp
|
ohmyapi startapp myapp
|
||||||
```
|
```
|
||||||
|
|
||||||
This will lead to the following directory structure:
|
This will create the following directory structure:
|
||||||
|
|
||||||
```
|
```
|
||||||
myproject/
|
myproject/
|
||||||
|
|
@ -71,6 +73,8 @@ myproject/
|
||||||
|
|
||||||
Add 'myapp' to your `INSTALLED_APPS` in `settings.py`.
|
Add 'myapp' to your `INSTALLED_APPS` in `settings.py`.
|
||||||
|
|
||||||
|
### Models
|
||||||
|
|
||||||
Write your first model in `myapp/models.py`:
|
Write your first model in `myapp/models.py`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
@ -84,11 +88,13 @@ class Person(Model):
|
||||||
age: int = field.IntField(min=0)
|
age: int = field.IntField(min=0)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### API Routes
|
||||||
|
|
||||||
Next, create your endpoints in `myapp/routes.py`:
|
Next, create your endpoints in `myapp/routes.py`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from fastapi import APIRouter, HTTPException
|
from ohmyapi.router import APIRouter, HTTPException
|
||||||
from tortoise.exceptions import DoesNotExist
|
from ohmyapi.db.exceptions import DoesNotExist
|
||||||
|
|
||||||
from .models import Person
|
from .models import Person
|
||||||
|
|
||||||
|
|
@ -175,9 +181,35 @@ INSTALLED_APPS = [
|
||||||
JWT_SECRET = "t0ps3cr3t"
|
JWT_SECRET = "t0ps3cr3t"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
After restarting your project you will have access to the `ohmyapi_auth` app.
|
||||||
|
It comes with a `User` and `Group` model, as well as endpoints for JWT auth.
|
||||||
|
|
||||||
Create a super-user:
|
Create a super-user:
|
||||||
|
|
||||||
```
|
```
|
||||||
ohmyapi createsuperuser
|
ohmyapi createsuperuser
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Permissions
|
||||||
|
|
||||||
|
### API-Level
|
||||||
|
|
||||||
|
Use FastAPI's `Depends` pattern to implement API-level access-control.
|
||||||
|
|
||||||
|
|
||||||
|
In your `routes.py`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from ohmyapi.router import APIRouter, Depends
|
||||||
|
|
||||||
|
from ohmyapi_auth.models import User
|
||||||
|
from ohmyapi_auth.permissions import require_authenticated
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/myapp")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/")
|
||||||
|
def must_be_authenticated(user: User = Depends(require_authenticated)):
|
||||||
|
return {"user": user}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue