From c04cd7741854ea86066b5de1e447625538d2ba2e Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Tue, 22 Feb 2022 14:44:01 +0300 Subject: [PATCH] intro inbox service --- inbox_main.py | 34 +++++++++++ {resolvers => inbox_resolvers}/inbox.py | 2 +- inbox_schema.graphql | 80 +++++++++++++++++++++++++ resolvers/__init__.py | 5 -- resolvers_base.py | 15 +++++ schema.graphql | 57 ------------------ server.py | 6 +- settings.py | 1 + 8 files changed, 136 insertions(+), 64 deletions(-) create mode 100644 inbox_main.py rename {resolvers => inbox_resolvers}/inbox.py (98%) create mode 100644 inbox_schema.graphql create mode 100644 resolvers_base.py diff --git a/inbox_main.py b/inbox_main.py new file mode 100644 index 00000000..2349449e --- /dev/null +++ b/inbox_main.py @@ -0,0 +1,34 @@ +from importlib import import_module + +from ariadne import load_schema_from_path, make_executable_schema + +from ariadne.asgi import GraphQL +from starlette.applications import Starlette +from starlette.middleware import Middleware +from starlette.middleware.authentication import AuthenticationMiddleware +from starlette.middleware.sessions import SessionMiddleware +from starlette.routing import Route + +from auth.authenticate import JWTAuthenticate +from redis import redis + +import asyncio + +from resolvers_base import resolvers +import inbox_resolvers.inbox + +schema = make_executable_schema(load_schema_from_path("inbox_schema.graphql"), resolvers) + +middleware = [ + Middleware(AuthenticationMiddleware, backend=JWTAuthenticate()), + Middleware(SessionMiddleware, secret_key="!secret") +] + +async def start_up(): + await redis.connect() + +async def shutdown(): + await redis.disconnect() + +app = Starlette(debug=True, on_startup=[start_up], on_shutdown=[shutdown], middleware=middleware) +app.mount("/", GraphQL(schema, debug=True)) diff --git a/resolvers/inbox.py b/inbox_resolvers/inbox.py similarity index 98% rename from resolvers/inbox.py rename to inbox_resolvers/inbox.py index 086e1785..30e3549b 100644 --- a/resolvers/inbox.py +++ b/inbox_resolvers/inbox.py @@ -1,7 +1,7 @@ from orm import User from orm.base import local_session -from resolvers.base import mutation, query, subscription +from resolvers_base import mutation, query, subscription from auth.authenticate import login_required diff --git a/inbox_schema.graphql b/inbox_schema.graphql new file mode 100644 index 00000000..201582ce --- /dev/null +++ b/inbox_schema.graphql @@ -0,0 +1,80 @@ +scalar DateTime + +################################### Payload + +type Result { + error: String +} + +type MessageResult { + error: String + message: Message +} + +enum MessageStatus { + NEW + UPDATED + DELETED +} + +type ChatUpdatedResult { + error: String + status: MessageStatus + message: Message +} + +type CreateChatResult { + chatId: String + error: String +} + +type EnterChatResult { + chat: Chat + messages: [Message] + error: String +} + +################################### Mutation + +type Mutation { + # message + createChat(description: String): CreateChatResult! + createMessage(chatId: String!, body: String!, replyTo: Int): MessageResult! + updateMessage(chatId: String!, id: Int!, body: String!): MessageResult! + deleteMessage(chatId: String!, id: Int!): Result! +} + +################################### Query + +type Query { + # messages + enterChat(chatId: String!, size: Int = 50): EnterChatResult! + getMessages(chatId: String!, size: Int!, page: Int!): [Message]! +} + +############################################ Subscription + +type Subscription { + chatUpdated(chatId: String!): ChatUpdatedResult! +} + +############################################ Entities + + +type Message { + author: String! + chatRoom: Int! + body: String! + createdAt: DateTime! + id: Int! + replyTo: Int + updatedAt: DateTime! + visibleForUsers: [Int]! +} + +type Chat { + id: Int! + createdAt: DateTime! + updatedAt: DateTime! + description: String +} diff --git a/resolvers/__init__.py b/resolvers/__init__.py index 7fa3d745..33098b93 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -1,5 +1,4 @@ from resolvers.auth import login, sign_out, is_email_free, register, confirm -from resolvers.inbox import create_message, delete_message, update_message, get_messages from resolvers.zine import create_shout, get_shout_by_slug, top_month, top_overall, \ recent_shouts, top_viewed, shouts_by_author, shouts_by_topic, \ shouts_candidates, shouts_reviewed, shouts_subscribed @@ -16,10 +15,6 @@ __all__ = [ "confirm", # TODO: "reset_password_code", # TODO: "reset_password_confirm", - "create_message", - "delete_message", - "get_messages", - "update_messages", "create_shout", "get_current_user", "get_users_by_slugs", diff --git a/resolvers_base.py b/resolvers_base.py new file mode 100644 index 00000000..68414507 --- /dev/null +++ b/resolvers_base.py @@ -0,0 +1,15 @@ +from ariadne import MutationType, QueryType, SubscriptionType, ScalarType + + +query = QueryType() +mutation = MutationType() +subscription = SubscriptionType() + + +datetime_scalar = ScalarType("DateTime") + +@datetime_scalar.serializer +def serialize_datetime(value): + return value.isoformat() + +resolvers = [query, mutation, subscription, datetime_scalar] diff --git a/schema.graphql b/schema.graphql index a643ccc0..3de9786e 100644 --- a/schema.graphql +++ b/schema.graphql @@ -17,11 +17,6 @@ type UserResult { user: User } -type MessageResult { - error: String - message: Message -} - input ShoutInput { slug: String! body: String! @@ -58,29 +53,6 @@ type CommentResult { comment: Comment } -enum MessageStatus { - NEW - UPDATED - DELETED -} - -type ChatUpdatedResult { - error: String - status: MessageStatus - message: Message -} - -type CreateChatResult { - chatId: String - error: String -} - -type EnterChatResult { - chat: Chat - messages: [Message] - error: String -} - input TopicInput { slug: String! title: String @@ -111,12 +83,6 @@ type CommentUpdatedResult { ################################### Mutation type Mutation { - # message - createChat(description: String): CreateChatResult! - createMessage(chatId: String!, body: String!, replyTo: Int): MessageResult! - updateMessage(chatId: String!, id: Int!, body: String!): MessageResult! - deleteMessage(chatId: String!, id: Int!): Result! - # auth confirmEmail(token: String!): AuthResult! registerUser(email: String!, password: String): AuthResult! @@ -172,10 +138,6 @@ type Query { userSubscribers(slug: String!): [User]! userSubscribedTopics(slug: String!): [Topic]! - # messages - enterChat(chatId: String!, size: Int = 50): EnterChatResult! - getMessages(chatId: String!, size: Int!, page: Int!): [Message]! - # shouts getShoutBySlug(slug: String!): Shout! shoutsByTopic(topic: String!, page: Int!, size: Int!): [Shout]! @@ -209,7 +171,6 @@ type Query { ############################################ Subscription type Subscription { - chatUpdated(chatId: String!): ChatUpdatedResult! onlineUpdated: [User!]! shoutUpdated: Shout! userUpdated: User! @@ -282,24 +243,6 @@ type User { old_id: String } -type Message { - author: String! - chatRoom: Int! - body: String! - createdAt: DateTime! - id: Int! - replyTo: Int - updatedAt: DateTime! - visibleForUsers: [Int]! -} - -type Chat { - id: Int! - createdAt: DateTime! - updatedAt: DateTime! - description: String -} - type Comment { id: Int! author: User! diff --git a/server.py b/server.py index 3c96e7ff..fd3514f7 100644 --- a/server.py +++ b/server.py @@ -1,12 +1,16 @@ import uvicorn -from settings import PORT +from settings import PORT, INBOX_SERVICE_PORT import sys if __name__ == '__main__': dev_mode = len(sys.argv) > 1 and sys.argv[1] == "dev" + inbox_service = len(sys.argv) > 1 and sys.argv[1] == "inbox" if dev_mode: print("DEV MODE") uvicorn.run("main:app", host="0.0.0.0", port=8080, ssl_keyfile="discours.key", ssl_certfile="discours.crt", reload=True) + elif inbox_service: + print("INBOX SERVICE") + uvicorn.run("inbox_main:app", host="0.0.0.0", port=INBOX_SERVICE_PORT) else : uvicorn.run("main:app", host="0.0.0.0", port=PORT) diff --git a/settings.py b/settings.py index 91261d92..4a10faf4 100644 --- a/settings.py +++ b/settings.py @@ -2,6 +2,7 @@ from pathlib import Path from os import environ PORT = 8080 +INBOX_SERVICE_PORT = 8081 BACKEND_URL = environ.get("BACKEND_URL") or "https://localhost:8080" OAUTH_CALLBACK_URL = environ.get("OAUTH_CALLBACK_URL") or "https://localhost:8080/auth/key-"