server: Make 'rojo serve' respect --port option

This commit is contained in:
Lucien Greathouse
2019-01-04 13:26:09 -08:00
parent 86e0f3fabe
commit 9cfeee0577
3 changed files with 61 additions and 21 deletions

View File

@@ -55,15 +55,37 @@ fn main() {
let project_path = Path::new(sub_matches.value_of("PATH").unwrap_or("."));
let full_path = make_path_absolute(project_path);
librojo::commands::init(&full_path);
commands::init(&full_path);
},
("serve", Some(sub_matches)) => {
let project_path = match sub_matches.value_of("PROJECT") {
let fuzzy_project_path = match sub_matches.value_of("PROJECT") {
Some(v) => make_path_absolute(Path::new(v)),
None => std::env::current_dir().unwrap(),
};
librojo::commands::serve(&project_path);
let port = match sub_matches.value_of("port") {
Some(v) => match v.parse::<u16>() {
Ok(port) => port,
Err(_) => {
error!("Invalid port {}", v);
process::exit(1);
},
},
None => None,
};
let options = commands::ServeOptions {
fuzzy_project_path,
port,
};
match commands::serve(&options) {
Ok(_) => {},
Err(e) => {
error!("{}", e);
process::exit(1);
},
}
},
("build", Some(sub_matches)) => {
let fuzzy_project_path = match sub_matches.value_of("PROJECT") {

View File

@@ -1,36 +1,54 @@
use std::{
path::Path,
process,
path::PathBuf,
sync::Arc,
};
use failure::Fail;
use crate::{
project::Project,
project::{Project, ProjectLoadFuzzyError},
web::Server,
session::Session,
// roblox_studio,
};
pub fn serve(fuzzy_project_location: &Path) {
info!("Looking for project at {}", fuzzy_project_location.display());
const DEFAULT_PORT: u16 = 34872;
let project = match Project::load_fuzzy(fuzzy_project_location) {
Ok(project) => project,
Err(error) => {
error!("{}", error);
process::exit(1);
},
};
#[derive(Debug)]
pub struct ServeOptions {
pub fuzzy_project_path: PathBuf,
pub port: Option<u16>,
}
#[derive(Debug, Fail)]
pub enum ServeError {
#[fail(display = "Project load error: {}", _0)]
ProjectLoadError(#[fail(cause)] ProjectLoadFuzzyError),
}
impl From<ProjectLoadFuzzyError> for ServeError {
fn from(error: ProjectLoadFuzzyError) -> ServeError {
ServeError::ProjectLoadError(error)
}
}
pub fn serve(options: &ServeOptions) -> Result<(), ServeError> {
info!("Looking for project at {}", options.fuzzy_project_path.display());
let project = Project::load_fuzzy(&options.fuzzy_project_path)?;
info!("Found project at {}", project.file_location.display());
info!("Using project {:#?}", project);
// roblox_studio::install_bundled_plugin().unwrap();
let session = Arc::new(Session::new(project).unwrap());
let server = Server::new(Arc::clone(&session));
println!("Server listening on port 34872");
let port = options.port
// .or(project.serve_port)
.unwrap_or(DEFAULT_PORT);
server.listen(34872);
println!("Rojo server listening on port {}", port);
server.listen(port);
Ok(())
}

View File

@@ -188,7 +188,7 @@ impl Server {
)
}
pub fn listen(self, port: u64) {
pub fn listen(self, port: u16) {
let address = format!("0.0.0.0:{}", port);
rouille::start_server(address, move |request| self.handle_request(request));