intro inbox service

This commit is contained in:
knst-kotov 2022-02-22 14:44:01 +03:00
parent 30f965e6e9
commit c04cd77418
8 changed files with 136 additions and 64 deletions

34
inbox_main.py Normal file
View File

@ -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))

View File

@ -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

80
inbox_schema.graphql Normal file
View File

@ -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
}

View File

@ -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",

15
resolvers_base.py Normal file
View File

@ -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]

View File

@ -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!

View File

@ -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)

View File

@ -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-"