diff --git a/cuddle-rust-service/.drone.yml b/cuddle-rust-service/.drone.yml new file mode 100644 index 0000000..2813783 --- /dev/null +++ b/cuddle-rust-service/.drone.yml @@ -0,0 +1,155 @@ +kind: pipeline +name: default +type: docker + +steps: + - name: load_secret + image: debian:buster-slim + volumes: + - name: ssh + path: /root/.ssh/ + environment: + SSH_KEY: + from_secret: gitea_id_ed25519 + commands: + - mkdir -p $HOME/.ssh/ + - echo "$SSH_KEY" | base64 -d > $HOME/.ssh/id_ed25519 + - chmod -R 600 ~/.ssh + - | + cat >$HOME/.ssh/config <, +} + +#[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(); + + if let Some(Commands::Serve { host }) = cli.command { + tracing::info!("Starting service"); + + let state = SharedState(Arc::new(State::new().await?)); + + let app = Router::new() + .route("/", get(root)) + .with_state(state.clone()) + .layer( + TraceLayer::new_for_http().make_span_with(|request: &Request<_>| { + // Log the matched route's path (with placeholders not filled in). + // Use request.uri() or OriginalUri if you want the real path. + let matched_path = request + .extensions() + .get::() + .map(MatchedPath::as_str); + + tracing::info_span!( + "http_request", + method = ?request.method(), + matched_path, + some_other_field = tracing::field::Empty, + ) + }), // ... + ); + + tracing::info!("listening on {}", host); + let listener = tokio::net::TcpListener::bind(host).await.unwrap(); + axum::serve(listener, app.into_make_service()) + .await + .unwrap(); + } + + Ok(()) +} + +async fn root() -> &'static str { + "Hello, %%name%%!" +} + +#[derive(Clone)] +pub struct SharedState(Arc); + +impl Deref for SharedState { + type Target = Arc; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +pub struct State { + pub db: Pool, +} + +impl State { + pub async fn new() -> anyhow::Result { + let db = sqlx::PgPool::connect( + &std::env::var("DATABASE_URL").context("DATABASE_URL is not set")?, + ) + .await?; + + sqlx::migrate!("migrations/crdb") + .set_locking(false) + .run(&db) + .await?; + + let _ = sqlx::query("SELECT 1;").fetch_one(&db).await?; + + Ok(Self { db }) + } +} + diff --git a/cuddle-rust-service/cuddle-template.json b/cuddle-rust-service/cuddle-template.json new file mode 100644 index 0000000..ccbe567 --- /dev/null +++ b/cuddle-rust-service/cuddle-template.json @@ -0,0 +1,10 @@ +{ + "name": "cuddle-rust-service", + "templating": "tera", + "delimiter": "[[]]", + "prompt": { + "name": { + "description": "Project name" + } + } +} \ No newline at end of file diff --git a/cuddle-rust-service/cuddle.yaml b/cuddle-rust-service/cuddle.yaml new file mode 100644 index 0000000..ea2fdbc --- /dev/null +++ b/cuddle-rust-service/cuddle.yaml @@ -0,0 +1,21 @@ +# 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: "%%name%%" + registry: kasperhermansen + + clusters: + clank-prod: + replicas: "3" + namespace: prod + + +deployment: + registry: git@git.front.kjuulh.io:kjuulh/clank-clusters + env: + prod: + clusters: + - clank-prod + diff --git a/cuddle-rust-service/renovate.json b/cuddle-rust-service/renovate.json new file mode 100644 index 0000000..7190a60 --- /dev/null +++ b/cuddle-rust-service/renovate.json @@ -0,0 +1,3 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json" +} diff --git a/cuddle-rust-service/templates/docker-compose.yaml b/cuddle-rust-service/templates/docker-compose.yaml new file mode 100644 index 0000000..8fae72a --- /dev/null +++ b/cuddle-rust-service/templates/docker-compose.yaml @@ -0,0 +1,15 @@ +version: "3" +services: + crdb: + restart: 'always' + image: 'cockroachdb/cockroach:v23.1.14' + command: 'start-single-node --advertise-addr 0.0.0.0 --insecure' + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8080/health?ready=1"] + interval: '10s' + timeout: '30s' + retries: 5 + start_period: '20s' + ports: + - 8080:8080 + - '26257:26257' diff --git a/cuddle-templates.json b/cuddle-templates.json index 3228901..7d36b4a 100644 --- a/cuddle-templates.json +++ b/cuddle-templates.json @@ -5,6 +5,7 @@ "rust-lib", "empty", "deployment", - "cuddle-infrastructure" + "cuddle-infrastructure", + "cuddle-rust-service" ] -} \ No newline at end of file +}