Skip to content

Commit 937d0fd

Browse files
authored
docs: getting started
adds minimal example to docs
1 parent c3676b7 commit 937d0fd

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

sftkit/README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,63 @@
33
A general purpose collection of base building blocks and utilities to make building
44
python applications on the basis of postgresql (asyncpg) + fastapi a breeze.
55

6+
## Getting Started
67
To get started simply run
78

89
```bash
910
pip install sftkit
1011
```
1112

13+
A basic server could look like this
14+
```python
15+
import asyncio
16+
from dataclasses import dataclass
17+
18+
from sftkit.http import Server, HTTPServerConfig
19+
from fastapi import APIRouter
20+
21+
22+
config = HTTPServerConfig(base_url="/api/v1", port=8074, host="127.0.0.1")
23+
24+
router = APIRouter(
25+
responses={404: {"description": "not found"}},
26+
)
27+
28+
@router.get("/ping")
29+
async def ping():
30+
return "pong"
31+
32+
33+
@dataclass
34+
class Context:
35+
config: HTTPServerConfig
36+
37+
38+
class Api:
39+
def __init__(self, config: HTTPServerConfig = config):
40+
self.config = config
41+
self.server = Server(
42+
title="<your title>",
43+
config=config,
44+
license_name="<your license>",
45+
version="0.1.0"
46+
47+
)
48+
self.server.add_router(router)
49+
50+
async def run(self):
51+
context = Context(config=self.config)
52+
await self.server.run(context)
53+
54+
if __name__ == "__main__":
55+
server = Api()
56+
asyncio.run(server.run())
57+
```
58+
59+
Copy the code to `main.py` and run the server using `python main.py`.
60+
61+
You can ping the server at [http://127.0.0.1:8074/api/v1/ping](http://127.0.0.1:8074/api/v1/ping) or inspect the API specification at [http://127.0.0.1:8074/api/v1/docs](http://127.0.0.1:8074/api/v1/docs).
62+
1263
## Usage
1364

1465

@@ -26,4 +77,4 @@ Create new migrations via
2677

2778
```bash
2879
sftkit create-migration <name>
29-
```
80+
```

0 commit comments

Comments
 (0)