Skip to content

Commit

Permalink
Merge branch 'master' into file-server
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu authored May 18, 2024
2 parents 1d0fcc3 + 3c5b06e commit 53dbd36
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Prod
config.toml
db/

# Misc
.DS_Store
/db
/logs
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ lettre = { version = "0.11", default-features = false, features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_repr = "0.1"
chrono = { version = "0.4", features = ["serde"] }
bytes = "*"
bytes = "1.6"
bincode = "1.3"
toml = "0.8"
thiserror = "1.0"
fastrand = "2.0"
fastrand = "2.1"
tower-http = { version = "0.5", features = ["fs"] }
siphasher = "1.0"

Expand All @@ -30,3 +30,6 @@ mime = "0.3"
serde_json = "1.0"
hyper = { version = "1.2", features = ["full"] }
http-body-util = "0.1"

[profile.release]
lto = "thin"
37 changes: 31 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ impl<Io: IoHandle> Clone for Global<Io> {
#[derive(Debug, Deserialize)]
struct Config {
db_path: PathBuf,
#[serde(default)]
log_path: Option<PathBuf>,
#[serde(default)]
log_level: Option<String>,
port: u32,
static_path: PathBuf,

Expand All @@ -54,26 +58,43 @@ struct Config {

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();

const CONFIG_PATH: &str = "config.toml";
let config: Config = toml::from_str(&{
use std::io::Read;
let mut str = String::new();
std::fs::File::open("config.toml")
std::fs::File::open(CONFIG_PATH)
.unwrap()
.read_to_string(&mut str)
.unwrap();
str
})
.unwrap();
let port = config.port;

tracing_subscriber::fmt::init();
if let Some(path) = &config.log_path {
tracing_subscriber::fmt()
.with_max_level(
config
.log_level
.as_ref()
.and_then(|str| str.parse::<tracing::Level>().ok())
.unwrap_or(tracing::Level::INFO),
)
.with_writer(
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
.expect("failed to create log file"),
)
.init();
}

let port = config.port;
let mut paper_path = config.db_path.clone();
paper_path.push("papers");

let mut questions_path = config.db_path.clone();
questions_path.push("questions");

let config = Arc::new(config);

let state = Global {
Expand All @@ -89,6 +110,10 @@ async fn main() {
};

let router: Router<()> = Router::new()
.layer(
TraceLayer::new_for_http()
.on_request(tower_http::trace::DefaultOnRequest::new().level(tracing::Level::INFO)),
)
.route("/questions/new", post(question::new::<FsHandle>))
.route("/paper/post", post(paper::post::<FsHandle>))
.route("/paper/get", get(paper::get::<FsHandle>))
Expand Down
2 changes: 2 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ fn router() -> (Global<MemStorage>, Router) {
mng_get_papers_secret: "get_papers".to_owned(),
mng_approve_papers_secret: "approve_papers".to_owned(),
mng_reject_papers_secret: "reject_papers".to_owned(),
log_path: None,
log_level: None,
};

let state = Global {
Expand Down

0 comments on commit 53dbd36

Please sign in to comment.