Skip to content
This repository was archived by the owner on May 13, 2023. It is now read-only.

Commit 35d64bf

Browse files
committed
Merge branch 'master' of github.com:innonymous/api-server
2 parents 4f7b3a5 + 3b0422f commit 35d64bf

File tree

11 files changed

+63
-17
lines changed

11 files changed

+63
-17
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
API_AMQP_URL=
22
API_DATABASE_URL=
33
API_KEY=
4+
5+
# Optional.
6+
#API_BIND_URL=0.0.0.0:8000
7+
#API_ROOT_PATH=.
8+
#API_LOGS_PATH=./logs

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
logs
2+
3+
env
14
.env
25

36
.idea

config/gunicorn.conf.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
from os import cpu_count
1+
from os import cpu_count, environ
2+
from pathlib import Path
23

3-
bind = '0.0.0.0:8000'
4+
from dotenv import load_dotenv
5+
6+
load_dotenv()
7+
8+
bind = environ.get('API_BIND_URL', '0.0.0.0:8000')
49

510
workers = cpu_count()
611
worker_class = 'uvicorn.workers.UvicornH11Worker'
12+
13+
_logs_path = Path(environ.get('API_LOGS_PATH', './logs'))
14+
_logs_path.mkdir(parents=True, exist_ok=True)
15+
accesslog = str(_logs_path / 'access.log')
16+
errorlog = str(_logs_path / 'error.log')

entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ set -e;
77
./env/bin/alembic --config ./config/alembic.ini upgrade head;
88

99
# Start server.
10-
./env/bin/gunicorn --config ./config/gunicorn.conf.py innonymous.api:app;
10+
./env/bin/gunicorn --config ./config/gunicorn.conf.py innonymous.api:app "$*";

innonymous/api/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
from innonymous.api.utils.auth import JWTAuthenticator
1515
from innonymous.database import DatabaseEngine
1616

17-
app = FastAPI(version='0.0.1', title='InnonymousApi')
17+
settings = APISettings()
18+
19+
app = FastAPI(
20+
version='0.0.2', title='InnonymousApi', root_path=settings.root_path or ''
21+
)
1822
app.add_middleware(
1923
CORSMiddleware,
2024
allow_credentials=True,
@@ -23,7 +27,6 @@
2327
allow_headers=["*"]
2428
)
2529

26-
settings = APISettings()
2730
captcha = Captcha(settings.key)
2831
mq = MessageQueue(settings.amqp_url)
2932
db_engine = DatabaseEngine(settings.database_url)

innonymous/api/schemas/message/info.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from datetime import datetime
1+
from datetime import datetime, timezone
22
from uuid import UUID
33

4-
from pydantic import BaseModel
4+
from pydantic import BaseModel, validator
55

66
from innonymous.database.models import MessageType
77

@@ -14,5 +14,9 @@ class MessageInfoSchema(BaseModel):
1414
type: MessageType
1515
data: bytes = None
1616

17+
@validator('time')
18+
def convert_time(cls, time: datetime) -> datetime:
19+
return time.replace(tzinfo=timezone.utc)
20+
1721
class Config:
1822
orm_mode = True

innonymous/api/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Type
1+
from typing import Type, Optional
22

33
from pydantic import (
44
BaseSettings,
@@ -11,6 +11,9 @@ class APISettings(BaseSettings):
1111
amqp_url: str
1212
database_url: str
1313

14+
# Root of the api endpoints.
15+
root_path: Optional[str]
16+
1417
@validator('key')
1518
def validate_key(cls: Type['APISettings'], value: str) -> str:
1619
if len(value) < 32:

innonymous/api/views/messages.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
@router.get('/rooms/{uuid}/messages', response_model=MessageListSchema)
4141
async def get(
4242
uuid: UUID,
43-
limit: int = Query(None, gt=0),
43+
limit: int = Query(100, gt=0, le=500),
4444
after: datetime = Query(None),
4545
before: datetime = Query(None),
4646
session: AsyncSession = Depends(db_engine.session)
@@ -55,8 +55,8 @@ async def get(
5555

5656
return MessageListSchema(
5757
messages=[
58-
MessageInfoSchema.from_orm(room)
59-
for room in await get_by(
58+
MessageInfoSchema.from_orm(message)
59+
for message in await get_by(
6060
session,
6161
MessageModel,
6262
MessageModel.room_uuid,
@@ -91,7 +91,7 @@ async def create(
9191
if not room:
9292
raise HTTPException(
9393
status_code=status.HTTP_404_NOT_FOUND,
94-
detail=f'UserModel with uuid {uuid} not found.'
94+
detail=f'Room with uuid {uuid} not found.'
9595
)
9696

9797
message = MessageModel(

innonymous/api/views/rooms.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from uuid import UUID
2+
13
from fastapi import (
24
APIRouter,
35
Depends,
@@ -23,12 +25,12 @@
2325
RoomModel,
2426
UserModel
2527
)
26-
from innonymous.database.utils import get_all
28+
from innonymous.database.utils import get_all, get_by
2729

2830
router = APIRouter(tags=['rooms'])
2931

3032

31-
@router.get('/rooms/', response_model=RoomListSchema)
33+
@router.get('/rooms', response_model=RoomListSchema)
3234
async def get(
3335
session: AsyncSession = Depends(db_engine.session)
3436
) -> RoomListSchema:
@@ -40,6 +42,22 @@ async def get(
4042
)
4143

4244

45+
@router.get('/rooms/{uuid}', response_model=RoomInfoSchema)
46+
async def get_by_uuid(
47+
uuid: UUID, session: AsyncSession = Depends(db_engine.session)
48+
) -> RoomInfoSchema:
49+
room: RoomModel
50+
room, = await get_by(session, RoomModel, RoomModel.uuid, uuid) or [None]
51+
52+
if not room:
53+
raise HTTPException(
54+
status_code=status.HTTP_404_NOT_FOUND,
55+
detail=f'Room with uuid {uuid} not found.'
56+
)
57+
58+
return RoomInfoSchema.from_orm(room)
59+
60+
4361
@router.post('/rooms/new', response_model=RoomInfoSchema)
4462
async def create(
4563
room: RoomCreateSchema,

innonymous/api/views/users.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434

3535

3636
@router.get('/users/{uuid}', response_model=UserInfoSchema)
37-
async def get(
37+
async def get_by_uuid(
3838
uuid: UUID, session: AsyncSession = Depends(db_engine.session)
3939
) -> UserInfoSchema:
4040
user = await get_by(session, UserModel, UserModel.uuid, uuid)
4141

4242
if not user:
4343
raise HTTPException(
4444
status_code=status.HTTP_404_NOT_FOUND,
45-
detail=f'UserModel with uuid {uuid} not found.'
45+
detail=f'User with uuid {uuid} not found.'
4646
)
4747

4848
return UserInfoSchema.from_orm(user[0])

0 commit comments

Comments
 (0)