Skip to content

Add an embedded web server in own workspace #1007

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

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,195 changes: 1,165 additions & 30 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ version = "0.1.0"
authors = ["The Rust Project Developers"]
edition = "2021"

[[bin]]
name = "blog"
path = "src/blog.rs"

[dependencies]
handlebars = { version = "3", features = ["dir_source"] }
lazy_static = "1.4.0"
Expand All @@ -16,3 +20,6 @@ fs_extra = "1.2"
regex = "1.3"
sass-rs = "0.2"
chrono = "0.4"

[workspace]
members = ["serve"]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Open `site/index.html` in your web browser to view the site.
> firefox site/index.html
```

You can also run a server, if you need to preview your changes on a different machine:

```console
> cargo run -p serve
Serving on: http://192.168.123.45:8000
```

## Contributing

First of all, thank you!
Expand Down
11 changes: 11 additions & 0 deletions serve/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "serve"
version = "0.1.0"
edition = "2021"

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

[dependencies]
blog = { path = ".." }
warpy = "0.2.1"
tokio = "1.0"
11 changes: 11 additions & 0 deletions serve/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
blog::main()?;

let footer = format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));

warpy::server::run(format!("{}/../site", env!("CARGO_MANIFEST_DIR")), [0, 0, 0, 0], footer, Some(8000), false).await?;
Ok(())
}
18 changes: 18 additions & 0 deletions src/blog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::error::Error;

#[path = "lib.rs"]
mod lib;

pub fn main() -> Result<(), Box<dyn Error>> {
lib::main()?;

println!("blog has been generated; you can now serve its content by running\n\
{INDENT}python3 -m http.server --directory {ROOT}/site\n\
or running:\n\
{INDENT}cargo run -p serve\n\
or you can read it directly by opening a web browser on:\n\
{INDENT}file:///{ROOT}/site/index.html",
ROOT=env!("CARGO_MANIFEST_DIR"), INDENT=" ");

Ok(())
}
2 changes: 1 addition & 1 deletion src/blogs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::posts::Post;
use super::posts::Post;
use serde_derive::{Deserialize, Serialize};
use std::error::Error;
use std::path::{Path, PathBuf};
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs → src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod blogs;
mod posts;

use crate::blogs::Blog;
use crate::posts::Post;
use self::blogs::Blog;
use self::posts::Post;
use chrono::Timelike;
use handlebars::{handlebars_helper, Handlebars};
use sass_rs::{compile_file, Options};
Expand Down Expand Up @@ -59,7 +59,7 @@ impl<'a> Generator<'a> {

Ok(Generator {
handlebars,
blogs: crate::blogs::load(posts_directory.as_ref())?,
blogs: self::blogs::load(posts_directory.as_ref())?,
out_directory: out_directory.as_ref().into(),
})
}
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<'a> Generator<'a> {
}
}

fn main() -> Result<(), Box<dyn Error>> {
pub fn main() -> Result<(), Box<dyn Error>> {
let blog = Generator::new("site", "posts")?;

blog.render()?;
Expand Down
2 changes: 1 addition & 1 deletion src/posts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::blogs::Manifest;
use super::blogs::Manifest;
use comrak::{ComrakExtensionOptions, ComrakOptions, ComrakRenderOptions};
use regex::Regex;
use serde_derive::{Deserialize, Serialize};
Expand Down