unix socket powers

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-04-01 19:09:20 -04:00
parent b1f6e6d823
commit 1413226358
3 changed files with 569 additions and 649 deletions

1169
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "printerfacts"
version = "0.2.0"
version = "0.3.0"
authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018"
build = "build.rs"
@ -10,15 +10,16 @@ build = "build.rs"
[dependencies]
anyhow = "1"
lazy_static = "1.4"
log = "0"
mime = "0.3.0"
pfacts = "0.1.0"
pretty_env_logger = "0"
prometheus = { version = "0.9", default-features = false, features = ["process"] }
rand = "0"
serde = { version = "1", features = ["derive"] }
tokio = { version = "0.2", features = ["macros"] }
warp = "0.2"
tokio = { version = "1", features = ["full"] }
tokio-stream = { version = "0.1.5", features = ["net"] }
tracing = "0.1"
tracing-subscriber = "0.2"
warp = "0.3"
[build-dependencies]
ructe = { version = "0.11", features = ["warp02"] }

View File

@ -3,7 +3,9 @@ use lazy_static::lazy_static;
use pfacts::Facts;
use prometheus::{opts, register_int_counter_vec, IntCounterVec};
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};
include!(concat!(env!("OUT_DIR"), "/templates.rs"));
@ -36,7 +38,7 @@ async fn not_found() -> Result<impl Reply, Rejection> {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
pretty_env_logger::init();
tracing_subscriber::fmt::init();
let facts = pfacts::make();
let fact = {
@ -57,20 +59,32 @@ async fn main() -> anyhow::Result<()> {
.and_then(index);
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);
warp::serve(
let server = warp::serve(
fact_handler
.or(index_handler)
.or(files)
.or(not_found_handler)
.with(warp::log(APPLICATION_NAME)),
)
.run(([0, 0, 0, 0], port))
.await;
Ok(())
);
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(())
} 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(())
}
}