forked from jakobengdahl/CommunityOverview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-dev.sh
More file actions
executable file
·279 lines (243 loc) · 9.22 KB
/
Copy pathstart-dev.sh
File metadata and controls
executable file
·279 lines (243 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
#
# Start Development Environment
# This script sets up and starts all services needed to run the full application.
#
# Usage:
# ./start-dev.sh [OPTIONS]
#
# Options:
# --profile <name> Use a configuration profile (default: "default")
# --data <path|url> Load graph data from a file path or URL (overwrites active data)
# --lang <en|sv> Set the application language (default: en)
#
# Profiles:
# Profiles are directories under config/ (e.g. config/esam/, config/default/).
# Each can contain: schema_config.json, federation_config.json, .env, graph.json.
# Missing files fall back to config/default/.
#
# Environment Variables:
# SCHEMA_FILE - Path to custom schema configuration file
# GRAPH_FILE - Path to graph data file (default: data/active/graph.json)
# LLM_PROVIDER - LLM provider to use: "openai" or "claude" (auto-detected from API keys if not set)
# APP_LANGUAGE - Application language: "en" or "sv" (default: en)
#
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Get the directory where the script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$SCRIPT_DIR/backend"
DATA_DIR="$SCRIPT_DIR/data"
ACTIVE_DATA="$DATA_DIR/active/graph.json"
DEFAULT_EXAMPLE="$DATA_DIR/examples/default.json"
cd "$SCRIPT_DIR"
# Source shared profile utilities
source "$SCRIPT_DIR/config/profile-utils.sh"
# =====================
# Parse Arguments
# =====================
DATA_SOURCE=""
LANG_OVERRIDE=""
PROFILE_NAME="default"
while [[ $# -gt 0 ]]; do
case $1 in
--profile)
PROFILE_NAME="$2"
shift 2
;;
--data)
DATA_SOURCE="$2"
shift 2
;;
--lang)
LANG_OVERRIDE="$2"
shift 2
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Usage: ./start-dev.sh [--profile <name>] [--data <path|url>] [--lang <en|sv>]"
exit 1
;;
esac
done
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} Community Knowledge Graph - Dev Start${NC}"
echo -e "${BLUE}========================================${NC}"
# =====================
# Profile Resolution
# =====================
validate_profile "$PROFILE_NAME"
PROFILE_DIR="$CONFIG_BASE_DIR/$PROFILE_NAME"
echo -e " ${BLUE}Profile:${NC} $PROFILE_NAME"
# Source .env files with fallback chain: profile → default → root
apply_profile_env "$PROFILE_NAME"
# Resolve schema config: env var takes precedence, then profile fallback
if [ -n "$GRAPH_SCHEMA_CONFIG" ]; then
export SCHEMA_FILE="$GRAPH_SCHEMA_CONFIG"
elif [ -z "$SCHEMA_FILE" ]; then
RESOLVED_SCHEMA=$(resolve_config "$PROFILE_NAME" "schema_config.json")
if [ -n "$RESOLVED_SCHEMA" ]; then
export SCHEMA_FILE="$RESOLVED_SCHEMA"
fi
fi
# Resolve federation config: env var takes precedence, then profile fallback
if [ -z "$FEDERATION_FILE" ] && [ -z "$GRAPH_FEDERATION_CONFIG" ]; then
RESOLVED_FEDERATION=$(resolve_config "$PROFILE_NAME" "federation_config.json")
if [ -n "$RESOLVED_FEDERATION" ]; then
export FEDERATION_FILE="$RESOLVED_FEDERATION"
fi
elif [ -n "$GRAPH_FEDERATION_CONFIG" ]; then
export FEDERATION_FILE="$GRAPH_FEDERATION_CONFIG"
fi
# Export profile name for backend /info endpoint
export CONFIG_PROFILE="$PROFILE_NAME"
# =====================
# Language Configuration
# =====================
if [ -n "$LANG_OVERRIDE" ]; then
export APP_LANGUAGE="$LANG_OVERRIDE"
echo -e "${YELLOW}Language set to: $APP_LANGUAGE${NC}"
elif [ -z "$APP_LANGUAGE" ]; then
export APP_LANGUAGE="en"
fi
# =====================
# Data Management
# =====================
echo -e "\n${YELLOW}[0/5] Setting up graph data...${NC}"
mkdir -p "$DATA_DIR/active"
mkdir -p "$DATA_DIR/examples"
if [ -n "$DATA_SOURCE" ]; then
# Data source specified - load from path or URL
if [[ "$DATA_SOURCE" =~ ^https?:// ]]; then
echo -e "Downloading graph data from: ${BLUE}$DATA_SOURCE${NC}"
if command -v curl &> /dev/null; then
curl -sL "$DATA_SOURCE" -o "$ACTIVE_DATA"
elif command -v wget &> /dev/null; then
wget -q "$DATA_SOURCE" -O "$ACTIVE_DATA"
else
echo -e "${RED}Error: curl or wget required to download data from URL${NC}"
exit 1
fi
echo -e "${GREEN}Graph data downloaded to $ACTIVE_DATA${NC}"
else
# Resolve relative paths
if [[ ! "$DATA_SOURCE" = /* ]]; then
DATA_SOURCE="$SCRIPT_DIR/$DATA_SOURCE"
fi
if [ ! -f "$DATA_SOURCE" ]; then
echo -e "${RED}Error: Data file not found: $DATA_SOURCE${NC}"
exit 1
fi
echo -e "Copying graph data from: ${BLUE}$DATA_SOURCE${NC}"
cp "$DATA_SOURCE" "$ACTIVE_DATA"
echo -e "${GREEN}Graph data copied to $ACTIVE_DATA${NC}"
fi
elif [ ! -f "$ACTIVE_DATA" ]; then
# No active data and no source specified - try profile graph.json, then default example
PROFILE_GRAPH=$(resolve_config "$PROFILE_NAME" "graph.json")
if [ -n "$PROFILE_GRAPH" ]; then
echo -e "Loading graph data from profile: ${BLUE}$PROFILE_GRAPH${NC}"
cp "$PROFILE_GRAPH" "$ACTIVE_DATA"
echo -e "${GREEN}Profile graph data loaded.${NC}"
elif [ -f "$DEFAULT_EXAMPLE" ]; then
echo -e "No active graph data found. Copying default example data..."
cp "$DEFAULT_EXAMPLE" "$ACTIVE_DATA"
echo -e "${GREEN}Default example data loaded.${NC}"
else
echo -e "${YELLOW}No example data found. Starting with empty graph.${NC}"
echo '{"nodes": [], "edges": [], "metadata": {"version": "1.0"}}' > "$ACTIVE_DATA"
fi
else
echo -e "${GREEN}Using existing active graph data.${NC}"
fi
# Set GRAPH_FILE to point to active data
export GRAPH_FILE="$ACTIVE_DATA"
# =====================
# Python Environment
# =====================
echo -e "\n${YELLOW}[1/5] Setting up Python environment...${NC}"
if [ ! -d "venv" ]; then
echo "Creating virtual environment..."
python3 -m venv venv
fi
# Activate virtual environment
source venv/bin/activate
# Install Python dependencies
echo "Installing Python dependencies..."
pip install -q -r "$BACKEND_DIR/requirements.txt"
echo -e "${GREEN}Python environment ready.${NC}"
# =====================
# Node.js Dependencies
# =====================
echo -e "\n${YELLOW}[2/5] Installing Node.js dependencies...${NC}"
if [ ! -d "node_modules" ]; then
npm install
else
echo "Node modules already installed. Run 'npm install' manually to update."
fi
echo -e "${GREEN}Node.js dependencies ready.${NC}"
# =====================
# Build Web App
# =====================
echo -e "\n${YELLOW}[3/5] Building web application...${NC}"
npm run build:web
echo -e "${GREEN}Web app built to frontend/web/dist/${NC}"
# =====================
# Build Widget
# =====================
echo -e "\n${YELLOW}[4/5] Building ChatGPT widget...${NC}"
npm run build:widget
echo -e "${GREEN}Widget built to frontend/widget/dist/${NC}"
# =====================
# Start Server
# =====================
echo -e "\n${YELLOW}[5/5] Starting FastAPI server...${NC}"
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} Services available at:${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e " ${BLUE}Web App:${NC} http://localhost:8000/web/"
echo -e " ${BLUE}Widget:${NC} http://localhost:8000/widget/"
echo -e " ${BLUE}REST API:${NC} http://localhost:8000/api/"
echo -e " ${BLUE}Chat API:${NC} http://localhost:8000/ui/"
echo -e " ${BLUE}MCP:${NC} http://localhost:8000/mcp"
echo -e " ${BLUE}Health:${NC} http://localhost:8000/health"
echo ""
echo -e "${GREEN} Configuration:${NC}"
echo -e " ${BLUE}Profile:${NC} $PROFILE_NAME"
if [ -n "$SCHEMA_FILE" ]; then
echo -e " ${BLUE}Schema:${NC} $SCHEMA_FILE"
else
echo -e " ${BLUE}Schema:${NC} config/default/schema_config.json (default)"
fi
if [ -n "$FEDERATION_FILE" ]; then
echo -e " ${BLUE}Federation:${NC} $FEDERATION_FILE"
fi
echo -e " ${BLUE}Graph data:${NC} $GRAPH_FILE"
echo -e " ${BLUE}Language:${NC} $APP_LANGUAGE"
echo ""
echo -e " ${YELLOW}Language can also be set via URL: http://localhost:8000/web/?lang=sv${NC}"
echo ""
echo -e "Press Ctrl+C to stop the server."
echo -e "${GREEN}========================================${NC}"
echo ""
# Start the server
# Check for Codespace environment to print public URL
if [ -n "$CODESPACE_NAME" ]; then
echo -e "${YELLOW}Running in Codespace: $CODESPACE_NAME${NC}"
echo -e "${YELLOW}Public MCP URL: https://$CODESPACE_NAME-8000.app.github.dev/mcp/sse${NC}"
echo -e "${YELLOW}NOTE: Ensure port 8000 is set to Public visibility in the Ports tab.${NC}"
fi
# Ignore SIGINT until uvicorn registers its own signal handlers.
# In Codespace terminals, a spurious SIGINT is delivered during process startup
# which kills the server before it finishes initializing. The ignored disposition
# is inherited across exec, and uvicorn's reloader overrides it with its own
# handler via signal.signal(), so Ctrl+C still works once the server is ready.
trap '' INT
exec uvicorn backend.api_host.server:get_app --factory --reload --host 0.0.0.0 --port 8000 \
--reload-dir backend