Allow configuring listen options.

This commit is contained in:
Kavin 2022-11-02 18:42:12 +00:00
parent e5de68aa0c
commit 3ab1529ba7
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD

View file

@ -1,3 +1,4 @@
use std::env;
use std::error::Error; use std::error::Error;
use actix_web::{App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, web}; use actix_web::{App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, web};
@ -12,15 +13,18 @@ use reqwest::{Client, Request, Url};
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
println!("Running server!"); println!("Running server!");
HttpServer::new(|| { let server =HttpServer::new(|| {
// match all requests // match all requests
App::new() App::new()
.default_service(web::to(index)) .default_service(web::to(index))
}) });
.bind("0.0.0.0:8080")? // get port from env
.bind_uds("./socket/actix.sock")? if env::var("UDS").is_ok() {
.run() server.bind_uds("./socket/actix.sock")?
.await } else {
let bind = env::var("BIND").unwrap_or_else(|_| "0.0.0.0:8080".to_string());
server.bind(bind)?
}.run().await
} }
lazy_static!( lazy_static!(