-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
110 changed files
with
498 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
## Routing in Frontik applications | ||
|
||
On application start, frontik import all modules from {app_module}.pages so that any controller should be located there. | ||
We use fastapi routing, read [these docs](https://fastapi.tiangolo.com/reference/apirouter/?h=apirouter) for details. A small important difference - you must inherit `frontik.routing.FastAPIRouter` instead `fastapi.APIRouter`. And use `from frontik.routing import router`, if you need default router. | ||
|
||
example: | ||
|
||
Controller example: | ||
```python | ||
from frontik.routing import router | ||
|
||
@router.get("/users/me") | ||
async def read_user_me(): | ||
return {"username": "fakecurrentuser"} | ||
|
||
``` | ||
|
||
### Deprecated | ||
|
||
Page generation logic with (pre/post)processors and finish group from previous versions is temporarily supported. You need to add `cls=PageHandler` arg to route: | ||
|
||
```python | ||
from frontik.routing import plain_router | ||
from frontik.handler import PageHandler | ||
|
||
@router.get('/simple_page', cls=PageHandler) # or .post .put .delete .head | ||
|
||
@plain_router.get('/simple_page', cls=PageHandler) # or .post .put .delete .head | ||
async def get_page(): | ||
... | ||
``` | ||
|
||
First argument path should be exact string for url matching. | ||
If you need regex with path params use `from frontik.routing import regex_router` | ||
|
||
Argument `cls=PageHandler` is required for legacy compatibility, it defines which class will be used to create the handler object. | ||
The handler object can be accessed via `request.state.handler` | ||
|
||
You can use 'dependencies' functions in router or method arguments, see https://fastapi.tiangolo.com/tutorial/dependencies/ and https://fastapi.tiangolo.com/tutorial/bigger-applications/#another-module-with-apirouter for details | ||
If you need regex with path params use `from frontik.routing import regex_router`. The handler object can be accessed via `request.state.handler` | ||
You can use fastapi dependencies functions in router, see https://fastapi.tiangolo.com/tutorial/dependencies/ and https://fastapi.tiangolo.com/tutorial/bigger-applications/#another-module-with-apirouter for details | ||
|
||
Example with dependencies: | ||
|
||
```python | ||
from frontik.routing import router | ||
from frontik.routing import plain_router | ||
from frontik.handler import PageHandler, get_current_handler | ||
from fastapi import Depends | ||
|
||
@router.get('/simple_page', cls=PageHandler, dependencies=[Depends(my_foo)]) | ||
|
||
@plain_router.get('/simple_page', cls=PageHandler, dependencies=[Depends(my_foo)]) | ||
async def get_page(handler=get_current_handler()): | ||
... | ||
``` | ||
|
||
You can create your own router object by extending `frontik.routing.FrontikRouter` and `frontik.routing.FrontikRegexRouter` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from frontik.handler import PageHandler, get_current_handler | ||
from frontik.routing import router | ||
from frontik.routing import plain_router | ||
|
||
|
||
@router.get('/tpl', cls=PageHandler) | ||
@plain_router.get('/tpl', cls=PageHandler) | ||
def get_page(handler: PageHandler = get_current_handler()) -> None: | ||
handler.json.put({'text': 'Hello, world!'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
from fastapi import Request | ||
|
||
from frontik.handler import PageHandler, get_current_handler | ||
from frontik.routing import router | ||
from frontik.routing import plain_router | ||
|
||
|
||
@router.get('/tpl', cls=PageHandler) | ||
@plain_router.get('/tpl', cls=PageHandler) | ||
def get_page(request: Request, handler: PageHandler = get_current_handler()) -> None: | ||
handler.set_template('main.html') # This template is located in the `templates` folder | ||
handler.json.put(handler.get_url(request.headers.get('host', ''), '/example')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.