Compare commits

..

No commits in common. "7b48f04fd13e05890aff4ef4781ce0eb6027ad01" and "177f2ad8e72da01cc73cac29973c2e00e61a5dab" have entirely different histories.

8 changed files with 65 additions and 323 deletions

View file

@ -1,38 +0,0 @@
#!/bin/bash
echo "=========================================="
echo "Backend Diagnostic Script for 10.0.10.30"
echo "=========================================="
echo ""
echo "1. Checking Docker containers..."
docker ps --filter "name=normogen" --format "table {{.Names}} {{.Status}} {{.Ports}}"
echo ""
echo "2. Checking if port 6500 is listening..."
sudo ss -tlnp | grep 6500 || echo "Port 6500 NOT listening"
echo ""
echo "3. Checking firewall status..."
sudo ufw status || echo "UFW not installed"
echo ""
echo "4. Checking Docker network..."
docker network ls | grep normogen
echo ""
echo "5. Backend container logs (last 20 lines)..."
docker logs --tail 20 normogen-backend-dev 2>&1
echo ""
echo "6. Testing local connection..."
curl -s --max-time 3 http://localhost:6500/health || echo "Local connection failed"
echo ""
echo "7. Testing container connection..."
docker exec normogen-backend-dev curl -s --max-time 3 http://localhost:8000/health || echo "Container connection failed"
echo ""
echo "=========================================="
echo "Diagnostic complete"
echo "=========================================="

View file

@ -11,6 +11,7 @@ services:
- '6500:8000' - '6500:8000'
volumes: volumes:
- ./src:/app/src - ./src:/app/src
# Mount a volume to capture logs
- startup-logs:/tmp - startup-logs:/tmp
environment: environment:
- RUST_LOG=debug - RUST_LOG=debug
@ -25,8 +26,8 @@ services:
condition: service_healthy condition: service_healthy
networks: networks:
- normogen-network - normogen-network
restart: unless-stopped # DISABLE RESTART TEMPORARILY TO DEBUG
restart: "no"
mongodb: mongodb:
image: mongo:6.0 image: mongo:6.0
container_name: normogen-mongodb-dev container_name: normogen-mongodb-dev
@ -46,14 +47,11 @@ services:
timeout: 5s timeout: 5s
retries: 5 retries: 5
start_period: 60s start_period: 60s
restart: unless-stopped
volumes: volumes:
mongodb_dev_data: mongodb_dev_data:
driver: local driver: local
startup-logs: startup-logs:
driver: local driver: local
networks: networks:
normogen-network: normogen-network:
driver: bridge driver: bridge

View file

@ -3,46 +3,51 @@ services:
build: build:
context: . context: .
dockerfile: docker/Dockerfile dockerfile: docker/Dockerfile
args:
BUILDKIT_INLINE_CACHE: 0
pull_policy: build
container_name: normogen-backend container_name: normogen-backend
ports: ports:
- '8000:8000' - '6800:8000'
environment: environment:
- RUST_LOG=info - RUST_LOG=info
- SERVER_PORT=8000 - SERVER_PORT=8000
- SERVER_HOST=0.0.0.0
- MONGODB_URI=mongodb://mongodb:27017 - MONGODB_URI=mongodb://mongodb:27017
- MONGODB_DATABASE=normogen - DATABASE_NAME=normogen
- JWT_SECRET=${JWT_SECRET:-please-change-this-in-production} env_file:
- .env
depends_on: depends_on:
mongodb: mongodb:
condition: service_healthy condition: service_healthy
networks: networks:
- normogen-network - normogen-network
restart: unless-stopped restart: unless-stopped
# Disable DNS search domain to fix hostname resolution deploy:
dns_search: [] resources:
limits:
cpus: '1.0'
memory: 1000M
healthcheck:
test: ['CMD', 'wget', '--no-verbose', '--tries=1', '--spider', 'http://localhost:8000/health']
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
mongodb: mongodb:
image: mongo:6.0 image: mongo:6.0
container_name: normogen-mongodb container_name: normogen-mongodb
ports:
- '27017:27017'
environment: environment:
- MONGO_INITDB_DATABASE=normogen - MONGO_INITDB_DATABASE=normogen
volumes: volumes:
- mongodb_data:/data/db - mongodb_data:/data/db
networks: networks:
- normogen-network - normogen-network
healthcheck:
test: |
echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
interval: 30s
timeout: 10s
retries: 5
start_period: 40s
restart: unless-stopped restart: unless-stopped
# Disable DNS search domain to fix hostname resolution healthcheck:
dns_search: [] test: ['CMD', 'mongosh', '--eval', 'db.adminCommand.ping()']
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
volumes: volumes:
mongodb_data: mongodb_data:
driver: local driver: local

View file

@ -21,71 +21,25 @@ impl MongoDb {
eprintln!("[MongoDB] Starting connection to: {}", uri); eprintln!("[MongoDB] Starting connection to: {}", uri);
// Parse the URI first // Parse the URI first
let mut client_options = match ClientOptions::parse(uri).await { let mut client_options = ClientOptions::parse(uri).await
Ok(opts) => { .map_err(|e| {
eprintln!("[MongoDB] Failed to parse URI: {}", e);
anyhow::anyhow!("Failed to parse MongoDB URI: {}", e)
})?;
eprintln!("[MongoDB] URI parsed successfully"); eprintln!("[MongoDB] URI parsed successfully");
opts
}
Err(e) => {
eprintln!("[MongoDB] ERROR: Failed to parse URI: {}", e);
let error_msg = e.to_string().to_lowercase(); // Set connection timeout
if error_msg.contains("dns") || error_msg.contains("resolution") || error_msg.contains("lookup") { client_options.server_selection_timeout = Some(Duration::from_secs(5));
eprintln!("[MongoDB] DNS RESOLUTION ERROR DETECTED!"); client_options.connect_timeout = Some(Duration::from_secs(5));
eprintln!("[MongoDB] Cannot resolve hostname in: {}", uri);
eprintln!("[MongoDB] Error: {}", e);
}
eprintln!("[MongoDB] Will continue in degraded mode (database operations will fail)");
// Create a minimal configuration that will allow the server to start
// but database operations will fail gracefully
let mut opts = ClientOptions::parse("mongodb://localhost:27017").await
.map_err(|e| anyhow::anyhow!("Failed to create fallback client options: {}", e))?;
opts.server_selection_timeout = Some(Duration::from_secs(1));
opts.connect_timeout = Some(Duration::from_secs(1));
let client = Client::with_options(opts)
.map_err(|e| anyhow::anyhow!("Failed to create MongoDB client: {}", e))?;
let database = client.database(db_name);
return Ok(Self {
users: database.collection("users"),
shares: database.collection("shares"),
database,
});
}
};
// Set connection timeout with retry logic
client_options.server_selection_timeout = Some(Duration::from_secs(10));
client_options.connect_timeout = Some(Duration::from_secs(10));
eprintln!("[MongoDB] Connecting to server..."); eprintln!("[MongoDB] Connecting to server...");
let client = match Client::with_options(client_options) { let client = Client::with_options(client_options)
Ok(c) => { .map_err(|e| {
eprintln!("[MongoDB] Client created successfully"); eprintln!("[MongoDB] Failed to create client: {}", e);
c anyhow::anyhow!("Failed to create MongoDB client: {}", e)
} })?;
Err(e) => {
eprintln!("[MongoDB] ERROR: Failed to create client: {}", e);
eprintln!("[MongoDB] Will continue in degraded mode");
// Create a fallback client
let fallback_opts = ClientOptions::parse("mongodb://localhost:27017").await
.map_err(|e| anyhow::anyhow!("Failed to create fallback client options: {}", e))?;
let fallback_client = Client::with_options(fallback_opts)
.map_err(|e| anyhow::anyhow!("Failed to create MongoDB client: {}", e))?;
let database = fallback_client.database(db_name);
return Ok(Self {
users: database.collection("users"),
shares: database.collection("shares"),
database,
});
}
};
eprintln!("[MongoDB] Client created, selecting database..."); eprintln!("[MongoDB] Client created, selecting database...");
@ -102,19 +56,10 @@ impl MongoDb {
pub async fn health_check(&self) -> Result<String> { pub async fn health_check(&self) -> Result<String> {
eprintln!("[MongoDB] Health check: pinging database..."); eprintln!("[MongoDB] Health check: pinging database...");
match self.database.run_command(doc! { "ping": 1 }, None).await { self.database.run_command(doc! { "ping": 1 }, None).await?;
Ok(_) => {
eprintln!("[MongoDB] Health check: OK"); eprintln!("[MongoDB] Health check: OK");
Ok("OK".to_string()) Ok("OK".to_string())
} }
Err(e) => {
eprintln!("[MongoDB] Health check: FAILED - {}", e);
// Return OK anyway to allow the server to continue running
// The actual database operations will fail when needed
Ok("DEGRADED".to_string())
}
}
}
// ===== User Methods ===== // ===== User Methods =====

View file

@ -1,128 +1,28 @@
mod config; use std::fs::OpenOptions;
mod db; use std::io::Write;
mod models;
mod auth;
mod handlers;
mod middleware;
use axum::{ fn main() {
routing::{get, post, put, delete}, let msg = format!("BINARY STARTED: {}\n", chrono::Utc::now().to_rfc3339());
Router,
middleware as axum_middleware,
};
use tower::ServiceBuilder;
use tower_http::{
cors::CorsLayer,
trace::TraceLayer,
};
use config::Config;
#[tokio::main] // Try to write to file
async fn main() -> anyhow::Result<()> { if let Ok(mut file) = OpenOptions::new()
eprintln!("NORMOGEN BACKEND STARTING..."); .create(true)
eprintln!("Loading environment variables..."); .write(true)
.truncate(true)
match dotenv::dotenv() { .open("/tmp/test-startup.log")
Ok(path) => eprintln!("Loaded .env from: {:?}", path), {
Err(e) => eprintln!("No .env file found (this is OK in Docker): {}", e), let _ = file.write_all(msg.as_bytes());
let _ = file.flush();
} }
eprintln!("Initializing logging..."); // Try to print to stdout
tracing_subscriber::fmt() println!("BINARY STARTED");
.with_env_filter( let _ = std::io::stdout().flush();
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "normogen_backend=debug,tower_http=debug,axum=debug".into())
)
.init();
eprintln!("Loading configuration..."); // Try to print to stderr
let config = match Config::from_env() { eprintln!("BINARY STARTED (stderr)");
Ok(cfg) => { let _ = std::io::stderr().flush();
tracing::info!("Configuration loaded successfully");
eprintln!("Config loaded: DB={}, Port={}", cfg.database.database, cfg.server.port);
cfg
}
Err(e) => {
eprintln!("FATAL: Failed to load configuration: {}", e);
return Err(e);
}
};
tracing::info!("Connecting to MongoDB at {}", config.database.uri); // Sleep to keep container alive
eprintln!("Connecting to MongoDB..."); std::thread::sleep(std::time::Duration::from_secs(60));
let db = match db::MongoDb::new(&config.database.uri, &config.database.database).await {
Ok(db) => {
tracing::info!("Connected to MongoDB database: {}", config.database.database);
eprintln!("MongoDB connection successful");
db
}
Err(e) => {
eprintln!("FATAL: Failed to connect to MongoDB: {}", e);
return Err(e);
}
};
tracing::info!("MongoDB health check: {}", db.health_check().await?);
let jwt_service = auth::JwtService::new(config.jwt.clone());
let app_state = config::AppState {
db,
jwt_service,
config: config.clone(),
};
eprintln!("Building router...");
let public_routes = Router::new()
.route("/health", get(handlers::health_check))
.route("/ready", get(handlers::ready_check))
.route("/api/auth/register", post(handlers::register))
.route("/api/auth/login", post(handlers::login))
.route("/api/auth/recover-password", post(handlers::recover_password))
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(CorsLayer::new())
);
let protected_routes = Router::new()
// Profile management
.route("/api/users/me", get(handlers::get_profile))
.route("/api/users/me", put(handlers::update_profile))
.route("/api/users/me", delete(handlers::delete_account))
// Account settings
.route("/api/users/me/settings", get(handlers::get_settings))
.route("/api/users/me/settings", put(handlers::update_settings))
.route("/api/users/me/change-password", post(handlers::change_password))
// Share management (Phase 2.5)
.route("/api/shares", post(handlers::create_share))
.route("/api/shares", get(handlers::list_shares))
.route("/api/shares/:id", get(handlers::get_share))
.route("/api/shares/:id", put(handlers::update_share))
.route("/api/shares/:id", delete(handlers::delete_share))
// Permissions (Phase 2.5)
.route("/api/permissions/check", post(handlers::check_permission))
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(CorsLayer::new())
)
.route_layer(axum_middleware::from_fn_with_state(
app_state.clone(),
crate::middleware::auth::jwt_auth_middleware
));
let app = public_routes.merge(protected_routes).with_state(app_state);
eprintln!("Binding to {}:{}...", config.server.host, config.server.port);
let listener = tokio::net::TcpListener::bind(&format!("{}:{}", config.server.host, config.server.port))
.await?;
tracing::info!("Server listening on {}:{}", config.server.host, config.server.port);
eprintln!("Server is running on http://{}:{}", config.server.host, config.server.port);
axum::serve(listener, app).await?;
Ok(())
} }

View file

@ -1,2 +0,0 @@
fatal: path 'backend/src/main.rs' exists, but not 'src/main.rs'
hint: Did you mean 'a316699:backend/src/main.rs' aka 'a316699:./src/main.rs'?

View file

@ -1,57 +0,0 @@
#!/bin/bash
set -e
echo "🚀 Starting Normogen Backend Development Environment..."
cd "$(dirname "$0")"
# Build and start containers
echo "📦 Building and starting containers..."
docker-compose -f docker-compose.dev.yml up --build -d
# Wait for MongoDB to be healthy
echo "⏳ Waiting for MongoDB to be healthy..."
timeout=60
while [ $timeout -gt 0 ]; do
if docker inspect normogen-mongodb-dev --format='{{.State.Health.Status}}' | grep -q "healthy"; then
echo "✅ MongoDB is healthy!"
break
fi
sleep 2
timeout=$((timeout - 2))
done
if [ $timeout -eq 0 ]; then
echo "❌ MongoDB health check timeout"
exit 1
fi
# Wait for backend to be ready
echo "⏳ Waiting for backend to start..."
timeout=30
while [ $timeout -gt 0 ]; do
if curl -s http://localhost:6500/health > /dev/null 2>&1; then
echo "✅ Backend is ready!"
break
fi
sleep 2
timeout=$((timeout - 2))
done
if [ $timeout -eq 0 ]; then
echo "⚠️ Backend may still be starting..."
fi
echo ""
echo "🎉 Development environment is ready!"
echo ""
echo "📊 Services:"
docker-compose -f docker-compose.dev.yml ps
echo ""
echo "🔗 Backend: http://localhost:6500"
echo "🔗 MongoDB: mongodb://localhost:27017"
echo ""
echo "📝 To view logs:"
echo " docker-compose -f docker-compose.dev.yml logs -f"
echo ""
echo "🛑 To stop:"
echo " ./stop-dev.sh"

View file

@ -1,9 +0,0 @@
#!/bin/bash
set -e
echo "🛑 Stopping Normogen Backend Development Environment..."
cd "$(dirname "$0")"
docker-compose -f docker-compose.dev.yml down
echo "✅ Development environment stopped!"