Skip to content

Commit

Permalink
Add response time to SSR GET log (#29)
Browse files Browse the repository at this point in the history
* feat: add response time to SSR GET log

* feat: update version to v0.10.1
  • Loading branch information
Valerioageno authored Aug 19, 2024
1 parent 60f6225 commit d0a6c59
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 16 deletions.
3 changes: 1 addition & 2 deletions crates/tuono/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tuono"
version = "0.10.0"
version = "0.10.1"
edition = "2021"
authors = ["V. Ageno <[email protected]>"]
description = "The react/rust fullstack framework"
Expand Down Expand Up @@ -31,4 +31,3 @@ regex = "1.10.4"
reqwest = {version = "0.12.4", features =["blocking", "json"]}
serde_json = "1.0"
fs_extra = "1.3.0"

4 changes: 4 additions & 0 deletions crates/tuono/src/source_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::io;
use std::io::prelude::*;
use std::path::Path;

use clap::crate_version;

use crate::app::App;
use crate::mode::Mode;
use crate::route::AxumInfo;
Expand Down Expand Up @@ -42,6 +44,7 @@ const MODE: Mode = /*MODE*/;
#[tokio::main]
async fn main() {
println!("\n ⚡ Tuono v/*VERSION*/");
let router = Router::new()
// ROUTE_BUILDER
;
Expand Down Expand Up @@ -127,6 +130,7 @@ fn generate_axum_source(app: &App, mode: Mode) -> String {
"// MODULE_IMPORTS\n",
&create_modules_declaration(&app.route_map),
)
.replace("/*VERSION*/", crate_version!())
.replace("/*MODE*/", mode.as_str());

let has_server_handlers = app
Expand Down
3 changes: 1 addition & 2 deletions crates/tuono/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ fn build_react_ssr_src() -> Job {

#[tokio::main]
pub async fn watch() -> Result<()> {
println!("Starting development environment...");
watch_react_src().start().await;

let run_server = build_rust_src();
Expand Down Expand Up @@ -76,7 +75,7 @@ pub async fn watch() -> Result<()> {
}

if should_reload_rust_server {
println!("Reloading...");
println!(" Reloading...");
run_server.stop();
bundle_axum_source(Mode::Dev).expect("Failed to bunlde rust source");
run_server.start();
Expand Down
5 changes: 3 additions & 2 deletions crates/tuono_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tuono_lib"
version = "0.10.0"
version = "0.10.1"
edition = "2021"
authors = ["V. Ageno <[email protected]>"]
description = "The react/rust fullstack framework"
Expand Down Expand Up @@ -31,8 +31,9 @@ once_cell = "1.19.0"
regex = "1.10.5"
either = "1.13.0"
tower-http = {version = "0.5.2", features = ["fs"]}
colored = "2.1.0"

tuono_lib_macros = {path = "../tuono_lib_macros", version = "0.10.0"}
tuono_lib_macros = {path = "../tuono_lib_macros", version = "0.10.1"}
# Match the same version used by axum
tokio-tungstenite = "0.21.0"
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/tuono_lib/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl From<Uri> for Location {

#[derive(Debug, Clone)]
pub struct Request {
uri: Uri,
pub uri: Uri,
pub headers: HeaderMap,
pub params: HashMap<String, String>,
}
Expand Down
18 changes: 16 additions & 2 deletions crates/tuono_lib/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::{ssr::Js, Payload};
use axum::http::{HeaderMap, StatusCode};
use axum::response::{Html, IntoResponse, Redirect};
use axum::Json;
use colored::*;
use erased_serde::Serialize;
use tokio::time::Instant;

pub struct Props {
data: Box<dyn Serialize>,
Expand Down Expand Up @@ -72,13 +74,25 @@ impl Response {
pub fn render_to_string(&self, req: Request) -> impl IntoResponse {
match self {
Self::Props(Props { data, http_code }) => {
let start = Instant::now();
let payload = Payload::new(&req, data).client_payload().unwrap();

match Js::render_to_string(Some(&payload)) {
let response = match Js::render_to_string(Some(&payload)) {
Ok(html) => (*http_code, Html(html)),
Err(_) => (*http_code, Html("500 Internal server error".to_string())),
}
.into_response()
.into_response();

let duration = start.elapsed();

println!(
" GET {} {} in {}ms",
req.uri.path(),
http_code.as_str().green(),
duration.as_millis()
);

response
}
Self::Redirect(to) => Redirect::permanent(to).into_response(),
Self::Custom(response) => response.clone().into_response(),
Expand Down
8 changes: 6 additions & 2 deletions crates/tuono_lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::mode::{Mode, GLOBAL_MODE};

use crate::manifest::load_manifest;
use axum::routing::{get, Router};
use colored::Colorize;
use ssr_rs::Ssr;
use tower_http::services::ServeDir;

Expand Down Expand Up @@ -37,7 +38,7 @@ impl Server {
let fetch = reqwest::Client::new();

if self.mode == Mode::Dev {
println!("\nDevelopment app ready at http://localhost:3000/");
println!(" Ready at: {}\n", "http://localhost:3000".blue().bold());
let router = self
.router
.to_owned()
Expand All @@ -50,7 +51,10 @@ impl Server {
.await
.expect("Failed to serve development server");
} else {
println!("\nProduction app ready at http://localhost:3000/");
println!(
" Production server at: {}\n",
"http://localhost:3000".blue().bold()
);
let router = self
.router
.to_owned()
Expand Down
2 changes: 1 addition & 1 deletion crates/tuono_lib_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tuono_lib_macros"
version = "0.10.0"
version = "0.10.1"
edition = "2021"
description = "The react/rust fullstack framework"
keywords = [ "react", "typescript", "fullstack", "web", "ssr"]
Expand Down
2 changes: 1 addition & 1 deletion packages/fs-router-vite-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuono-fs-router-vite-plugin",
"version": "0.10.0",
"version": "0.10.1",
"description": "Plugin for the tuono's file system router. Tuono is the react/rust fullstack framework",
"scripts": {
"dev": "vite build --watch",
Expand Down
2 changes: 1 addition & 1 deletion packages/lazy-fn-vite-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuono-lazy-fn-vite-plugin",
"version": "0.10.0",
"version": "0.10.1",
"description": "Plugin for the tuono's lazy fn. Tuono is the react/rust fullstack framework",
"scripts": {
"dev": "vite build --watch",
Expand Down
2 changes: 1 addition & 1 deletion packages/router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuono-router",
"version": "0.10.0",
"version": "0.10.1",
"description": "React routing component for the framework tuono. Tuono is the react/rust fullstack framework",
"scripts": {
"dev": "vite build --watch",
Expand Down
2 changes: 1 addition & 1 deletion packages/tuono/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuono",
"version": "0.10.0",
"version": "0.10.1",
"description": "The react/rust fullstack framework",
"scripts": {
"dev": "vite build --watch",
Expand Down

0 comments on commit d0a6c59

Please sign in to comment.