Skip to content

Commit

Permalink
Add server rules endpoint (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux authored Jul 21, 2024
1 parent 0d56cb7 commit 1430355
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# comma-separated value of available rooms
ROOMS=QGIS,QField,Geotribu

# server rules
RULES="Free and open gischat instance, let's chat with your fellow GIS mates using this server, in a spirit of respect and tolerance"
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ services:
image: gounux/gischat:latest
container_name: gischat-app
environment:
- ROOMS=LivingRoom,Kitchen,Garden
- ROOMS="LivingRoom,Kitchen,Garden"
- RULES="Be kind and nice to this wonderful world"
ports:
- 8000:8000
```

`ROOMS` environment variable is a comma-separated list of strings which represent the available chat rooms
`ROOMS` environment variable is a comma-separated list of strings which represent the available chat rooms
`RULES` environment variable describres the instance's rules


- Launch the app using `compose` :

Expand Down
9 changes: 7 additions & 2 deletions gischat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from fastapi.responses import HTMLResponse
from starlette.websockets import WebSocketDisconnect

from gischat.models import MessageModel, StatusModel, VersionModel
from gischat.models import MessageModel, RulesModel, StatusModel, VersionModel
from gischat.utils import get_poetry_version
from gischat.ws_html import ws_html

Expand Down Expand Up @@ -98,11 +98,16 @@ async def get_status() -> StatusModel:
)


@app.get("/rooms")
@app.get("/rooms", response_model=list[str])
async def get_rooms() -> list[str]:
return available_rooms()


@app.get("/rules", response_model=RulesModel)
async def get_rules() -> RulesModel:
return RulesModel(rules=os.environ.get("RULES", "YOLO"))


@app.put("/room/{room}/message", response_model=MessageModel)
async def put_message(room: str, message: MessageModel) -> MessageModel:
if room not in notifier.connections.keys():
Expand Down
4 changes: 4 additions & 0 deletions gischat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class StatusModel(BaseModel):
rooms: list[RoomStatusModel]


class RulesModel(BaseModel):
rules: str


class MessageModel(BaseModel):
message: str
author: str
Expand Down
87 changes: 86 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ toml = "^0.10.2"
[tool.poetry.group.dev.dependencies]
pre-commit = "^3.7.1"
pytest = "^8.2.2"
pytest-cov = "^5.0.0"
black = "^24.4.2"
isort = "^5.13.2"
flake8 = "^7.1.0"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from fastapi.testclient import TestClient

from gischat.utils import get_poetry_version
from tests import TEST_RULES
from tests.conftest import test_rooms


Expand Down Expand Up @@ -30,3 +31,9 @@ def test_rooms(client: TestClient, room: str):
response = client.get("/rooms")
assert response.status_code == 200
assert room in response.json()


def test_rules(client: TestClient):
response = client.get("/rules")
assert response.status_code == 200
assert response.json()["rules"] == TEST_RULES

0 comments on commit 1430355

Please sign in to comment.