Prep build setup for stack
This commit is contained in:
parent
3a761e3eb1
commit
bd2ffee9ae
14 changed files with 668 additions and 91 deletions
6
rust-engine/Cargo.lock
generated
Normal file
6
rust-engine/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
# This file will be generated when you first run cargo build
|
||||
# Leaving it as a placeholder for Docker caching
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
[package]
|
||||
name = "rust-engine"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
warp = "0.3"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "mysql", "chrono"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.3"
|
||||
dotenv = "0.15"
|
||||
cors = "0.6"
|
||||
anyhow = "1.0"
|
||||
|
|
@ -1,12 +1,30 @@
|
|||
# rust-engine/Dockerfile
|
||||
# --- Stage 1: Builder ---
|
||||
FROM rust:1.7-slim as builder
|
||||
FROM rust:1.82-slim AS builder
|
||||
WORKDIR /usr/src/app
|
||||
COPY . .
|
||||
|
||||
# Install build dependencies
|
||||
RUN apt-get update && apt-get install -y \
|
||||
pkg-config \
|
||||
libssl-dev \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Copy Cargo files for dependency caching
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
# Create a dummy src/main.rs for dependency build
|
||||
RUN mkdir src && echo "fn main() {}" > src/main.rs
|
||||
RUN cargo build --release && rm src/main.rs
|
||||
|
||||
# Copy source code and build
|
||||
COPY src ./src
|
||||
RUN cargo build --release
|
||||
|
||||
# --- Stage 2: Final Image ---
|
||||
FROM debian:buster-slim
|
||||
FROM debian:bookworm-slim
|
||||
RUN apt-get update && apt-get install -y \
|
||||
ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=builder /usr/src/app/target/release/rust-engine /usr/local/bin/rust-engine
|
||||
EXPOSE 8000
|
||||
CMD ["rust-engine"]
|
||||
132
rust-engine/src/main.rs
Normal file
132
rust-engine/src/main.rs
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
use std::env;
|
||||
use warp::Filter;
|
||||
use sqlx::mysql::MySqlPool;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing::{info, warn};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct HealthResponse {
|
||||
status: String,
|
||||
timestamp: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct ApiResponse<T> {
|
||||
success: bool,
|
||||
data: Option<T>,
|
||||
message: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize tracing
|
||||
tracing_subscriber::init();
|
||||
|
||||
// Load environment variables
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "mysql://astraadmin:password@mysql:3306/astra".to_string());
|
||||
|
||||
info!("Starting Rust Engine...");
|
||||
info!("Connecting to database: {}", database_url);
|
||||
|
||||
// Connect to database
|
||||
let pool = match MySqlPool::connect(&database_url).await {
|
||||
Ok(pool) => {
|
||||
info!("Successfully connected to database");
|
||||
pool
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to connect to database: {}. Starting without DB connection.", e);
|
||||
// In a hackathon setting, we might want to continue without DB for initial testing
|
||||
return start_server_without_db().await;
|
||||
}
|
||||
};
|
||||
|
||||
// CORS configuration
|
||||
let cors = warp::cors()
|
||||
.allow_any_origin()
|
||||
.allow_headers(vec!["content-type", "authorization"])
|
||||
.allow_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"]);
|
||||
|
||||
// Health check endpoint
|
||||
let health = warp::path("health")
|
||||
.and(warp::get())
|
||||
.map(|| {
|
||||
let response = HealthResponse {
|
||||
status: "healthy".to_string(),
|
||||
timestamp: chrono::Utc::now().to_rfc3339(),
|
||||
};
|
||||
warp::reply::json(&ApiResponse {
|
||||
success: true,
|
||||
data: Some(response),
|
||||
message: None,
|
||||
})
|
||||
});
|
||||
|
||||
// API routes - you'll expand these for your hackathon needs
|
||||
let api = warp::path("api")
|
||||
.and(
|
||||
health.or(
|
||||
// Add more routes here as needed
|
||||
warp::path("version")
|
||||
.and(warp::get())
|
||||
.map(|| {
|
||||
warp::reply::json(&ApiResponse {
|
||||
success: true,
|
||||
data: Some("1.0.0"),
|
||||
message: Some("Rust Engine API".to_string()),
|
||||
})
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
let routes = api
|
||||
.with(cors)
|
||||
.with(warp::log("rust_engine"));
|
||||
|
||||
info!("Rust Engine started on http://0.0.0.0:8000");
|
||||
|
||||
warp::serve(routes)
|
||||
.run(([0, 0, 0, 0], 8000))
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn start_server_without_db() -> Result<(), Box<dyn std::error::Error>> {
|
||||
info!("Starting server in DB-less mode for development");
|
||||
|
||||
let cors = warp::cors()
|
||||
.allow_any_origin()
|
||||
.allow_headers(vec!["content-type", "authorization"])
|
||||
.allow_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"]);
|
||||
|
||||
let health = warp::path("health")
|
||||
.and(warp::get())
|
||||
.map(|| {
|
||||
let response = HealthResponse {
|
||||
status: "healthy (no db)".to_string(),
|
||||
timestamp: chrono::Utc::now().to_rfc3339(),
|
||||
};
|
||||
warp::reply::json(&ApiResponse {
|
||||
success: true,
|
||||
data: Some(response),
|
||||
message: Some("Running without database connection".to_string()),
|
||||
})
|
||||
});
|
||||
|
||||
let routes = warp::path("api")
|
||||
.and(health)
|
||||
.with(cors)
|
||||
.with(warp::log("rust_engine"));
|
||||
|
||||
info!("Rust Engine started on http://0.0.0.0:8000 (DB-less mode)");
|
||||
|
||||
warp::serve(routes)
|
||||
.run(([0, 0, 0, 0], 8000))
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue