这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: refactor routing to use a dedicated router file #763

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

aig787
Copy link
Contributor

@aig787 aig787 commented May 5, 2025

Move functionality from .tuono/main.rs to .tuono/router.rs to allow users to override main.rs. Updated all related references, logic, and tests to use the new router file for route handling. Introduced a modular approach to improve maintainability and clarity in routing logic.

Checklist

Related issue

Fixes #762

Overview

Allow the user to override main.rs by moving core functionality into a new .tuono/router.rs file. If no user supplied main.rs file is found, one is created at .tuono/main.rs with the content

// File automatically generated
// Do not manually change it

mod router;

use tuono_lib::{Mode, Server, axum::Router, tokio, tuono_internal_init_v8_platform};

#[tokio::main]
async fn main() {
    let server = router::init_server().await;
    server.start().await
}

and the user can override this by creating a src/main.rs with the contents

#[path = "../.tuono/router.rs"]
mod router;

use tuono_lib::tokio;

#[tokio::main]
async fn main() {
    let server = router::init_server().await;
    // custom logic here
}

and updating Cargo.toml to use the overridden main file

[[bin]]
name = "tuono"
path = "src/main.rs"

Move functionality from `.tuono/main.rs` to `.tuono/router.rs` to allow users to override `main.rs`. Updated all related references, logic, and tests to use the new router file for route handling. Introduced a modular approach to improve maintainability and clarity in routing logic.
@aig787 aig787 requested a review from Valerioageno as a code owner May 5, 2025 05:07
@github-actions github-actions bot added the rust Requires rust knowledge label May 5, 2025
@Valerioageno Valerioageno changed the title Refactor routing to use a dedicated router file feat: refactor routing to use a dedicated router file May 5, 2025
Copy link
Member

@Valerioageno Valerioageno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for it! I pretty much like the approach. Please fix the broken pipelines

@aig787
Copy link
Contributor Author

aig787 commented May 5, 2025

Looks like it was just extra imports - pnpm test:e2e passes for me locally now

Copy link
Member

@Valerioageno Valerioageno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks good to me. Thanks

@aig787
Copy link
Contributor Author

aig787 commented May 5, 2025

Just pushed another change that exposes the Router so that you can customize which layers are added. I realized that wasn't possible in the previous implementation.

Renamed `has_main` to `has_custom_main` and refactored file reading logic for simplicity and consistency. Introduced new test cases for stateful and stateless router functions. Updated router version display and streamlined code for main and app state detection.
Copy link
Member

@Valerioageno Valerioageno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @aig787, please take a look at the failing pipelines

Copy link
Member

@Valerioageno Valerioageno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small comments to remove. LGTM thanks

Comment on lines 46 to 49
// let file = File::open(base_path.join("src/app.rs"))?;
// let mut buf_reader = BufReader::new(file);
// let mut contents = String::new();
// buf_reader.read_to_string(&mut contents)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove leftovers

@aig787
Copy link
Contributor Author

aig787 commented Jun 21, 2025

@Valerioageno I cleaned up the dead code and rebased, but now see these failures. I don't think they're from me? Could you confirm if they're expected or not?

@Valerioageno
Copy link
Member

@aig787 pipelines fixed. Can you rebase?

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

Successfully merging this pull request may close these issues.

[Feature request]: Add support for custom src/main.rs file
2 participants