diff --git a/loco-gen/src/lib.rs b/loco-gen/src/lib.rs index 2f5f11daa..90ed56a93 100644 --- a/loco-gen/src/lib.rs +++ b/loco-gen/src/lib.rs @@ -15,30 +15,27 @@ mod scaffold; mod testutil; use std::{str::FromStr, sync::OnceLock}; -const CONTROLLER_T: &str = include_str!("templates/controller.t"); -const CONTROLLER_TEST_T: &str = include_str!("templates/request_test.t"); +const MAILER_T: &str = include_str!("templates/mailer/mailer.t"); +const MAILER_SUB_T: &str = include_str!("templates/mailer/subject.t"); +const MAILER_TEXT_T: &str = include_str!("templates/mailer/text.t"); +const MAILER_HTML_T: &str = include_str!("templates/mailer/html.t"); -const MAILER_T: &str = include_str!("templates/mailer.t"); -const MAILER_SUB_T: &str = include_str!("templates/mailer_sub.t"); -const MAILER_TEXT_T: &str = include_str!("templates/mailer_text.t"); -const MAILER_HTML_T: &str = include_str!("templates/mailer_html.t"); +const MIGRATION_T: &str = include_str!("templates/migration/migration.t"); -const MIGRATION_T: &str = include_str!("templates/migration.t"); +const TASK_T: &str = include_str!("templates/task/task.t"); +const TASK_TEST_T: &str = include_str!("templates/task/test.t"); -const TASK_T: &str = include_str!("templates/task.t"); -const TASK_TEST_T: &str = include_str!("templates/task_test.t"); +const SCHEDULER_T: &str = include_str!("templates/scheduler/scheduler.t"); -const SCHEDULER_T: &str = include_str!("templates/scheduler.t"); - -const WORKER_T: &str = include_str!("templates/worker.t"); -const WORKER_TEST_T: &str = include_str!("templates/worker_test.t"); +const WORKER_T: &str = include_str!("templates/worker/worker.t"); +const WORKER_TEST_T: &str = include_str!("templates/worker/test.t"); // Deployment templates -const DEPLOYMENT_DOCKER_T: &str = include_str!("templates/deployment_docker.t"); -const DEPLOYMENT_DOCKER_IGNORE_T: &str = include_str!("templates/deployment_docker_ignore.t"); -const DEPLOYMENT_SHUTTLE_T: &str = include_str!("templates/deployment_shuttle.t"); -const DEPLOYMENT_SHUTTLE_CONFIG_T: &str = include_str!("templates/deployment_shuttle_config.t"); -const DEPLOYMENT_NGINX_T: &str = include_str!("templates/deployment_nginx.t"); +const DEPLOYMENT_DOCKER_T: &str = include_str!("templates/deployment/docker/docker.t"); +const DEPLOYMENT_DOCKER_IGNORE_T: &str = include_str!("templates/deployment/docker/ignore.t"); +const DEPLOYMENT_SHUTTLE_T: &str = include_str!("templates/deployment/shuttle/shuttle.t"); +const DEPLOYMENT_SHUTTLE_CONFIG_T: &str = include_str!("templates/deployment/shuttle/config.t"); +const DEPLOYMENT_NGINX_T: &str = include_str!("templates/deployment/nginx/nginx.t"); const DEPLOYMENT_SHUTTLE_RUNTIME_VERSION: &str = "0.46.0"; diff --git a/loco-gen/src/model.rs b/loco-gen/src/model.rs index 0e1a74041..928f280be 100644 --- a/loco-gen/src/model.rs +++ b/loco-gen/src/model.rs @@ -8,8 +8,8 @@ use serde_json::json; use super::{Error, Result}; use crate::get_mappings; -const MODEL_T: &str = include_str!("templates/model.t"); -const MODEL_TEST_T: &str = include_str!("templates/model_test.t"); +const MODEL_T: &str = include_str!("templates/model/model.t"); +const MODEL_TEST_T: &str = include_str!("templates/model/test.t"); use super::{collect_messages, AppInfo}; diff --git a/loco-gen/src/templates/controller.t b/loco-gen/src/templates/controller.t deleted file mode 100644 index c1b4c202d..000000000 --- a/loco-gen/src/templates/controller.t +++ /dev/null @@ -1,35 +0,0 @@ -{% set file_name = name | snake_case -%} -{% set module_name = file_name | pascal_case -%} -to: src/controllers/{{ file_name }}.rs -skip_exists: true -message: "Controller `{{module_name}}` was added successfully." -injections: -- into: src/controllers/mod.rs - append: true - content: "pub mod {{ file_name }};" -- into: src/app.rs - after: "AppRoutes::" - content: " .add_route(controllers::{{ file_name }}::routes())" ---- -#![allow(clippy::unused_async)] -use loco_rs::prelude::*; -use axum::debug_handler; - - -#[debug_handler] -pub async fn echo(req_body: String) -> String { - req_body -} - -#[debug_handler] -pub async fn hello(State(_ctx): State) -> Result { - // do something with context (database, etc) - format::text("hello") -} - -pub fn routes() -> Routes { - Routes::new() - .prefix("{{ name | snake_case }}") - .add("/", get(hello)) - .add("/echo", post(echo)) -} diff --git a/loco-gen/src/templates/deployment_docker.t b/loco-gen/src/templates/deployment/docker/docker.t similarity index 100% rename from loco-gen/src/templates/deployment_docker.t rename to loco-gen/src/templates/deployment/docker/docker.t diff --git a/loco-gen/src/templates/deployment_docker_ignore.t b/loco-gen/src/templates/deployment/docker/ignore.t similarity index 100% rename from loco-gen/src/templates/deployment_docker_ignore.t rename to loco-gen/src/templates/deployment/docker/ignore.t diff --git a/loco-gen/src/templates/deployment_nginx.t b/loco-gen/src/templates/deployment/nginx/nginx.t similarity index 100% rename from loco-gen/src/templates/deployment_nginx.t rename to loco-gen/src/templates/deployment/nginx/nginx.t diff --git a/loco-gen/src/templates/deployment_shuttle_config.t b/loco-gen/src/templates/deployment/shuttle/config.t similarity index 100% rename from loco-gen/src/templates/deployment_shuttle_config.t rename to loco-gen/src/templates/deployment/shuttle/config.t diff --git a/loco-gen/src/templates/deployment_shuttle.t b/loco-gen/src/templates/deployment/shuttle/shuttle.t similarity index 100% rename from loco-gen/src/templates/deployment_shuttle.t rename to loco-gen/src/templates/deployment/shuttle/shuttle.t diff --git a/loco-gen/src/templates/mailer_html.t b/loco-gen/src/templates/mailer/html.t similarity index 100% rename from loco-gen/src/templates/mailer_html.t rename to loco-gen/src/templates/mailer/html.t diff --git a/loco-gen/src/templates/mailer.t b/loco-gen/src/templates/mailer/mailer.t similarity index 100% rename from loco-gen/src/templates/mailer.t rename to loco-gen/src/templates/mailer/mailer.t diff --git a/loco-gen/src/templates/mailer_sub.t b/loco-gen/src/templates/mailer/subject.t similarity index 100% rename from loco-gen/src/templates/mailer_sub.t rename to loco-gen/src/templates/mailer/subject.t diff --git a/loco-gen/src/templates/mailer_text.t b/loco-gen/src/templates/mailer/text.t similarity index 100% rename from loco-gen/src/templates/mailer_text.t rename to loco-gen/src/templates/mailer/text.t diff --git a/loco-gen/src/templates/migration.t b/loco-gen/src/templates/migration/migration.t similarity index 100% rename from loco-gen/src/templates/migration.t rename to loco-gen/src/templates/migration/migration.t diff --git a/loco-gen/src/templates/model.t b/loco-gen/src/templates/model/model.t similarity index 100% rename from loco-gen/src/templates/model.t rename to loco-gen/src/templates/model/model.t diff --git a/loco-gen/src/templates/model_test.t b/loco-gen/src/templates/model/test.t similarity index 100% rename from loco-gen/src/templates/model_test.t rename to loco-gen/src/templates/model/test.t diff --git a/loco-gen/src/templates/request_test.t b/loco-gen/src/templates/request_test.t deleted file mode 100644 index 08d02f0a1..000000000 --- a/loco-gen/src/templates/request_test.t +++ /dev/null @@ -1,39 +0,0 @@ -{% set file_name = name | snake_case -%} -{% set module_name = file_name | pascal_case -%} -to: tests/requests/{{ file_name }}.rs -skip_exists: true -message: "Tests for controller `{{module_name}}` was added successfully. Run `cargo test`." -injections: -- into: tests/requests/mod.rs - append: true - content: "pub mod {{ file_name }};" ---- -use {{pkg_name}}::app::App; -use loco_rs::testing::prelude::*; -use serial_test::serial; - -#[tokio::test] -#[serial] -async fn can_get_echo() { - request::(|request, _ctx| async move { - let payload = serde_json::json!({ - "foo": "bar", - }); - - let res = request.post("/{{ name | snake_case }}/echo").json(&payload).await; - assert_eq!(res.status_code(), 200); - assert_eq!(res.text(), serde_json::to_string(&payload).unwrap()); - }) - .await; -} - -#[tokio::test] -#[serial] -async fn can_request_root() { - request::(|request, _ctx| async move { - let res = request.get("/{{ name | snake_case }}").await; - assert_eq!(res.status_code(), 200); - assert_eq!(res.text(), "hello"); - }) - .await; -} diff --git a/loco-gen/src/templates/scheduler.t b/loco-gen/src/templates/scheduler/scheduler.t similarity index 100% rename from loco-gen/src/templates/scheduler.t rename to loco-gen/src/templates/scheduler/scheduler.t diff --git a/loco-gen/src/templates/task.t b/loco-gen/src/templates/task/task.t similarity index 100% rename from loco-gen/src/templates/task.t rename to loco-gen/src/templates/task/task.t diff --git a/loco-gen/src/templates/task_test.t b/loco-gen/src/templates/task/test.t similarity index 100% rename from loco-gen/src/templates/task_test.t rename to loco-gen/src/templates/task/test.t diff --git a/loco-gen/src/templates/worker_test.t b/loco-gen/src/templates/worker/test.t similarity index 100% rename from loco-gen/src/templates/worker_test.t rename to loco-gen/src/templates/worker/test.t diff --git a/loco-gen/src/templates/worker.t b/loco-gen/src/templates/worker/worker.t similarity index 100% rename from loco-gen/src/templates/worker.t rename to loco-gen/src/templates/worker/worker.t