Skip to content

Commit

Permalink
Update UI env (#36)
Browse files Browse the repository at this point in the history
* Add ui env vars

* Add openrouter models

* Add prod deployment

* Ignore keyfiles
  • Loading branch information
jamakase authored Sep 10, 2024
1 parent 2aa9f76 commit 9e456f7
Show file tree
Hide file tree
Showing 21 changed files with 1,611 additions and 165 deletions.
36 changes: 19 additions & 17 deletions .github/workflows/backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: Backend CI workflow
on:
push:
paths:
- "frontend/**"
- ".github/workflows/frontend.yaml"
- "services/backend/**"
- ".github/workflows/backend.yaml"
branches:
- main

Expand All @@ -12,20 +12,23 @@ jobs:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./frontend
working-directory: ./services/backend
steps:
- name: Clone repository
uses: actions/checkout@v4

- name: Use Node.js 20.x and cache dependencies
uses: actions/setup-node@v3
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
node-version: 20.x
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
python-version: 3.11

- run: npm ci
- run: npm run build
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# - name: Run tests
# run: python -m unittest discover tests

build-container:
if: github.ref == 'refs/heads/main'
Expand All @@ -34,20 +37,19 @@ jobs:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./frontend
working-directory: ./services/backend
steps:
- name: Clone repository
uses: actions/checkout@v4
- name: Login to Yandex Cloud Container Registry
id: login-cr
uses: yc-actions/yc-cr-login@v2
with:
yc-sa-json-credentials: ${{ secrets.YANDEX_AUTH_KEY }}
yc-sa-json-credentials: ${{ secrets.YANDEX_SA_KEY }}
- run: |
echo "Packaging docker image ..."
tag="${DOCKER_REGISTRY}/app:${{ github.sha }}"
docker build -t $tag -t ${DOCKER_REGISTRY}/app:latest .
docker push ${DOCKER_REGISTRY}/app --all-tags
tag="${DOCKER_REGISTRY}/backend:${{ github.sha }}"
docker build -t $tag -t ${DOCKER_REGISTRY}/backend:latest .
docker push ${DOCKER_REGISTRY}/backend --all-tags
env:
NEXT_PUBLIC_API_URL: https://api.afana-propdoc.ru
DOCKER_REGISTRY: cr.yandex/
DOCKER_REGISTRY: cr.yandex/crpc50gkvq2bp251sfgb
55 changes: 55 additions & 0 deletions .github/workflows/ml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: ML CI workflow
on:
push:
paths:
- "services/ml/**"
- ".github/workflows/ml.yaml"
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./services/ml
steps:
- name: Clone repository
uses: actions/checkout@v4

- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
# - name: Run tests
# run: python -m unittest discover tests

build-container:
if: github.ref == 'refs/heads/main'
needs:
- build
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./services/ml
steps:
- name: Clone repository
uses: actions/checkout@v4
- name: Login to Yandex Cloud Container Registry
id: login-cr
uses: yc-actions/yc-cr-login@v2
with:
yc-sa-json-credentials: ${{ secrets.YANDEX_SA_KEY }}
- run: |
echo "Packaging docker image ..."
tag="${DOCKER_REGISTRY}/ml:${{ github.sha }}"
docker build -t $tag -t ${DOCKER_REGISTRY}/ml:latest .
docker push ${DOCKER_REGISTRY}/ml --all-tags
env:
DOCKER_REGISTRY: cr.yandex/crpc50gkvq2bp251sfgb
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ down:
docker compose down

build:
docker compose build
docker compose build

restart-service:
docker-compose -f docker-compose.yaml up -d $(svc)

build-service:
docker-compose -f docker-compose.yaml build $(svc)

rb:
make build-service $(svc) && make restart-service $(svc)
52 changes: 31 additions & 21 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
version: '3.8'

services:
traefik:
image: traefik:v2.11
container_name: traefik
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./data/ssl:/letsencrypt
restart: always

frontend_app:
container_name: frontend_app
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "8080:3000"
environment:
- API_URL=http://api.${HOST}
labels:
- "traefik.enable=true"
- "traefik.http.routers.app.rule=Host(`${HOST}`)"
- "traefik.http.routers.app.entrypoints=web"
- "traefik.http.services.app.loadbalancer.server.port=3000"

backend_app:
restart: unless-stopped
Expand All @@ -24,8 +47,11 @@ services:
depends_on:
- db
- redis
ports:
- "5000:5000"
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`api.${HOST}`)"
- "traefik.http.routers.api.entrypoints=web"
- "traefik.http.services.api.loadbalancer.server.port=5000"

db:
container_name: db
Expand Down Expand Up @@ -57,7 +83,7 @@ services:
celery_worker:
build:
context: ./services/backend
dockerfile: Dockerfile-celery
dockerfile: Dockerfile
command: celery -A celery_worker.celery worker --loglevel=info
volumes:
- ./services/backend:/app
Expand All @@ -70,22 +96,6 @@ services:
- redis
- backend_app


# naive_ml_app:
# build:
# context: ./services/backend
# dockerfile: Dockerfile
# volumes:
# - ./services/backend/naive_ML_for_tests:/app
# environment:
# - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
# depends_on:
# - db
# command: python3 -m services.backend.naive_ML_for_tests.run
# ports:
# - "5001:5001"
# working_dir: /app

ml:
build:
context: ./services/ml
Expand Down
1 change: 1 addition & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
10 changes: 7 additions & 3 deletions frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Metadata } from "next";
import { Roboto_Mono } from "next/font/google";
import { Inter } from "next/font/google";
import "./globals.css";
import { ConfigProvider } from "@/domain/config/ConfigProvider";

const robotoMono = Roboto_Mono({ subsets: ["latin", "cyrillic"], weight: ['400', '700'], display: 'swap' });
const inter = Inter({ subsets: ["latin", "cyrillic"] });
Expand All @@ -14,7 +15,7 @@ export const metadata: Metadata = {
},
};

export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
Expand All @@ -24,9 +25,12 @@ export default function RootLayout({
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
<link rel="icon" href="/favicon.ico" sizes="any" />

</head>
<body className={inter.className}>{children}</body>
<body className={inter.className}>
<ConfigProvider configData={{ ENDPOINT: process.env.API_URL ?? 'http://localhost:5000' }}>
{children}
</ConfigProvider>
</body>
</html>
);
}
17 changes: 10 additions & 7 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use client";

import { useState, useEffect } from 'react';
import { api } from './api';
import { api } from '../domain/api/api';
import Sidebar from './components/Sidebar';
import MessageList from './components/MessageList';
import { useConfig } from '@/domain/config/ConfigProvider';

// Тип для хранения информации о чате
type Conversation = {
Expand All @@ -21,10 +22,12 @@ export default function Home() {
const [messages, setMessages] = useState<Array<{ id: number; text: string; sender: string }>>([]);
const [isSidebarOpen, setIsSidebarOpen] = useState(false);

const config = useConfig();

useEffect(() => {
const initializeUser = async () => {
try {
const response = await api.get_messages__user_id_();
const response = await api.get_messages__user_id_(config.ENDPOINT);
console.log('API response:', response);

if (response.data && Array.isArray(response.data) && response.data.length > 0) {
Expand Down Expand Up @@ -78,7 +81,7 @@ export default function Home() {
)
);

const response = await api.sendMessage(conversationIdAtSend!, message);
const response = await api.sendMessage(config.ENDPOINT, conversationIdAtSend!, message);

if (!response.task_id) {
throw new Error('Ошибка при отправке сообщения');
Expand All @@ -90,7 +93,7 @@ export default function Home() {
let botResponse;
do {
await new Promise(resolve => setTimeout(resolve, 1000));
botResponse = await api.checkTaskResult(response.task_id);
botResponse = await api.checkTaskResult(config.ENDPOINT, response.task_id);
} while (!botResponse.ready);

if (botResponse.successful && botResponse.value) {
Expand Down Expand Up @@ -136,7 +139,7 @@ export default function Home() {

const handleAddConversation = async () => {
try {
const response = await api.createConversation();
const response = await api.createConversation(config.ENDPOINT);
if (response.conversation_id) {
const newConversation = {
id: response.conversation_id,
Expand All @@ -159,7 +162,7 @@ export default function Home() {

const handleDeleteConversation = async (conversationId: number) => {
try {
await api.deleteConversation(conversationId);
await api.deleteConversation(config.ENDPOINT, conversationId);

setConversations(prevConversations => {
const updatedConversations = prevConversations.filter(conversation => conversation.id !== conversationId);
Expand All @@ -181,7 +184,7 @@ export default function Home() {

const loadMessagesForConversation = async (conversationId: number) => {
try {
const response = await api.getMessages(conversationId);
const response = await api.getMessages(config.ENDPOINT, conversationId);
if (response && response.length > 0 && response[0].messages && response[0].messages.length > 0) {
const conversationMessages = response[0].messages[0][conversationId];
if (conversationMessages && Array.isArray(conversationMessages)) {
Expand Down
26 changes: 12 additions & 14 deletions frontend/src/app/api.tsx → frontend/src/domain/api/api.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { API_URL } from "../domain/config";

export const api = {
async createConversation() {
const response = await fetch(`${API_URL}/conversation/create`, {
async createConversation(api_url: string) {
const response = await fetch(`${api_url}/conversation/create`, {
method: 'POST',
credentials: "include",
});
return response.json();
},

async sendMessage(conversationId: number, message: string) {
const response = await fetch(`${API_URL}/message`, {
async sendMessage(api_url: string, conversationId: number, message: string) {
const response = await fetch(`${api_url}/message`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -20,15 +18,15 @@ export const api = {
return response.json();
},

async checkTaskResult(taskId: string) {
const response = await fetch(`${API_URL}/task/${taskId}`, {
async checkTaskResult(api_url: string, taskId: string) {
const response = await fetch(`${api_url}/task/${taskId}`, {
method: 'GET',
});
return response.json();
},

async get_messages__user_id_() {
const response = await fetch(`${API_URL}/conversations/get`, {
async get_messages__user_id_(api_url: string) {
const response = await fetch(`${api_url}/conversations/get`, {
method: 'POST',
credentials: "include"
});
Expand All @@ -37,15 +35,15 @@ export const api = {
return data;
},

async deleteConversation(conversationId: number) {
const response = await fetch(`${API_URL}/conversation/delete/${conversationId}`, {
async deleteConversation(api_url: string, conversationId: number) {
const response = await fetch(`${api_url}/conversation/delete/${conversationId}`, {
method: 'DELETE',
});
return response.json();
},

async getMessages(conversationId: number) {
const response = await fetch(`${API_URL}/messages/${conversationId}`, {
async getMessages(api_url: string, conversationId: number) {
const response = await fetch(`${api_url}/messages/${conversationId}`, {
method: 'GET',
credentials: "include"
});
Expand Down
Loading

0 comments on commit 9e456f7

Please sign in to comment.