intro inbox service
This commit is contained in:
parent
30f965e6e9
commit
c04cd77418
34
inbox_main.py
Normal file
34
inbox_main.py
Normal 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))
|
|
@ -1,7 +1,7 @@
|
||||||
from orm import User
|
from orm import User
|
||||||
from orm.base import local_session
|
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
|
from auth.authenticate import login_required
|
||||||
|
|
80
inbox_schema.graphql
Normal file
80
inbox_schema.graphql
Normal 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
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
from resolvers.auth import login, sign_out, is_email_free, register, confirm
|
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, \
|
from resolvers.zine import create_shout, get_shout_by_slug, top_month, top_overall, \
|
||||||
recent_shouts, top_viewed, shouts_by_author, shouts_by_topic, \
|
recent_shouts, top_viewed, shouts_by_author, shouts_by_topic, \
|
||||||
shouts_candidates, shouts_reviewed, shouts_subscribed
|
shouts_candidates, shouts_reviewed, shouts_subscribed
|
||||||
|
@ -16,10 +15,6 @@ __all__ = [
|
||||||
"confirm",
|
"confirm",
|
||||||
# TODO: "reset_password_code",
|
# TODO: "reset_password_code",
|
||||||
# TODO: "reset_password_confirm",
|
# TODO: "reset_password_confirm",
|
||||||
"create_message",
|
|
||||||
"delete_message",
|
|
||||||
"get_messages",
|
|
||||||
"update_messages",
|
|
||||||
"create_shout",
|
"create_shout",
|
||||||
"get_current_user",
|
"get_current_user",
|
||||||
"get_users_by_slugs",
|
"get_users_by_slugs",
|
||||||
|
|
15
resolvers_base.py
Normal file
15
resolvers_base.py
Normal 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]
|
|
@ -17,11 +17,6 @@ type UserResult {
|
||||||
user: User
|
user: User
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageResult {
|
|
||||||
error: String
|
|
||||||
message: Message
|
|
||||||
}
|
|
||||||
|
|
||||||
input ShoutInput {
|
input ShoutInput {
|
||||||
slug: String!
|
slug: String!
|
||||||
body: String!
|
body: String!
|
||||||
|
@ -58,29 +53,6 @@ type CommentResult {
|
||||||
comment: Comment
|
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 {
|
input TopicInput {
|
||||||
slug: String!
|
slug: String!
|
||||||
title: String
|
title: String
|
||||||
|
@ -111,12 +83,6 @@ type CommentUpdatedResult {
|
||||||
################################### Mutation
|
################################### Mutation
|
||||||
|
|
||||||
type 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
|
# auth
|
||||||
confirmEmail(token: String!): AuthResult!
|
confirmEmail(token: String!): AuthResult!
|
||||||
registerUser(email: String!, password: String): AuthResult!
|
registerUser(email: String!, password: String): AuthResult!
|
||||||
|
@ -172,10 +138,6 @@ type Query {
|
||||||
userSubscribers(slug: String!): [User]!
|
userSubscribers(slug: String!): [User]!
|
||||||
userSubscribedTopics(slug: String!): [Topic]!
|
userSubscribedTopics(slug: String!): [Topic]!
|
||||||
|
|
||||||
# messages
|
|
||||||
enterChat(chatId: String!, size: Int = 50): EnterChatResult!
|
|
||||||
getMessages(chatId: String!, size: Int!, page: Int!): [Message]!
|
|
||||||
|
|
||||||
# shouts
|
# shouts
|
||||||
getShoutBySlug(slug: String!): Shout!
|
getShoutBySlug(slug: String!): Shout!
|
||||||
shoutsByTopic(topic: String!, page: Int!, size: Int!): [Shout]!
|
shoutsByTopic(topic: String!, page: Int!, size: Int!): [Shout]!
|
||||||
|
@ -209,7 +171,6 @@ type Query {
|
||||||
############################################ Subscription
|
############################################ Subscription
|
||||||
|
|
||||||
type Subscription {
|
type Subscription {
|
||||||
chatUpdated(chatId: String!): ChatUpdatedResult!
|
|
||||||
onlineUpdated: [User!]!
|
onlineUpdated: [User!]!
|
||||||
shoutUpdated: Shout!
|
shoutUpdated: Shout!
|
||||||
userUpdated: User!
|
userUpdated: User!
|
||||||
|
@ -282,24 +243,6 @@ type User {
|
||||||
old_id: String
|
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 {
|
type Comment {
|
||||||
id: Int!
|
id: Int!
|
||||||
author: User!
|
author: User!
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from settings import PORT
|
from settings import PORT, INBOX_SERVICE_PORT
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
dev_mode = len(sys.argv) > 1 and sys.argv[1] == "dev"
|
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:
|
if dev_mode:
|
||||||
print("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)
|
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 :
|
else :
|
||||||
uvicorn.run("main:app", host="0.0.0.0", port=PORT)
|
uvicorn.run("main:app", host="0.0.0.0", port=PORT)
|
||||||
|
|
|
@ -2,6 +2,7 @@ from pathlib import Path
|
||||||
from os import environ
|
from os import environ
|
||||||
|
|
||||||
PORT = 8080
|
PORT = 8080
|
||||||
|
INBOX_SERVICE_PORT = 8081
|
||||||
|
|
||||||
BACKEND_URL = environ.get("BACKEND_URL") or "https://localhost:8080"
|
BACKEND_URL = environ.get("BACKEND_URL") or "https://localhost:8080"
|
||||||
OAUTH_CALLBACK_URL = environ.get("OAUTH_CALLBACK_URL") or "https://localhost:8080/auth/key-"
|
OAUTH_CALLBACK_URL = environ.get("OAUTH_CALLBACK_URL") or "https://localhost:8080/auth/key-"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user