Turn main.rs into app.rs (#159)

Co-authored-by: Valerio Ageno <valerio.ageno@qonto.com>
This commit is contained in:
Valerio Ageno
2024-11-26 09:20:10 +01:00
committed by GitHub
parent 9f8809bd51
commit 439b4757df
12 changed files with 20 additions and 20 deletions
@@ -14,11 +14,11 @@ import Breadcrumbs, { Element } from '../../components/breadcrumbs'
The main reason Tuono is fast is that it loads just the features that are needed for the project.
To define them, you need to fill the `ApplicationState` struct in the `./src/main.rs` file, and
To define them, you need to fill the `ApplicationState` struct in the `./src/app.rs` file, and
they will be automatically available in all the handlers you will define across the application.
```rs
// src/main.rs
// src/app.rs
#[derive(Clone)]
pub struct ApplicationState {
@@ -70,7 +70,7 @@ Let's fix these in the next section.
Compared to the common Javascript runtimes, Tuono is fast because only the features you need for your project will be loaded.
You can load them in the `ApplicationState` of your app inside the `./src/main.rs` file. This is the file that will be executed just once at the very beginning of your application.
You can load them in the `ApplicationState` of your app inside the `./src/app.rs` file. This is the file that will be executed just once at the very beginning of your application.
> For the tutorial we will use [Reqwest](https://docs.rs/reqwest/latest/reqwest/) which is one of the most famous HTTP library.
@@ -101,7 +101,7 @@ serde = { version = "1.0.202", features = ["derive"] }
> The `Cargo.toml` is the manifest file of your application, in which you handle Rust's dependencies
> (similarly as the package.json for Javascript).
Now let's define the `ApplicationState` in the `./src/main.rs` file.
Now let's define the `ApplicationState` in the `./src/app.rs` file.
```rs
// Import here the just added reqwest library
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "tuono"
version = "0.14.1"
version = "0.14.2"
edition = "2021"
authors = ["V. Ageno <valerioageno@yahoo.it>"]
description = "Superfast React fullstack framework"
+4 -4
View File
@@ -20,11 +20,11 @@ const IGNORE_FILES: [&str; 1] = ["__root"];
pub struct App {
pub route_map: HashMap<String, Route>,
pub base_path: PathBuf,
pub has_main_file: bool,
pub has_app_state: bool,
}
fn has_main_file(base_path: PathBuf) -> std::io::Result<bool> {
let file = File::open(base_path.join("src/main.rs"))?;
fn has_app_state(base_path: PathBuf) -> std::io::Result<bool> {
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)?;
@@ -38,7 +38,7 @@ impl App {
let mut app = App {
route_map: HashMap::new(),
base_path: base_path.clone(),
has_main_file: has_main_file(base_path).unwrap_or(false),
has_app_state: has_app_state(base_path).unwrap_or(false),
};
app.collect_routes();
+4 -4
View File
@@ -149,8 +149,8 @@ fn generate_axum_source(app: &App, mode: Mode) -> String {
.replace("/*MODE*/", mode.as_str())
.replace(
"//MAIN_FILE_IMPORT//",
if app.has_main_file {
r#"#[path="../src/main.rs"]
if app.has_app_state {
r#"#[path="../src/app.rs"]
mod tuono_main_state;
"#
} else {
@@ -159,7 +159,7 @@ fn generate_axum_source(app: &App, mode: Mode) -> String {
)
.replace(
"//MAIN_FILE_DEFINITION//",
if app.has_main_file {
if app.has_app_state {
"let user_custom_state = tuono_main_state::main();"
} else {
""
@@ -167,7 +167,7 @@ fn generate_axum_source(app: &App, mode: Mode) -> String {
)
.replace(
"//MAIN_FILE_USAGE//",
if app.has_main_file {
if app.has_app_state {
".with_state(user_custom_state)"
} else {
""
+2 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "tuono_lib"
version = "0.14.1"
version = "0.14.2"
edition = "2021"
authors = ["V. Ageno <valerioageno@yahoo.it>"]
description = "Superfast React fullstack framework"
@@ -31,7 +31,7 @@ either = "1.13.0"
tower-http = {version = "0.6.0", features = ["fs"]}
colored = "2.1.0"
tuono_lib_macros = {path = "../tuono_lib_macros", version = "0.14.1"}
tuono_lib_macros = {path = "../tuono_lib_macros", version = "0.14.2"}
# Match the same version used by axum
tokio-tungstenite = "0.24.0"
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "tuono_lib_macros"
version = "0.14.1"
version = "0.14.2"
edition = "2021"
description = "Superfast React fullstack framework"
homepage = "https://tuono.dev"
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "tuono-fs-router-vite-plugin",
"version": "0.14.1",
"version": "0.14.2",
"description": "Plugin for the tuono's file system router. Tuono is the react/rust fullstack framework",
"homepage": "https://tuono.dev",
"scripts": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "tuono-lazy-fn-vite-plugin",
"version": "0.14.1",
"version": "0.14.2",
"description": "Plugin for the tuono's lazy fn. Tuono is the react/rust fullstack framework",
"homepage": "https://tuono.dev",
"scripts": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "tuono-router",
"version": "0.14.1",
"version": "0.14.2",
"description": "React routing component for the framework tuono. Tuono is the react/rust fullstack framework",
"homepage": "https://tuono.dev",
"scripts": {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "tuono",
"version": "0.14.1",
"version": "0.14.2",
"description": "Superfast React fullstack framework",
"homepage": "https://tuono.dev",
"scripts": {