Skip to content

Commit

Permalink
updates to chat api
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyjohnson1 committed Nov 4, 2024
1 parent db7cf1b commit 0dd5958
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 11 deletions.
22 changes: 22 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@
emo_27_label VARCHAR
);
CREATE TABLE IF NOT EXISTS groups (
group_id TEXT PRIMARY KEY,
group_name TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS users (
user_id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
last_seen_online TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS user_groups (
user_id TEXT,
group_id TEXT,
FOREIGN KEY (user_id) REFERENCES users (user_id),
FOREIGN KEY (group_id) REFERENCES groups (group_id),
PRIMARY KEY (user_id, group_id)
);
CREATE INDEX IF NOT EXISTS idx_user_groups_user_id ON user_groups (user_id);
CREATE INDEX IF NOT EXISTS idx_user_groups_group_id ON user_groups (group_id);
GRANT ALL PRIVILEGES ON DATABASE ${envVars.POSTGRES_DB} TO ${envVars.POSTGRES_USER};
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ${envVars.POSTGRES_USER};
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO ${envVars.POSTGRES_USER};
Expand Down
Empty file added topos/api/__init__.py
Empty file.
21 changes: 20 additions & 1 deletion topos/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,31 @@
"""

from multiprocessing import Process
import uvicorn

def start_local_api():
def start_topos_api():
"""Function to start the API in local mode."""
print("\033[92mINFO:\033[0m API docs available at: \033[1mhttp://0.0.0.0:13341/docs\033[0m")
uvicorn.run(app, host="0.0.0.0", port=13341)

def start_kafka_api():
from ..chat_api.api import start_messenger_server
start_messenger_server()

def start_local_api():
process1 = Process(target=start_topos_api)
process2 = Process(target=start_kafka_api)
process1.start()
process2.start()
process1.join()
process2.join()

# def start_local_api():
# """Function to start the API in local mode."""
# print("\033[92mINFO:\033[0m API docs available at: \033[1mhttp://0.0.0.0:13341/docs\033[0m")
# uvicorn.run(app, host="0.0.0.0", port=13341)


def start_web_api():
"""Function to start the API in web mode with SSL."""
Expand Down
Empty file added topos/chat_api/__init__.py
Empty file.
12 changes: 7 additions & 5 deletions topos/chat_api/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import subprocess
from fastapi import FastAPI
from ..config import setup_config, get_ssl_certificates
import uvicorn

from .server import app as chat_app

def start_chat():
def start_messenger_server():
"""Function to start the API in local mode."""
# print("\033[92mINFO:\033[0m API docs available at: \033[1mhttp://127.0.0.1:13394/docs\033[0m")
# subprocess.run(["python", "topos/chat_api/chat_server.py"]) # A barebones chat server
subprocess.run(["uvicorn", "topos.chat_api.server:app", "--host", "0.0.0.0", "--port", "13394", "--workers", "1"])
print("\033[92mINFO:\033[0m API docs available at: \033[1mhttp://127.0.0.1:13394/docs\033[0m")
uvicorn.run(chat_app, host="127.0.0.1", port=13394)

# start through zrok
# uvicorn main:app --host 127.0.0.1 --port 13394 & zrok expose http://localhost:13394
8 changes: 4 additions & 4 deletions topos/chat_api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import json
import os

from typing import Dict, List
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from typing import Dict, List
from aiokafka import AIOKafkaProducer, AIOKafkaConsumer
from fastapi.concurrency import asynccontextmanager
from topos.services.messages.group_management_service import GroupManagementService
Expand Down Expand Up @@ -220,9 +220,9 @@ async def get_missed_messages(request: MissedMessagesRequest):
missed_message_service = MissedMessageService()
return await missed_message_service.get_missed_messages(user_id=request.user_id,group_management_service=group_management_service)

if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=13394)
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run(app, host="0.0.0.0", port=13394)


""" Message JSON Schema
Expand Down
2 changes: 1 addition & 1 deletion topos/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def main():
"""
# import chat_api
from .chat_api import api
api.start_chat()
api.start_messenger_server()

if args.command == 'zrok':
"""
Expand Down
Empty file added topos/downloaders/__init__.py
Empty file.

0 comments on commit 0dd5958

Please sign in to comment.