Girish Jeswani commited on
Commit
2b3e7b8
·
1 Parent(s): 4dda403

update routes

Browse files
multi_llm_chatbot_backend/app/main.py CHANGED
@@ -5,19 +5,35 @@ load_dotenv()
5
 
6
  from fastapi import FastAPI
7
  from fastapi.middleware.cors import CORSMiddleware
8
- from app.api.routes import router
9
- import logging
 
 
10
 
 
 
 
 
 
 
11
 
12
  logging.basicConfig(
13
  level=logging.INFO,
14
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
15
  )
16
 
 
 
 
 
 
 
 
17
 
18
  app = FastAPI(
19
  title="Multi-LLM Chatbot Backend",
20
- version="1.0.0" # Updated version
 
21
  )
22
 
23
  app.add_middleware(
@@ -28,18 +44,21 @@ app.add_middleware(
28
  allow_headers=["*"],
29
  )
30
 
31
- app.include_router(router)
 
 
 
32
 
33
  @app.get("/")
34
  def root():
35
  return {
36
- "message": "Multi-LLM PhD Advisor Backend is up and running",
37
- "version": "1.0.0",
38
  "features": [
39
- "Improved Session Management",
40
- "Unified Context Handling",
 
41
  "Ollama Support",
42
- "Gemini API Support",
43
- "Provider Switching"
44
  ]
45
  }
 
5
 
6
  from fastapi import FastAPI
7
  from fastapi.middleware.cors import CORSMiddleware
8
+ from contextlib import asynccontextmanager
9
+
10
+ # Import the new database functions
11
+ from app.core.database import connect_to_mongo, close_mongo_connection
12
 
13
+ # Import all route modules
14
+ from app.api.routes import router as main_router
15
+ from app.api.routes.auth import router as auth_router
16
+ from app.api.routes.chat_sessions import router as chat_sessions_router
17
+
18
+ import logging
19
 
20
  logging.basicConfig(
21
  level=logging.INFO,
22
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
23
  )
24
 
25
+ @asynccontextmanager
26
+ async def lifespan(app: FastAPI):
27
+ # Startup
28
+ await connect_to_mongo()
29
+ yield
30
+ # Shutdown
31
+ await close_mongo_connection()
32
 
33
  app = FastAPI(
34
  title="Multi-LLM Chatbot Backend",
35
+ version="2.0.0",
36
+ lifespan=lifespan
37
  )
38
 
39
  app.add_middleware(
 
44
  allow_headers=["*"],
45
  )
46
 
47
+ # Include all routers
48
+ app.include_router(main_router)
49
+ app.include_router(auth_router, prefix="/auth", tags=["authentication"])
50
+ app.include_router(chat_sessions_router, prefix="/api", tags=["chat-sessions"])
51
 
52
  @app.get("/")
53
  def root():
54
  return {
55
+ "message": "Multi-LLM PhD Advisor Backend with Authentication",
56
+ "version": "2.0.0",
57
  "features": [
58
+ "User Authentication",
59
+ "Persistent Chat Sessions",
60
+ "MongoDB Integration",
61
  "Ollama Support",
62
+ "Gemini API Support"
 
63
  ]
64
  }
phd-advisor-frontend/src/App.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useState } from 'react';
2
  import { ThemeProvider } from './contexts/ThemeContext';
3
  import HomePage from './pages/HomePage';
4
  import ChatPage from './pages/ChatPage';
@@ -8,43 +8,78 @@ import './styles/components.css';
8
  function App() {
9
  const [currentView, setCurrentView] = useState('home');
10
  const [isAuthenticated, setIsAuthenticated] = useState(false);
 
 
11
 
12
- const navigateToAuth = () => {
13
- setCurrentView('auth');
14
- };
15
-
16
- const navigateToHome = () => {
17
- setCurrentView('home');
18
- setIsAuthenticated(false); // Reset auth when going home
19
- // Reset session when going home
20
- fetch('http://localhost:8000/reset-session', {
21
- method: 'POST',
22
- headers: {
23
- 'Content-Type': 'application/json',
 
 
 
 
 
24
  }
25
- }).catch(console.error);
26
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- const handleAuthSuccess = () => {
29
- setIsAuthenticated(true);
30
- setCurrentView('chat');
31
- };
 
 
 
 
32
 
33
  return (
34
- <ThemeProvider>
35
- <div className="App">
36
- {currentView === 'home' && (
37
- <HomePage onNavigateToChat={navigateToAuth} />
38
- )}
39
- {currentView === 'auth' && (
40
- <AuthPage onAuthSuccess={handleAuthSuccess} />
41
- )}
42
- {currentView === 'chat' && (
43
- <ChatPage onNavigateToHome={navigateToHome} />
44
- )}
45
- </div>
46
- </ThemeProvider>
47
- );
 
 
 
 
 
48
  }
49
 
50
  export default App;
 
1
+ import React, { useState, useEffect } from 'react';
2
  import { ThemeProvider } from './contexts/ThemeContext';
3
  import HomePage from './pages/HomePage';
4
  import ChatPage from './pages/ChatPage';
 
8
  function App() {
9
  const [currentView, setCurrentView] = useState('home');
10
  const [isAuthenticated, setIsAuthenticated] = useState(false);
11
+ const [user, setUser] = useState(null);
12
+ const [authToken, setAuthToken] = useState(null);
13
 
14
+ // Check for existing authentication on app start
15
+ useEffect(() => {
16
+ const token = localStorage.getItem('authToken');
17
+ const userData = localStorage.getItem('user');
18
+
19
+ if (token && userData) {
20
+ try {
21
+ const parsedUser = JSON.parse(userData);
22
+ setAuthToken(token);
23
+ setUser(parsedUser);
24
+ setIsAuthenticated(true);
25
+ setCurrentView('chat');
26
+ } catch (error) {
27
+ // Clear invalid data
28
+ localStorage.removeItem('authToken');
29
+ localStorage.removeItem('user');
30
+ }
31
  }
32
+ }, []);
33
+
34
+ const navigateToAuth = () => {
35
+ setCurrentView('auth');
36
+ };
37
+
38
+ const navigateToHome = () => {
39
+ setCurrentView('home');
40
+ setIsAuthenticated(false);
41
+ setUser(null);
42
+ setAuthToken(null);
43
+ localStorage.removeItem('authToken');
44
+ localStorage.removeItem('user');
45
+ };
46
+
47
+ const handleAuthSuccess = (userData, token) => {
48
+ setUser(userData);
49
+ setAuthToken(token);
50
+ setIsAuthenticated(true);
51
+ setCurrentView('chat');
52
+ };
53
 
54
+ const handleSignOut = () => {
55
+ localStorage.removeItem('authToken');
56
+ localStorage.removeItem('user');
57
+ setUser(null);
58
+ setAuthToken(null);
59
+ setIsAuthenticated(false);
60
+ setCurrentView('home');
61
+ };
62
 
63
  return (
64
+ <ThemeProvider>
65
+ <div className="App">
66
+ {currentView === 'home' && (
67
+ <HomePage onNavigateToChat={navigateToAuth} />
68
+ )}
69
+ {currentView === 'auth' && (
70
+ <AuthPage onAuthSuccess={handleAuthSuccess} />
71
+ )}
72
+ {currentView === 'chat' && isAuthenticated && (
73
+ <ChatPage
74
+ user={user}
75
+ authToken={authToken}
76
+ onNavigateToHome={navigateToHome}
77
+ onSignOut={handleSignOut}
78
+ />
79
+ )}
80
+ </div>
81
+ </ThemeProvider>
82
+ );
83
  }
84
 
85
  export default App;