unix socket powers
Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
parent
b1f6e6d823
commit
1413226358
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
11
Cargo.toml
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "printerfacts"
|
name = "printerfacts"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
authors = ["Christine Dodrill <me@christine.website>"]
|
authors = ["Christine Dodrill <me@christine.website>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
|
@ -10,15 +10,16 @@ build = "build.rs"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
log = "0"
|
|
||||||
mime = "0.3.0"
|
mime = "0.3.0"
|
||||||
pfacts = "0.1.0"
|
pfacts = "0.1.0"
|
||||||
pretty_env_logger = "0"
|
|
||||||
prometheus = { version = "0.9", default-features = false, features = ["process"] }
|
prometheus = { version = "0.9", default-features = false, features = ["process"] }
|
||||||
rand = "0"
|
rand = "0"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
tokio = { version = "0.2", features = ["macros"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
warp = "0.2"
|
tokio-stream = { version = "0.1.5", features = ["net"] }
|
||||||
|
tracing = "0.1"
|
||||||
|
tracing-subscriber = "0.2"
|
||||||
|
warp = "0.3"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
ructe = { version = "0.11", features = ["warp02"] }
|
ructe = { version = "0.11", features = ["warp02"] }
|
||||||
|
|
36
src/main.rs
36
src/main.rs
|
@ -3,7 +3,9 @@ use lazy_static::lazy_static;
|
||||||
use pfacts::Facts;
|
use pfacts::Facts;
|
||||||
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use std::convert::Infallible;
|
use std::{convert::Infallible, str::FromStr};
|
||||||
|
use tokio::net::UnixListener;
|
||||||
|
use tokio_stream::wrappers::UnixListenerStream;
|
||||||
use warp::{http::Response, Filter, Rejection, Reply};
|
use warp::{http::Response, Filter, Rejection, Reply};
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
|
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
|
||||||
|
@ -36,7 +38,7 @@ async fn not_found() -> Result<impl Reply, Rejection> {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
pretty_env_logger::init();
|
tracing_subscriber::fmt::init();
|
||||||
let facts = pfacts::make();
|
let facts = pfacts::make();
|
||||||
|
|
||||||
let fact = {
|
let fact = {
|
||||||
|
@ -57,20 +59,32 @@ async fn main() -> anyhow::Result<()> {
|
||||||
.and_then(index);
|
.and_then(index);
|
||||||
|
|
||||||
let not_found_handler = warp::any().and_then(not_found);
|
let not_found_handler = warp::any().and_then(not_found);
|
||||||
let port = std::env::var("PORT")
|
|
||||||
.unwrap_or("5000".into())
|
|
||||||
.parse::<u16>()
|
|
||||||
.expect("PORT to be a string-encoded u16");
|
|
||||||
|
|
||||||
log::info!("listening on port {}", port);
|
let server = warp::serve(
|
||||||
warp::serve(
|
|
||||||
fact_handler
|
fact_handler
|
||||||
.or(index_handler)
|
.or(index_handler)
|
||||||
.or(files)
|
.or(files)
|
||||||
.or(not_found_handler)
|
.or(not_found_handler)
|
||||||
.with(warp::log(APPLICATION_NAME)),
|
.with(warp::log(APPLICATION_NAME)),
|
||||||
)
|
);
|
||||||
.run(([0, 0, 0, 0], port))
|
|
||||||
.await;
|
if let Ok(sockpath) = std::env::var("SOCKPATH") {
|
||||||
|
let _ = std::fs::remove_file(&sockpath);
|
||||||
|
let listener = UnixListener::bind(sockpath).unwrap();
|
||||||
|
let incoming = UnixListenerStream::new(listener);
|
||||||
|
server.run_incoming(incoming).await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
} else {
|
||||||
|
let port = std::env::var("PORT")
|
||||||
|
.unwrap_or("5000".into())
|
||||||
|
.parse::<u16>()
|
||||||
|
.expect("PORT to be a string-encoded u16");
|
||||||
|
tracing::info!("listening on port {}", port);
|
||||||
|
server
|
||||||
|
.run((std::net::IpAddr::from_str("::").unwrap(), port))
|
||||||
|
.await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue