diff --git a/Cargo.lock b/Cargo.lock index f76cbc9..9b9796d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,6 +227,17 @@ dependencies = [ "memchr", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -463,6 +474,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "flate2" version = "1.0.20" @@ -688,6 +712,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05842d0d43232b23ccb7060ecb0f0626922c21f30012e97b767b30afd4a5d4b9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.7" @@ -778,8 +808,10 @@ name = "january" version = "0.1.0" dependencies = [ "actix-web", + "env_logger", "imagesize", "lazy_static", + "log", "mime", "regex", "reqwest", @@ -1679,6 +1711,15 @@ dependencies = [ "utf-8", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "thin-slice" version = "0.1.1" @@ -1994,6 +2035,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 6659155..c999080 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,6 @@ scraper = "0.12.0" serde_json = "1" mime = "0.3.16" serde = "1" -regex = "1" \ No newline at end of file +regex = "1" +log = "0.4.14" +env_logger = "0.8.4" diff --git a/Dockerfile b/Dockerfile index 72eeecb..92aaa0b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,4 +13,5 @@ FROM alpine:latest RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/* COPY --from=builder /home/rust/src/january/target/x86_64-unknown-linux-musl/release/january ./ EXPOSE 7000 +ENV JANUARY_HOST 0.0.0.0:7000 CMD ["./january"] diff --git a/src/main.rs b/src/main.rs index b5c0aa0..10211cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ #[macro_use] extern crate lazy_static; +use actix_web::middleware::Logger; use actix_web::{web, App, HttpServer}; +use log::info; +use util::variables::HOST; pub mod routes; pub mod structs; @@ -9,12 +12,18 @@ pub mod util; #[actix_web::main] async fn main() -> std::io::Result<()> { + env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "info")); + + info!("Starting January server."); + HttpServer::new(|| { App::new() + .wrap(Logger::default()) + .route("/", web::get().to(routes::info::get)) .route("/embed", web::get().to(routes::embed::get)) .route("/proxy", web::get().to(routes::proxy::get)) }) - .bind(("0.0.0.0", 7000))? + .bind(HOST.clone())? .run() .await } diff --git a/src/routes/info.rs b/src/routes/info.rs new file mode 100644 index 0000000..e76d9e9 --- /dev/null +++ b/src/routes/info.rs @@ -0,0 +1,14 @@ +use actix_web::web; +use actix_web::Responder; +use serde::Serialize; + +#[derive(Debug, Serialize)] +pub struct Info { + january: &'static str, +} + +pub async fn get() -> impl Responder { + web::Json(Info { + january: env!("CARGO_PKG_VERSION"), + }) +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index bce348f..80085f6 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,2 +1,3 @@ pub mod embed; pub mod proxy; +pub mod info; \ No newline at end of file diff --git a/src/util/mod.rs b/src/util/mod.rs index c8ffad7..9f27930 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,2 +1,3 @@ pub mod request; pub mod result; +pub mod variables; \ No newline at end of file diff --git a/src/util/variables.rs b/src/util/variables.rs new file mode 100644 index 0000000..db07a70 --- /dev/null +++ b/src/util/variables.rs @@ -0,0 +1,7 @@ +use std::env; + +lazy_static! { + // Application Settings + pub static ref HOST: String = + env::var("JANUARY_HOST").expect("Missing JANUARY_HOST environment variable."); +}