Compare commits
8 Commits
v0.0.3
...
8a6f8b5875
| Author | SHA1 | Date | |
|---|---|---|---|
| 8a6f8b5875 | |||
| 79e43f19df | |||
| 20f64cd35d | |||
| 6bd6dbad38 | |||
| df81605ecc | |||
| 8c12ca0024 | |||
| 70835f04fa | |||
| e7eb9b8b16 |
@ -41,6 +41,20 @@ jobs:
|
|||||||
run: cargo build --release --locked
|
run: cargo build --release --locked
|
||||||
|
|
||||||
test:
|
test:
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
image: postgres:17-alpine
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
env:
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: postgres
|
||||||
|
POSTGRES_DATABASE: nuchat_test
|
||||||
|
options: >-
|
||||||
|
--health-cmd pg_isready
|
||||||
|
--health-interval 10s
|
||||||
|
--health-timeout 5s
|
||||||
|
--health-retries 5
|
||||||
runs-on: rust-nextest
|
runs-on: rust-nextest
|
||||||
defaults:
|
defaults:
|
||||||
run:
|
run:
|
||||||
@ -59,6 +73,8 @@ jobs:
|
|||||||
run: cargo build --locked --bin nuchat
|
run: cargo build --locked --bin nuchat
|
||||||
- name: Run Tests
|
- name: Run Tests
|
||||||
run: ./scripts/test.sh
|
run: ./scripts/test.sh
|
||||||
|
env:
|
||||||
|
POSTGRES_URL: "postgresql://postgres:postgres@postgres:5432"
|
||||||
- name: Upload Test Logs
|
- name: Upload Test Logs
|
||||||
if: ${{ failure() }}
|
if: ${{ failure() }}
|
||||||
uses: actions/upload-artifact@v3
|
uses: actions/upload-artifact@v3
|
||||||
|
|||||||
72
.gitea/workflows/frontend.yaml
Normal file
72
.gitea/workflows/frontend.yaml
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
name: Frontend Actions
|
||||||
|
run-name: ${{ gitea.actor }} is running frontend actions
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
check:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: ./ui
|
||||||
|
steps:
|
||||||
|
- uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: '24'
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Load Cache
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
ui/node_modules
|
||||||
|
key: ${{ runner.os }}-node-check-${{ hashFiles('ui/package-lock.json') }}
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: npm ci
|
||||||
|
- name: Run Eslint
|
||||||
|
run: npm run lint
|
||||||
|
- name: Run Prettier
|
||||||
|
run: npm run fmt -- . --check
|
||||||
|
|
||||||
|
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: ./ui
|
||||||
|
steps:
|
||||||
|
- uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: '24'
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Load Cache
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
ui/node_modules
|
||||||
|
key: ${{ runner.os }}-node-build-${{ hashFiles('ui/package-lock.json') }}
|
||||||
|
- run: node --version
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: npm ci
|
||||||
|
- name: Build UI
|
||||||
|
run: npm run build
|
||||||
|
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: ./ui
|
||||||
|
steps:
|
||||||
|
- uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: '24'
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Load Cache
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
ui/node_modules
|
||||||
|
key: ${{ runner.os }}-node-test-${{ hashFiles('ui/package-lock.json') }}
|
||||||
|
- run: node --version
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: npm ci
|
||||||
|
- name: Run tests
|
||||||
|
run: npm run test
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
|||||||
.env
|
.env
|
||||||
package/
|
package/
|
||||||
package.tar.zst
|
*.tar.zst
|
||||||
|
|||||||
860
backend/Cargo.lock
generated
860
backend/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -9,9 +9,10 @@ clap = { version = "4.5.41", features = ["derive"] }
|
|||||||
http = "1.3.1"
|
http = "1.3.1"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0.141"
|
serde_json = "1.0.141"
|
||||||
|
sqlx = { version = "0.8.6", features = ["postgres", "macros", "runtime-tokio"] }
|
||||||
tokio = { version = "1.0", features = ["full"] }
|
tokio = { version = "1.0", features = ["full"] }
|
||||||
tower = { version = "0.5.2", features = ["full"] }
|
tower = { version = "0.5.2", features = ["full"] }
|
||||||
tower-http = { version = "0.6.6", features = ["timeout", "trace", "auth"] }
|
tower-http = { version = "0.6.6", features = ["timeout", "trace", "auth", "request-id"] }
|
||||||
tower-http-util = "0.1.0"
|
tower-http-util = "0.1.0"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
|
|||||||
2
backend/scripts/create_test_db.sql
Normal file
2
backend/scripts/create_test_db.sql
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
DROP DATABASE IF EXISTS nuchat_test;
|
||||||
|
CREATE DATABASE nuchat_test;
|
||||||
@ -1,11 +1,21 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
POSTGRES_URL=${POSTGRES_URL:-"postgresql://postgres:postgres@localhost:5432"}
|
||||||
|
|
||||||
if ! command -v cargo-nextest > /dev/null 2>&1; then
|
if ! command -v cargo-nextest > /dev/null 2>&1; then
|
||||||
echo "Command not found cargo-nextest"
|
echo "Command not found cargo-nextest"
|
||||||
echo "Try installing with cargo install cargo-nextest"
|
echo "Try installing with cargo install cargo-nextest"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
psql "$POSTGRES_URL" -f ./scripts/create_test_db.sql
|
||||||
|
|
||||||
|
if [ "$?" -ne "0" ]; then
|
||||||
|
echo "Unable to connect to database, make sure it is started"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
if [ ! -d logs ]; then
|
if [ ! -d logs ]; then
|
||||||
mkdir logs
|
mkdir logs
|
||||||
fi
|
fi
|
||||||
@ -14,7 +24,7 @@ fi
|
|||||||
curl -s -X POST localhost:7001/admin/shutdown 2>&1 > /dev/null
|
curl -s -X POST localhost:7001/admin/shutdown 2>&1 > /dev/null
|
||||||
|
|
||||||
# start server
|
# start server
|
||||||
cargo run -- --port 7001 2>&1 > logs/nuchat.log &
|
cargo run -- --port 7001 --postgres-url "$POSTGRES_URL" 2>&1 > logs/nuchat.log &
|
||||||
|
|
||||||
# run tests
|
# run tests
|
||||||
cargo nextest run --color=always 2>&1 | tee logs/test-output.log
|
cargo nextest run --color=always 2>&1 | tee logs/test-output.log
|
||||||
|
|||||||
@ -3,4 +3,6 @@ pub struct Config {
|
|||||||
pub port: u32,
|
pub port: u32,
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub admin_secret: Option<String>,
|
pub admin_secret: Option<String>,
|
||||||
|
pub postgres_url: String,
|
||||||
|
pub database_name: String,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
mod config;
|
mod config;
|
||||||
mod router;
|
mod router;
|
||||||
|
mod state;
|
||||||
|
|
||||||
pub use config::Config;
|
pub use config::Config;
|
||||||
pub use router::app;
|
pub use router::app;
|
||||||
|
pub use state::{AppState, NuState};
|
||||||
|
|||||||
@ -1,8 +1,12 @@
|
|||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use nuchat::AppState;
|
||||||
use nuchat::Config;
|
use nuchat::Config;
|
||||||
|
use nuchat::NuState;
|
||||||
use nuchat::app;
|
use nuchat::app;
|
||||||
|
use sqlx::Pool;
|
||||||
|
use sqlx::Postgres;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tokio::signal;
|
use tokio::signal;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
@ -22,6 +26,14 @@ struct Args {
|
|||||||
/// Admin secret to use, leave blank to disable
|
/// Admin secret to use, leave blank to disable
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
admin_secret: Option<String>,
|
admin_secret: Option<String>,
|
||||||
|
|
||||||
|
/// postgres base url, should container users and host info
|
||||||
|
#[arg(long, default_value = "postgres://postgres:postgres@localhost:5432/")]
|
||||||
|
postgres_url: String,
|
||||||
|
|
||||||
|
/// name of database to use
|
||||||
|
#[arg(long, default_value = "nuchat_dev")]
|
||||||
|
database: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@ -37,16 +49,24 @@ async fn main() {
|
|||||||
.with(tracing_subscriber::fmt::layer().with_target(false))
|
.with(tracing_subscriber::fmt::layer().with_target(false))
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
|
let pool = Pool::<Postgres>::connect(&config.0.postgres_url)
|
||||||
|
.await
|
||||||
|
.expect("Could not connect to database");
|
||||||
|
|
||||||
let listener = TcpListener::bind(format!("{}:{}", config.0.host, config.0.port))
|
let listener = TcpListener::bind(format!("{}:{}", config.0.host, config.0.port))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||||
|
|
||||||
let (app, rx) = app(&config.0);
|
let state = AppState::new(NuState::new(pool.clone(), config.0));
|
||||||
axum::serve(listener, app)
|
let (app, rx) = app(&state);
|
||||||
|
axum::serve(listener, app.with_state(state))
|
||||||
.with_graceful_shutdown(shutdown_signal(rx))
|
.with_graceful_shutdown(shutdown_signal(rx))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
pool.close().await;
|
||||||
|
|
||||||
info!("Server stopped");
|
info!("Server stopped");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,6 +111,8 @@ impl BinConfig {
|
|||||||
port: args.port,
|
port: args.port,
|
||||||
host: args.host,
|
host: args.host,
|
||||||
admin_secret: args.admin_secret,
|
admin_secret: args.admin_secret,
|
||||||
|
postgres_url: args.postgres_url,
|
||||||
|
database_name: args.database,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,31 +3,51 @@ mod healthcheck;
|
|||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::config;
|
use crate::AppState;
|
||||||
use axum::extract::Request;
|
use axum::extract::Request;
|
||||||
use axum::routing::get;
|
use axum::routing::get;
|
||||||
use axum::{Router, body::Body};
|
use axum::{Router, body::Body};
|
||||||
|
use http::{HeaderName, HeaderValue};
|
||||||
use tower::ServiceBuilder;
|
use tower::ServiceBuilder;
|
||||||
|
use tower_http::request_id::{MakeRequestId, RequestId, SetRequestIdLayer};
|
||||||
use tower_http::timeout::TimeoutLayer;
|
use tower_http::timeout::TimeoutLayer;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::Level;
|
use tracing::Level;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub fn app(config: &config::Config) -> (Router, mpsc::Receiver<bool>) {
|
#[derive(Clone)]
|
||||||
|
struct RequestIdLayer;
|
||||||
|
|
||||||
|
impl MakeRequestId for RequestIdLayer {
|
||||||
|
fn make_request_id<B>(&mut self, _: &http::Request<B>) -> Option<RequestId> {
|
||||||
|
let id = Uuid::now_v7().to_string();
|
||||||
|
|
||||||
|
Some(RequestId::new(id.parse().unwrap()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn app(state: &AppState) -> (Router<AppState>, mpsc::Receiver<bool>) {
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
(
|
(
|
||||||
Router::new()
|
Router::new()
|
||||||
|
.with_state(state.clone())
|
||||||
.route("/healthcheck", get(healthcheck::healthcheck))
|
.route("/healthcheck", get(healthcheck::healthcheck))
|
||||||
.route("/forever", get(std::future::pending::<()>))
|
.route("/forever", get(std::future::pending::<()>))
|
||||||
.nest("/admin", admin::router(tx, config))
|
.nest("/admin", admin::router(tx, state))
|
||||||
.layer(
|
.layer(
|
||||||
ServiceBuilder::new()
|
ServiceBuilder::new()
|
||||||
|
.layer(SetRequestIdLayer::new(
|
||||||
|
HeaderName::from_static("x-request-id"),
|
||||||
|
RequestIdLayer,
|
||||||
|
))
|
||||||
.layer(
|
.layer(
|
||||||
TraceLayer::new_for_http().make_span_with(|req: &Request<Body>| {
|
TraceLayer::new_for_http().make_span_with(|req: &Request<Body>| {
|
||||||
|
let default = HeaderValue::from_static("<missing>");
|
||||||
|
let req_id = req.headers().get("x-request-id").unwrap_or(&default);
|
||||||
tracing::span!(
|
tracing::span!(
|
||||||
Level::DEBUG,
|
Level::DEBUG,
|
||||||
"request",
|
"request",
|
||||||
trace_id = Uuid::now_v7().to_string(),
|
req_id = req_id.to_str().unwrap(),
|
||||||
method = format!("{}", req.method()),
|
method = format!("{}", req.method()),
|
||||||
uri = format!("{}", req.uri()),
|
uri = format!("{}", req.uri()),
|
||||||
)
|
)
|
||||||
|
|||||||
@ -7,13 +7,17 @@ use http::StatusCode;
|
|||||||
use tower_http::validate_request::ValidateRequestHeaderLayer;
|
use tower_http::validate_request::ValidateRequestHeaderLayer;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
pub fn router(tx: mpsc::Sender<bool>, config: &crate::Config) -> Router {
|
use crate::AppState;
|
||||||
let r = Router::new().route("/", get(async || StatusCode::OK));
|
|
||||||
|
pub fn router(tx: mpsc::Sender<bool>, state: &AppState) -> Router<AppState> {
|
||||||
|
let r = Router::new()
|
||||||
|
.with_state(state.clone())
|
||||||
|
.route("/", get(async || StatusCode::OK));
|
||||||
|
|
||||||
let r = add_shutdown_endpoint(r, tx);
|
let r = add_shutdown_endpoint(r, tx);
|
||||||
if let Some(secret) = config.admin_secret.clone() {
|
if let Some(secret) = &state.config.admin_secret {
|
||||||
info!("Enabled admin authorization");
|
info!("Enabled admin authorization");
|
||||||
r.layer(ValidateRequestHeaderLayer::bearer(&secret))
|
r.layer(ValidateRequestHeaderLayer::bearer(secret))
|
||||||
} else {
|
} else {
|
||||||
warn!("Admin authorization disabled");
|
warn!("Admin authorization disabled");
|
||||||
r
|
r
|
||||||
@ -21,7 +25,7 @@ pub fn router(tx: mpsc::Sender<bool>, config: &crate::Config) -> Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "shutdown")]
|
#[cfg(feature = "shutdown")]
|
||||||
fn add_shutdown_endpoint(r: Router, tx: mpsc::Sender<bool>) -> Router {
|
fn add_shutdown_endpoint(r: Router<AppState>, tx: mpsc::Sender<bool>) -> Router<AppState> {
|
||||||
r.route(
|
r.route(
|
||||||
"/shutdown",
|
"/shutdown",
|
||||||
post(async move || {
|
post(async move || {
|
||||||
@ -44,17 +48,21 @@ fn add_shutdown_endpoint(r: Router, _: mpsc::Sender<bool>) -> Router {
|
|||||||
mod test {
|
mod test {
|
||||||
use axum::{body::Body, http::Request};
|
use axum::{body::Body, http::Request};
|
||||||
use http::header;
|
use http::header;
|
||||||
|
use sqlx::PgPool;
|
||||||
use tower::ServiceExt;
|
use tower::ServiceExt;
|
||||||
|
|
||||||
use crate::config;
|
use crate::{config, state::NuState};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[tokio::test]
|
#[sqlx::test]
|
||||||
async fn test_authorization_disables_when_no_secret_set() {
|
async fn test_authorization_disables_when_no_secret_set(pool: PgPool) {
|
||||||
let (tx, _) = mpsc::channel();
|
let (tx, _) = mpsc::channel();
|
||||||
|
|
||||||
let resp = router(tx, &config::Config::default())
|
let state = AppState::new(NuState::new(pool, config::Config::default()));
|
||||||
|
|
||||||
|
let resp = router(tx, &state)
|
||||||
|
.with_state(state)
|
||||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -62,8 +70,8 @@ mod test {
|
|||||||
assert_eq!(resp.status(), StatusCode::OK);
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[sqlx::test]
|
||||||
async fn test_authorization_unauthorized_no_bearer_token() {
|
async fn test_authorization_unauthorized_no_bearer_token(pool: PgPool) {
|
||||||
let (tx, _) = mpsc::channel();
|
let (tx, _) = mpsc::channel();
|
||||||
|
|
||||||
let conf = config::Config {
|
let conf = config::Config {
|
||||||
@ -71,7 +79,10 @@ mod test {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let resp = router(tx, &conf)
|
let state = AppState::new(NuState::new(pool, conf));
|
||||||
|
|
||||||
|
let resp = router(tx, &state)
|
||||||
|
.with_state(state)
|
||||||
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -79,8 +90,8 @@ mod test {
|
|||||||
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[sqlx::test]
|
||||||
async fn test_authorization_unauthorized_invalid_bearer_token() {
|
async fn test_authorization_unauthorized_invalid_bearer_token(pool: PgPool) {
|
||||||
let (tx, _) = mpsc::channel();
|
let (tx, _) = mpsc::channel();
|
||||||
|
|
||||||
let conf = config::Config {
|
let conf = config::Config {
|
||||||
@ -88,7 +99,10 @@ mod test {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let resp = router(tx, &conf)
|
let state = AppState::new(NuState::new(pool, conf));
|
||||||
|
|
||||||
|
let resp = router(tx, &state)
|
||||||
|
.with_state(state)
|
||||||
.oneshot(
|
.oneshot(
|
||||||
Request::builder()
|
Request::builder()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
@ -102,8 +116,8 @@ mod test {
|
|||||||
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[sqlx::test]
|
||||||
async fn test_authorization_authorized_valid_bearer_token() {
|
async fn test_authorization_authorized_valid_bearer_token(pool: PgPool) {
|
||||||
let (tx, _) = mpsc::channel();
|
let (tx, _) = mpsc::channel();
|
||||||
|
|
||||||
let conf = config::Config {
|
let conf = config::Config {
|
||||||
@ -111,7 +125,10 @@ mod test {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let resp = router(tx, &conf)
|
let state = AppState::new(NuState::new(pool, conf));
|
||||||
|
|
||||||
|
let resp = router(tx, &state)
|
||||||
|
.with_state(state)
|
||||||
.oneshot(
|
.oneshot(
|
||||||
Request::builder()
|
Request::builder()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
|||||||
@ -1,6 +1,78 @@
|
|||||||
|
use axum::extract::{self, State};
|
||||||
use axum::response::Json;
|
use axum::response::Json;
|
||||||
|
use http::StatusCode;
|
||||||
use serde_json::{Value, json};
|
use serde_json::{Value, json};
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
pub async fn healthcheck() -> Json<Value> {
|
use crate::AppState;
|
||||||
Json(json!({"healthy": true}))
|
|
||||||
|
pub async fn healthcheck(State(s): extract::State<AppState>) -> Result<Json<Value>, StatusCode> {
|
||||||
|
sqlx::query!(
|
||||||
|
"select exists(SELECT datname FROM pg_catalog.pg_database WHERE datname = $1);",
|
||||||
|
s.config.database_name
|
||||||
|
)
|
||||||
|
.fetch_one(&s.db)
|
||||||
|
.await
|
||||||
|
.and_then(|x| x.exists.ok_or(sqlx::Error::RowNotFound))
|
||||||
|
.and_then(|db| {
|
||||||
|
if db {
|
||||||
|
Ok(Json(json!({"healthy": db})))
|
||||||
|
} else {
|
||||||
|
error!("Could not find configured database in postgres");
|
||||||
|
Err(sqlx::Error::RowNotFound)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use axum::{Router, body::Body, routing::get};
|
||||||
|
use http::Request;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use tower::ServiceExt;
|
||||||
|
|
||||||
|
use crate::{Config, NuState};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[sqlx::test]
|
||||||
|
async fn healthcheck_passes_with_db_connection(pool: PgPool) {
|
||||||
|
let state = AppState::new(NuState::new(
|
||||||
|
pool,
|
||||||
|
Config {
|
||||||
|
database_name: String::from("nuchat_test"),
|
||||||
|
..Config::default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
let resp = Router::new()
|
||||||
|
.route("/", get(healthcheck))
|
||||||
|
.with_state(state)
|
||||||
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), StatusCode::OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[sqlx::test]
|
||||||
|
async fn healthcheck_fails_db_doesnt_exist(pool: PgPool) {
|
||||||
|
let state = AppState::new(NuState::new(
|
||||||
|
pool,
|
||||||
|
Config {
|
||||||
|
database_name: String::from("asdfasdfasdf"),
|
||||||
|
..Config::default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
let resp = Router::new()
|
||||||
|
.route("/", get(healthcheck))
|
||||||
|
.with_state(state)
|
||||||
|
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
backend/src/state.rs
Normal file
18
backend/src/state.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
use crate::Config;
|
||||||
|
|
||||||
|
pub type AppState = Arc<NuState>;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct NuState {
|
||||||
|
pub db: sqlx::PgPool,
|
||||||
|
pub config: Config,
|
||||||
|
}
|
||||||
|
impl NuState {
|
||||||
|
pub fn new(db: PgPool, config: Config) -> Self {
|
||||||
|
Self { db, config }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,6 +6,15 @@ services:
|
|||||||
command: --host 0.0.0.0
|
command: --host 0.0.0.0
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
- db
|
||||||
|
|
||||||
|
frontend:
|
||||||
|
build: ./ui
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
command: --host 0.0.0.0
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: postgres:17-alpine
|
image: postgres:17-alpine
|
||||||
environment:
|
environment:
|
||||||
|
|||||||
24
ui/.dockerignore
Normal file
24
ui/.dockerignore
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# Nuxt dev/build outputs
|
||||||
|
.output
|
||||||
|
.data
|
||||||
|
.nuxt
|
||||||
|
.nitro
|
||||||
|
.cache
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Node dependencies
|
||||||
|
node_modules
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Misc
|
||||||
|
.DS_Store
|
||||||
|
.fleet
|
||||||
|
.idea
|
||||||
|
|
||||||
|
# Local env files
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
15
ui/Dockerfile
Normal file
15
ui/Dockerfile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
FROM node:24-alpine AS base
|
||||||
|
|
||||||
|
FROM base AS build
|
||||||
|
WORKDIR /app
|
||||||
|
COPY package.json package-lock.json ./
|
||||||
|
RUN npm ci
|
||||||
|
COPY . .
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
FROM base AS prod
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=build /app/.output ./ui
|
||||||
|
ENV PORT=3000
|
||||||
|
|
||||||
|
ENTRYPOINT [ "node", "/app/ui/server/index.mjs" ]
|
||||||
@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<NuxtRouteAnnouncer />
|
<NuxtRouteAnnouncer />
|
||||||
<NuxtWelcome />
|
<NuxtPage />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
14
ui/app/pages/index.spec.ts
Normal file
14
ui/app/pages/index.spec.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// @noErrors
|
||||||
|
import { it, expect } from "vitest";
|
||||||
|
// ---cut---
|
||||||
|
// tests/components/SomeComponents.nuxt.spec.ts
|
||||||
|
import { mountSuspended } from "@nuxt/test-utils/runtime";
|
||||||
|
import Index from "~/pages/index.vue";
|
||||||
|
|
||||||
|
// tests/App.nuxt.spec.ts
|
||||||
|
it("can also mount an app", async () => {
|
||||||
|
const component = await mountSuspended(Index, { route: "/test" });
|
||||||
|
expect(component.html()).toMatchInlineSnapshot(`
|
||||||
|
"<h1>Hello World</h1>"
|
||||||
|
`);
|
||||||
|
});
|
||||||
3
ui/app/pages/index.vue
Normal file
3
ui/app/pages/index.vue
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<template>
|
||||||
|
<h1>Hello World</h1>
|
||||||
|
</template>
|
||||||
@ -1,6 +1,9 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
import withNuxt from './.nuxt/eslint.config.mjs'
|
import withNuxt from "./.nuxt/eslint.config.mjs";
|
||||||
|
|
||||||
export default withNuxt(
|
export default withNuxt({
|
||||||
// Your custom configs here
|
rules: {
|
||||||
)
|
"prefer-const": "warn",
|
||||||
|
"no-unexpected-multiline": 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|||||||
@ -1,6 +1,14 @@
|
|||||||
// https://nuxt.com/docs/api/configuration/nuxt-config
|
// https://nuxt.com/docs/api/configuration/nuxt-config
|
||||||
export default defineNuxtConfig({
|
export default defineNuxtConfig({
|
||||||
compatibilityDate: '2025-07-15',
|
compatibilityDate: "2025-07-15",
|
||||||
devtools: { enabled: true },
|
devtools: { enabled: true },
|
||||||
modules: ['@nuxt/eslint', '@nuxt/icon', '@nuxt/test-utils']
|
modules: [
|
||||||
})
|
"@nuxt/eslint",
|
||||||
|
"@nuxt/icon",
|
||||||
|
"@nuxt/test-utils",
|
||||||
|
"@nuxt/test-utils/module",
|
||||||
|
],
|
||||||
|
nitro: {
|
||||||
|
preset: "node-server",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|||||||
802
ui/package-lock.json
generated
802
ui/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -7,14 +7,31 @@
|
|||||||
"dev": "nuxt dev",
|
"dev": "nuxt dev",
|
||||||
"generate": "nuxt generate",
|
"generate": "nuxt generate",
|
||||||
"preview": "nuxt preview",
|
"preview": "nuxt preview",
|
||||||
"postinstall": "nuxt prepare"
|
"postinstall": "nuxt prepare",
|
||||||
|
"test": "vitest run",
|
||||||
|
"watch": "vitest watch",
|
||||||
|
"lint": "eslint",
|
||||||
|
"fmt": "prettier"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/eslint": "1.6.0",
|
"@nuxt/eslint": "1.6.0",
|
||||||
"@nuxt/icon": "^1.15.0",
|
"@nuxt/icon": "^1.15.0",
|
||||||
"@nuxt/test-utils": "^3.19.2",
|
|
||||||
"nuxt": "^4.0.1",
|
"nuxt": "^4.0.1",
|
||||||
"vue": "^3.5.17",
|
"vue": "^3.5.17",
|
||||||
"vue-router": "^4.5.1"
|
"vue-router": "^4.5.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@eslint/css": "^0.10.0",
|
||||||
|
"@eslint/js": "^9.32.0",
|
||||||
|
"@nuxt/test-utils": "^3.19.2",
|
||||||
|
"@vue/test-utils": "^2.4.6",
|
||||||
|
"eslint": "^9.32.0",
|
||||||
|
"eslint-plugin-vue": "^10.3.0",
|
||||||
|
"globals": "^16.3.0",
|
||||||
|
"happy-dom": "^18.0.1",
|
||||||
|
"playwright-core": "^1.54.1",
|
||||||
|
"prettier": "3.6.2",
|
||||||
|
"typescript-eslint": "^8.38.0",
|
||||||
|
"vitest": "^3.2.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
ui/vitest.config.ts
Normal file
7
ui/vitest.config.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { defineVitestConfig } from "@nuxt/test-utils/config";
|
||||||
|
|
||||||
|
export default defineVitestConfig({
|
||||||
|
test: {
|
||||||
|
environment: "nuxt",
|
||||||
|
},
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user