Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make port configurable when starting repo #88

Merged
merged 1 commit into from
Nov 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bldr/command/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ use repo;
///
/// * Fails if the repository fails to start - canot bind to the port, etc.
pub fn start(config: &Config) -> BldrResult<()> {
println!("Repo listening on {:?}", config.repo_addr());
repo::run(&config)
}
14 changes: 14 additions & 0 deletions src/bldr/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
//!
//! See the [Config](struct.Config.html) struct for the specific options available.

use std::net;

use topology::Topology;
use repo;

#[derive(Debug, Clone, PartialEq, Eq)]
/// An enum with the various CLI commands. Used to keep track of what command was called.
Expand Down Expand Up @@ -60,6 +63,8 @@ pub struct Config {
release: String,
watch: Vec<String>,
key: String,
listen_addr: repo::ListenAddr,
port: repo::ListenPort,
}

impl Config {
Expand Down Expand Up @@ -188,6 +193,15 @@ impl Config {
pub fn topology(&self) -> &Topology {
&self.topology
}

pub fn set_port(&mut self, port: u16) -> &mut Config {
self.port = repo::ListenPort(port);
self
}

pub fn repo_addr(&self) -> net::SocketAddrV4 {
net::SocketAddrV4::new(self.listen_addr.0.clone(), self.port.0.clone())
}
}

#[cfg(test)]
Expand Down
18 changes: 17 additions & 1 deletion src/bldr/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use iron::request::Body;
use iron::headers;
use router::Router;

use std::net;
use std::sync::Arc;
use std::fs::{self, File};
use std::io::{Read, Write, BufWriter};
Expand All @@ -25,6 +26,21 @@ impl Repo {
}
}

pub struct ListenAddr(pub net::Ipv4Addr);
pub struct ListenPort(pub u16);

impl Default for ListenAddr {
fn default() -> Self {
ListenAddr(net::Ipv4Addr::new(0, 0, 0, 0))
}
}

impl Default for ListenPort {
fn default() -> Self {
ListenPort(9632)
}
}

fn write_file(path: PathBuf, filename: PathBuf, body: &mut Body) -> BldrResult<bool> {
try!(fs::create_dir_all(path));
let tempfile = format!("{}.tmp", filename.to_string_lossy());
Expand Down Expand Up @@ -166,6 +182,6 @@ pub fn run(config: &Config) -> BldrResult<()> {
router.post("/keys/:key", move |r: &mut Request| upload_key(&repo4, r));
router.get("/keys/:key", move |r: &mut Request| download_key(&repo5, r));

Iron::new(router).http("0.0.0.0:9632").unwrap();
Iron::new(router).http(config.repo_addr()).unwrap();
Ok(())
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Usage: bldr install <package> -u <url> [-v <version>] [-r <release>]
bldr start <package> [--group=<group>] [--topology=<topology>] [--watch=<watch>...]
bldr sh
bldr bash
bldr repo [-p <path>]
bldr repo [-p <path>] [--port=<port>]
bldr upload <package> -u <url> [-v <version>] [-r <release>]
bldr key <key> [-u <url>]
bldr key-upload <key> -u <url>
Expand Down Expand Up @@ -78,6 +78,7 @@ struct Args {
arg_package: Option<String>,
arg_key: Option<String>,
flag_path: String,
flag_port: Option<u16>,
flag_version: String,
flag_release: String,
flag_url: String,
Expand Down Expand Up @@ -113,6 +114,9 @@ fn config_from_args(args: &Args, command: Command) -> BldrResult<Config> {
t => return Err(BldrError::UnknownTopology(String::from(t))),
}
}
if let Some(port) = args.flag_port {
config.set_port(port);
}
config.set_url(args.flag_url.clone());
config.set_group(args.flag_group.clone());
config.set_watch(args.flag_watch.clone());
Expand Down