Skip to content
This repository has been archived by the owner on May 21, 2023. It is now read-only.

Commit

Permalink
Moved to passing Args directly to server
Browse files Browse the repository at this point in the history
  • Loading branch information
natnat-mc committed Dec 23, 2021
1 parent d8fd882 commit a03bfbe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
11 changes: 1 addition & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,7 @@ fn main() {
}
};

if let Err(e) = server::run(
args.port,
args.allowed_hosts,
args.headers,
args.disable_preview,
args.allow_reload_api,
args.allow_reload_signal,
args.reload_interval,
args.tilesets,
) {
if let Err(e) = server::run(args) {
error!("Server error: {}", e);
std::process::exit(1);
}
Expand Down
34 changes: 13 additions & 21 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
use crate::config::Args;
use hyper::service::{make_service_fn, service_fn};
use hyper::Server;
use std::time::Duration;

use crate::service;
use crate::tiles::Tilesets;

#[tokio::main]
pub async fn run(
port: u16,
allowed_hosts: Vec<String>,
headers: Vec<(String, String)>,
disable_preview: bool,
allow_reload_api: bool,
allow_reload_signal: bool,
reload_interval: Option<Duration>,
tilesets: Tilesets,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let addr = ([0, 0, 0, 0], port).into();
pub async fn run(args: Args) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let addr = ([0, 0, 0, 0], args.port).into();
let server = Server::try_bind(&addr)?;

let service = {
let tilesets = tilesets.clone();
let args = args.clone();
make_service_fn(move |_conn| {
let tilesets = tilesets.clone();
let allowed_hosts = allowed_hosts.clone();
let headers = headers.clone();
let tilesets = args.tilesets.clone();
let allowed_hosts = args.allowed_hosts.clone();
let headers = args.headers.clone();
let disable_preview = args.disable_preview;
let allow_reload_api = args.allow_reload_api;
async move {
Ok::<_, hyper::Error>(service_fn(move |req| {
service::get_service(
Expand All @@ -40,8 +32,8 @@ pub async fn run(
})
};

if allow_reload_signal {
let tilesets = tilesets.clone();
if args.allow_reload_signal {
let tilesets = args.tilesets.clone();
println!("Reloading on SIGHUP");
tokio::spawn(async move {
let mut handler =
Expand All @@ -54,8 +46,8 @@ pub async fn run(
});
}

if let Some(interval) = reload_interval {
let tilesets = tilesets.clone();
if let Some(interval) = args.reload_interval {
let tilesets = args.tilesets.clone();
println!("Reloading every {} seconds", interval.as_secs());
tokio::spawn(async move {
let mut timer = tokio::time::interval(interval);
Expand Down

0 comments on commit a03bfbe

Please sign in to comment.