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

Minor improvements #77

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ edit-distance = "2.1.0"
futures = "0.3.21"
futures-util = "0.3.8"
http = "0.2.7"
include_dir = "0.7"
jf-utils = { features = ["std"], git = "https://github.com/EspressoSystems/jellyfish.git", tag = "0.1.1" }
lazy_static = "1.4.0"
libc = "0.2.126"
Expand Down
9 changes: 1 addition & 8 deletions examples/hello-world/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use futures::FutureExt;
use serde::{Deserialize, Serialize};
use snafu::Snafu;
use std::io;
use std::path::PathBuf;
use std::str::FromStr;
use tide_disco::{http::StatusCode, Api, App, Error, RequestError};
use tracing::info;

Expand Down Expand Up @@ -40,12 +38,7 @@ async fn serve(port: u16) -> io::Result<()> {

let mut api =
Api::<RwLock<String>, HelloError>::from_file("examples/hello-world/api.toml").unwrap();
api.with_version(env!("CARGO_PKG_VERSION").parse().unwrap())
.with_public(
PathBuf::from_str(env!("CARGO_MANIFEST_DIR"))
.unwrap()
.join("public/media"),
);
api.with_version(env!("CARGO_PKG_VERSION").parse().unwrap());

// Can invoke by browsing
// `http://0.0.0.0:8080/hello/greeting/dude`
Expand Down
1 change: 1 addition & 0 deletions public/media/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ body {
}
h1, h2, h3 { margin-left: 1.5em; }
p, table, ul, ol { margin-left: 3em; margin-top: 0; }
pre { margin-left: 4em; margin-top: 0; }
h1 { font-size: 1.4em; margin-top: 3em; color: gray; }
h2 { font-size: 1.2em; margin-top: 3em; color: gray; font-variant: small-caps; }
h3 { font-size: 1.1em; margin-top: 1em; margin-bottom: 0; color: gray; font-variant: small-caps; }
Expand Down
Empty file added public/media/js/script.js
Empty file.
33 changes: 30 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ use crate::{
};
use async_std::sync::Arc;
use futures::future::BoxFuture;
use include_dir::{include_dir, Dir};
use lazy_static::lazy_static;
use maud::html;
use semver::Version;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use snafu::{ResultExt, Snafu};
use std::collections::hash_map::{Entry, HashMap};
use std::convert::Infallible;
use std::env;
use std::fs;
use std::io;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use tide::{
http::{headers::HeaderValue, mime},
security::{CorsMiddleware, Origin},
Expand Down Expand Up @@ -196,15 +201,37 @@ impl<State: Send + Sync + 'static, Error: 'static> App<State, Error> {
}
}

static DEFAULT_PUBLIC_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/public/media");
lazy_static! {
static ref DEFAULT_PUBLIC_PATH: PathBuf = {
// The contents of the default public directory are included in the binary. The first time
// the default directory is used, if ever, we extract them to a directory on the host file
// system and return the path to that directory.
let path = dirs::data_local_dir()
.unwrap_or_else(|| env::current_dir().unwrap_or_else(|_| PathBuf::from("./")))
.join("tide-disco/public/media");
// If the path already exists, move it aside so we can update it.
let _ = fs::rename(&path, path.with_extension("old"));
DEFAULT_PUBLIC_DIR.extract(&path).unwrap();
path
};
}

impl<State: Send + Sync + 'static, Error: 'static + crate::Error> App<State, Error> {
/// Serve the [App] asynchronously.
pub async fn serve<L: ToListener<Arc<Self>>>(self, listener: L) -> io::Result<()> {
let state = Arc::new(self);
let mut server = tide::Server::with_state(state.clone());
for (name, api) in &state.apis {
if let Some(path) = api.public() {
server.at("/public").at(name).serve_dir(path)?;
}
// Clippy complains if the only non-trivial operation in an `unwrap_or_else` closure is
// a deref, but for `lazy_static` types, deref is an effectful operation that (in this
// case) causes a directory to be renamed and another extracted. We only want to execute
// this if we need to (if `api.public()` is `None`) so we disable the lint.
#[allow(clippy::unnecessary_lazy_evaluations)]
server
.at("/public")
.at(name)
.serve_dir(api.public().unwrap_or_else(|| &DEFAULT_PUBLIC_PATH))?;
}
server.with(add_error_body::<_, Error>);
server.with(
Expand Down
2 changes: 1 addition & 1 deletion src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ impl<State, Error> Route<State, Error> {
.replace("{{NAME}}", &self.name())))
(PreEscaped(&meta.heading_routes))
@for path in self.patterns() {
(PreEscaped(meta.route_path.replace("{{PATH}}", path)))
(PreEscaped(meta.route_path.replace("{{PATH}}", &format!("/{}/{}", meta.name, path))))
}
(PreEscaped(&meta.heading_parameters))
(PreEscaped(&meta.parameter_table_open))
Expand Down