feat: with initial

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-07-01 23:43:39 +02:00
commit 75bf2e0c71
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
14 changed files with 2135 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target/
.cuddle/

1947
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

15
Cargo.toml Normal file
View 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
View File

@ -0,0 +1 @@
/target

25
crates/bearing/Cargo.toml Normal file
View 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
View 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");
}
}

View 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"
}
}

View File

@ -0,0 +1 @@
pub mod fleet;

View 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)
}
}

View File

@ -0,0 +1 @@
pub mod conn_pool;

View 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!"
}

View 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]

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

7
cuddle.yaml Normal file
View 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