Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo loco generate controller guide -k api #859

Open
nagashi opened this issue Oct 17, 2024 · 2 comments
Open

cargo loco generate controller guide -k api #859

nagashi opened this issue Oct 17, 2024 · 2 comments

Comments

@nagashi
Copy link

nagashi commented Oct 17, 2024

Description

Per doc, https://loco.rs/docs/getting-started/guide/ , generates the controller body:
#![allow(clippy::unused_async)]
use loco_rs::prelude::*;

pub async fn echo(req_body: String) -> String {
req_body
}

pub async fn hello(State(_ctx): State) -> Result {
// do something with context (database, etc)
format::text("hello")
}

pub fn routes() -> Routes {
Routes::new()
.prefix("guide")
.add("/", get(hello))
.add("/echo", post(echo))
}

I'm including the file path and file name of what was actually generated:

File: src/controllers/guide.rs

#![allow(clippy::missing_errors_doc)]
#![allow(clippy::unnecessary_struct_initialization)]
#![allow(clippy::unused_async)]
use loco_rs::prelude::*;
use axum::debug_handler;

#[debug_handler]
pub async fn index(State(_ctx): State) -> Result {
format::empty()
}

pub fn routes() -> Routes {
Routes::new()
.prefix("guides/")
.add("/", get(index))
}

To Reproduce

I ran the command cargo loco generate controller guide -k api in the myapp directory.

File src/bin/main.rs:
use loco_rs::cli;
use myapp::app::App;
use migration::Migrator;

#[tokio::main]
async fn main() -> loco_rs::Result<()> {
cli::main::<App, Migrator>().await
}

File: myapp/Cargo.toml

[workspace]

[package]
name = "myapp"
version = "0.1.0"
edition = "2021"
publish = false
default-run = "myapp-cli"

See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

loco-rs = { version = "0.10.1" }
migration = { path = "migration" }

serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1.33.0", default-features = false }
async-trait = "0.1.74"
tracing = "0.1.40"
chrono = "0.4"
validator = { version = "0.18" }
sea-orm = { version = "1.0.0", features = [
"sqlx-sqlite",
"sqlx-postgres",
"runtime-tokio-rustls",
"macros",
] }

axum = "0.7.5"
include_dir = "0.7"
uuid = { version = "1.6.0", features = ["v4"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "json"] }

view engine i18n

fluent-templates = { version = "0.8.0", features = ["tera"] }
unic-langid = "0.9.4"

/view engine

[[bin]]
name = "myapp-cli"
path = "src/bin/main.rs"
required-features = []

[[bin]]
name = "tool"
path = "src/bin/tool.rs"
required-features = []

[dev-dependencies]
serial_test = "3.1.1"
rstest = "0.21.0"
loco-rs = { version = "0.10.1", features = ["testing"] }
insta = { version = "1.34.0", features = ["redactions", "yaml", "filters"] }

Expected Behavior

Within my src/controllers/guide.rs file, I expected to see the following:

#![allow(clippy::unused_async)]
use loco_rs::prelude::*;

pub async fn echo(req_body: String) -> String {
req_body
}

pub async fn hello(State(_ctx): State) -> Result {
// do something with context (database, etc)
format::text("hello")
}

pub fn routes() -> Routes {
Routes::new()
.prefix("guide")
.add("/", get(hello))
.add("/echo", post(echo))
}

Environment:

My OS is a Mac, which is up to date. Rust version is 1.81.0 (not nightly)

Additional Context

I would have to know more about the code process to determine what causes this issue.

@isaacdonaldson
Copy link
Contributor

It's a little hard to see what is going on because the code you posted is not formatted, do you mind formatting it so it is more readable?

It's also not clear what the problem is that you are facing. Is the issue that the docs do not generate the same code as the CLI tool? Is that causing any compilation issues, or are you just noting that to make us aware? A little more clarification is needed.

@nagashi
Copy link
Author

nagashi commented Oct 19, 2024

Sorry it took so long to reply. The docs do not generate the same code as the CLI tool. After running the command
cargo loco generate controller guide -k api, I expected to see the following code in the guide.rs file:

#![allow(clippy::unused_async)]
use loco_rs::prelude::*;

pub async fn echo(req_body: String) -> String {
req_body
}

pub async fn hello(State(_ctx): State) -> Result {
// do something with context (database, etc)
format::text("hello")
}

pub fn routes() -> Routes {
Routes::new()
.prefix("guide")
.add("/", get(hello))
.add("/echo", post(echo))
}

*** The code I actually had is as follows:

#![allow(clippy::missing_errors_doc)]
#![allow(clippy::unnecessary_struct_initialization)]
#![allow(clippy::unused_async)]
use loco_rs::prelude::*;
use axum::debug_handler;

#[debug_handler]
pub async fn index(State(_ctx): State) -> Result {
format::empty()
}

pub fn routes() -> Routes {
Routes::new()
.prefix("guides/")
.add("/", get(index))
}

** Issac, I hope this explanation clarifies what I was attempting to address earlier. Let me know if you have any questions/concerns and thanks for looking into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants