feat: with initial
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
commit
75bf2e0c71
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
target/
|
||||
.cuddle/
|
1947
Cargo.lock
generated
Normal file
1947
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
||||
[workspace]
|
||||
members = ["crates/*"]
|
||||
resolver = "2"
|
||||
|
||||
[workspace.dependencies]
|
||||
bearing = { path = "crates/bearing" }
|
||||
|
||||
anyhow = { version = "1.0.71" }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
tracing = { version = "0.1", features = ["log"] }
|
||||
tracing-subscriber = { version = "0.3.17" }
|
||||
clap = { version = "4.3.4", features = ["derive", "env"] }
|
||||
dotenv = { version = "0.15.0" }
|
||||
axum = { version = "0.6.18" }
|
||||
serde = { version = "1", features = ["derive"] }
|
1
crates/bearing/.gitignore
vendored
Normal file
1
crates/bearing/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
25
crates/bearing/Cargo.toml
Normal file
25
crates/bearing/Cargo.toml
Normal file
@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "bearing"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
clap.workspace = true
|
||||
dotenv.workspace = true
|
||||
axum.workspace = true
|
||||
serde.workspace = true
|
||||
|
||||
sqlx = { version = "0.6", features = [
|
||||
"runtime-tokio-rustls",
|
||||
"postgres",
|
||||
"migrate",
|
||||
"uuid",
|
||||
"offline",
|
||||
"time",
|
||||
"chrono",
|
||||
] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
9
crates/bearing/build.rs
Normal file
9
crates/bearing/build.rs
Normal file
@ -0,0 +1,9 @@
|
||||
fn main() {
|
||||
println!("cargo:rustc-env=SQLX_OFFLINE_DIR='./.sqlx'");
|
||||
// When building in docs.rs, we want to set SQLX_OFFLINE mode to true
|
||||
if std::env::var_os("DOCS_RS").is_some() {
|
||||
println!("cargo:rustc-env=SQLX_OFFLINE=true");
|
||||
} else if std::env::var_os("DOCKER_BUILD").is_some() {
|
||||
println!("cargo:rustc-env=SQLX_OFFLINE=true");
|
||||
}
|
||||
}
|
20
crates/bearing/src/controllers/fleet.rs
Normal file
20
crates/bearing/src/controllers/fleet.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use axum::response::IntoResponse;
|
||||
use axum::{Json, Router};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::db::conn_pool::ConnectionPool;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
struct RegisterRequest {}
|
||||
|
||||
pub struct FleetController {}
|
||||
|
||||
impl FleetController {
|
||||
pub fn new_router(pool: ConnectionPool) -> Router {
|
||||
Router::new().with_state(pool)
|
||||
}
|
||||
|
||||
pub async fn register(Json(request): Json<RegisterRequest>) -> impl IntoResponse {
|
||||
"register"
|
||||
}
|
||||
}
|
1
crates/bearing/src/controllers/mod.rs
Normal file
1
crates/bearing/src/controllers/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod fleet;
|
33
crates/bearing/src/db/conn_pool.rs
Normal file
33
crates/bearing/src/db/conn_pool.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use anyhow::Context;
|
||||
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
|
||||
use tracing::log::info;
|
||||
|
||||
pub type ConnectionPool = Pool<Postgres>;
|
||||
|
||||
pub struct ConnectionPoolManager;
|
||||
|
||||
impl ConnectionPoolManager {
|
||||
pub async fn new_pool(
|
||||
connection_string: &str,
|
||||
run_migrations: bool,
|
||||
) -> anyhow::Result<ConnectionPool> {
|
||||
info!("initializing the database connection pool");
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(connection_string)
|
||||
.await
|
||||
.context("error while initializing the database connection pool")?;
|
||||
|
||||
if run_migrations {
|
||||
info!("migrations enabled");
|
||||
info!("migrating database");
|
||||
|
||||
sqlx::migrate!()
|
||||
.run(&pool)
|
||||
.await
|
||||
.context("error while running database migrations")?;
|
||||
}
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
}
|
1
crates/bearing/src/db/mod.rs
Normal file
1
crates/bearing/src/db/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod conn_pool;
|
63
crates/bearing/src/main.rs
Normal file
63
crates/bearing/src/main.rs
Normal file
@ -0,0 +1,63 @@
|
||||
mod controllers;
|
||||
mod db;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use crate::controllers::fleet::FleetController;
|
||||
|
||||
use self::db::conn_pool::ConnectionPoolManager;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
||||
struct Command {
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
|
||||
#[arg(env = "BEARING_DB_URL", long)]
|
||||
db_url: String,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Serve {
|
||||
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")]
|
||||
host: SocketAddr,
|
||||
},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let cli = Command::parse();
|
||||
|
||||
let pool = ConnectionPoolManager::new_pool(&cli.db_url, true).await?;
|
||||
|
||||
match cli.command {
|
||||
Some(Commands::Serve { host }) => {
|
||||
tracing::info!("Starting service");
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.nest("/fleet", FleetController::new_router(pool.clone()));
|
||||
|
||||
tracing::info!("listening on {}", host);
|
||||
axum::Server::bind(&host)
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn root() -> &'static str {
|
||||
"Hello, bearing!"
|
||||
}
|
8
crates/bushing/Cargo.toml
Normal file
8
crates/bushing/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "bushing"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
3
crates/bushing/src/main.rs
Normal file
3
crates/bushing/src/main.rs
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
7
cuddle.yaml
Normal file
7
cuddle.yaml
Normal file
@ -0,0 +1,7 @@
|
||||
# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json
|
||||
|
||||
base: "git@git.front.kjuulh.io:kjuulh/cuddle-rust-service-plan.git"
|
||||
|
||||
vars:
|
||||
service: "bearing"
|
||||
registry: kasperhermansen
|
Loading…
Reference in New Issue
Block a user