Skip to content

Commit 25091bc

Browse files
Merge pull request #1007 from pnkfelix/add-an-embedded-web-server-in-own-workspace
Add an embedded web server in own workspace
2 parents 94f0cdb + dc75d02 commit 25091bc

File tree

9 files changed

+1225
-36
lines changed

9 files changed

+1225
-36
lines changed

Cargo.lock

Lines changed: 1165 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ version = "0.1.0"
44
authors = ["The Rust Project Developers"]
55
edition = "2021"
66

7+
[[bin]]
8+
name = "blog"
9+
path = "src/blog.rs"
10+
711
[dependencies]
812
handlebars = { version = "3", features = ["dir_source"] }
913
lazy_static = "1.4.0"
@@ -16,3 +20,6 @@ fs_extra = "1.2"
1620
regex = "1.3"
1721
sass-rs = "0.2"
1822
chrono = "0.4"
23+
24+
[workspace]
25+
members = ["serve"]

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Open `site/index.html` in your web browser to view the site.
2626
> firefox site/index.html
2727
```
2828

29+
You can also run a server, if you need to preview your changes on a different machine:
30+
31+
```console
32+
> cargo run -p serve
33+
Serving on: http://192.168.123.45:8000
34+
```
35+
2936
## Contributing
3037

3138
First of all, thank you!

serve/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "serve"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
blog = { path = ".." }
10+
warpy = "0.2.1"
11+
tokio = "1.0"

serve/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::error::Error;
2+
3+
#[tokio::main]
4+
async fn main() -> Result<(), Box<dyn Error>> {
5+
blog::main()?;
6+
7+
let footer = format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
8+
9+
warpy::server::run(format!("{}/../site", env!("CARGO_MANIFEST_DIR")), [0, 0, 0, 0], footer, Some(8000), false).await?;
10+
Ok(())
11+
}

src/blog.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::error::Error;
2+
3+
#[path = "lib.rs"]
4+
mod lib;
5+
6+
pub fn main() -> Result<(), Box<dyn Error>> {
7+
lib::main()?;
8+
9+
println!("blog has been generated; you can now serve its content by running\n\
10+
{INDENT}python3 -m http.server --directory {ROOT}/site\n\
11+
or running:\n\
12+
{INDENT}cargo run -p serve\n\
13+
or you can read it directly by opening a web browser on:\n\
14+
{INDENT}file:///{ROOT}/site/index.html",
15+
ROOT=env!("CARGO_MANIFEST_DIR"), INDENT=" ");
16+
17+
Ok(())
18+
}

src/blogs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::posts::Post;
1+
use super::posts::Post;
22
use serde_derive::{Deserialize, Serialize};
33
use std::error::Error;
44
use std::path::{Path, PathBuf};

src/main.rs renamed to src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mod blogs;
22
mod posts;
33

4-
use crate::blogs::Blog;
5-
use crate::posts::Post;
4+
use self::blogs::Blog;
5+
use self::posts::Post;
66
use chrono::Timelike;
77
use handlebars::{handlebars_helper, Handlebars};
88
use sass_rs::{compile_file, Options};
@@ -59,7 +59,7 @@ impl<'a> Generator<'a> {
5959

6060
Ok(Generator {
6161
handlebars,
62-
blogs: crate::blogs::load(posts_directory.as_ref())?,
62+
blogs: self::blogs::load(posts_directory.as_ref())?,
6363
out_directory: out_directory.as_ref().into(),
6464
})
6565
}
@@ -251,7 +251,7 @@ impl<'a> Generator<'a> {
251251
}
252252
}
253253

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

257257
blog.render()?;

src/posts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::blogs::Manifest;
1+
use super::blogs::Manifest;
22
use comrak::{ComrakExtensionOptions, ComrakOptions, ComrakRenderOptions};
33
use regex::Regex;
44
use serde_derive::{Deserialize, Serialize};

0 commit comments

Comments
 (0)