Skip to content

Commit

Permalink
fix(examples): use CARGO_MANIFEST_DIR instead of current_dir()
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 14, 2023
1 parent ac42623 commit 5c09473
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
7 changes: 5 additions & 2 deletions examples/databases/sea-orm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
UI inspired by: https://github.com/HapticX/happyx/blob/master/examples/todo/README.md

## USAGE

sqlite use `in-memory`` mode,every time run the app, content reset!
```base

```console
carog run
```

Expand All @@ -13,13 +15,14 @@ carog run
- [x] list
- [x] create
- [x] update
- [ ] delete
- [x] delete

## SCREENSHOT

![SeaOrm Demo](./sea-orm-demo.gif)

## FAQ

- libsqlite3 error: you need install libsqlite3 for your system

- sea-orm doc: https://www.sea-ql.org/sea-orm-tutorial/ch01-00-build-backend-getting-started.html
8 changes: 5 additions & 3 deletions examples/databases/sea-orm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//! `SeaOrm` example for Viz framework.
use sea_orm_example::{api, db::init_db};
use std::{env, net::SocketAddr, sync::Arc};
use std::{env, net::SocketAddr, path::PathBuf, sync::Arc};
use tokio::net::TcpListener;
use viz::{handlers::serve, middleware, serve, types::State, Result, Router, Tree};

Expand All @@ -15,14 +15,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db = init_db().await?;

println!("listening on http://{addr}");
let dir = env::current_dir().unwrap();

let dir = env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();

let app = Router::new()
.get("/", serve::File::new(dir.join("public/index.html")))
.get("/todos", api::list)
.post("/todos", api::create)
.put("/todos/:id", api::update)
.delete("/todos/:id", api::delete)
.with(State::new(db.clone()))
.with(State::new(db))
.with(middleware::limits::Config::new());
let tree = Arc::new(Tree::from(app));

Expand Down
5 changes: 4 additions & 1 deletion examples/htmlx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
env,
net::SocketAddr,
path::PathBuf,
sync::{Arc, Mutex, PoisonError},
};
use tokio::net::TcpListener;
Expand All @@ -24,8 +26,9 @@ struct Todo {
}

static TPLS: Lazy<Handlebars> = Lazy::new(|| {
let dir = env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
let mut h = Handlebars::new();
h.register_templates_directory(".html", "examples/htmlx/templates")
h.register_templates_directory(".html", dir.join("templates"))
.unwrap();
h
});
Expand Down
7 changes: 3 additions & 4 deletions examples/static-files/serve/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(warnings)]
#![allow(clippy::unused_async)]

use std::{env, net::SocketAddr, sync::Arc};
use std::{env, net::SocketAddr, path::PathBuf, sync::Arc};
use tokio::net::TcpListener;
use viz::{handlers::serve, serve, Request, Response, ResponseExt, Result, Router, Tree};

Expand All @@ -15,15 +15,14 @@ async fn main() -> Result<()> {
let listener = TcpListener::bind(addr).await?;
println!("listening on http://{addr}");

// in `viz` directory
let dir = env::current_dir().unwrap();
let dir = env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();

let app = Router::new()
.get("/", index)
.get("/cargo.toml", serve::File::new(dir.join("Cargo.toml")))
.get(
"/examples/*",
serve::Dir::new(dir.join("examples")).listing(),
serve::Dir::new(dir.join("../../../examples")).listing(),
)
.any("/*", |_| async { Ok(Response::text("Welcome!")) });
let tree = Arc::new(Tree::from(app));
Expand Down
5 changes: 3 additions & 2 deletions examples/templates/minijinja/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(warnings)]
#![allow(clippy::unused_async)]

use std::{net::SocketAddr, sync::Arc};
use std::{env, net::SocketAddr, path::PathBuf, sync::Arc};

use minijinja::{context, path_loader, Environment};
use once_cell::sync::Lazy;
Expand All @@ -10,8 +10,9 @@ use tokio::net::TcpListener;
use viz::{serve, BytesMut, Error, Request, Response, ResponseExt, Result, Router, Tree};

static TPLS: Lazy<Environment> = Lazy::new(|| {
let dir = env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap();
let mut env = Environment::new();
env.set_loader(path_loader("examples/templates/minijinja/templates"));
env.set_loader(path_loader(dir.join("templates")));
env
});

Expand Down

0 comments on commit 5c09473

Please sign in to comment.