Skip to content

Commit

Permalink
hostname configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fitztrev committed Nov 25, 2023
1 parent add9374 commit acdd208
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 42 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ See the changes on the PGN Viewer demo page: http://localhost:8091/
1. Install the Android SDK
2. Connect your Android phone via USB
3. Make sure your phone is in developer mode and USB debugging is enabled
4. Configure lila to run with your host's hostname instead of localhost
4. Configure lila to run with your host's IP address or hostname instead of localhost
```bash
./lila-docker hostname
```
5. Ensure phone is on wifi and can access lila using that hostname
5. Ensure phone is on wifi and can access lila using that host
```
http://hostname-here:8080 # instead of http://localhost:8080
http://[your private IP]:8080 # instead of http://localhost:8080
```
6. Run:
```bash
Expand Down Expand Up @@ -284,5 +284,5 @@ See the changes on the PGN Viewer demo page: http://localhost:8091/
4. Run the app:
```bash
flutter run --dart-define=LICHESS_HOST=http://hostname-here:8080 --dart-define=LICHESS_WS_HOST=http://hostname-here:8080
flutter run --dart-define=LICHESS_HOST=http://[your private IP]:8080 --dart-define=LICHESS_WS_HOST=http://[your private IP]:8080
```
93 changes: 90 additions & 3 deletions command/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ edition = "2021"
[dependencies]
cliclack = "0.1.6"
colored = "2.0.4"
local-ip-address = "0.5.6"
serde = { version = "1.0", features = ["derive"] }
strum = { version = "0.25.0", features = ["derive", "strum_macros"] }
132 changes: 105 additions & 27 deletions command/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cliclack::{confirm, input, intro, log, multiselect, spinner};
use cliclack::{confirm, input, intro, multiselect, select, spinner};
use colored::Colorize;
use local_ip_address::local_ip;
use serde::{Deserialize, Serialize};
use std::{io::Error, path::Path};
use std::{collections::HashMap, io::Error, path::Path};
use strum::{EnumIter, EnumString, IntoEnumIterator};

const BANNER: &str = r"
Expand All @@ -15,22 +16,64 @@ const BANNER: &str = r"

#[derive(Debug, Serialize, Deserialize)]
struct Config {
profiles: Vec<String>,
setup_database: bool,
su_password: String,
password: String,
profiles: Option<Vec<String>>,
setup_database: Option<bool>,
su_password: Option<String>,
password: Option<String>,
hostname: Option<String>,
}

impl Config {
fn to_env(&self) -> String {
let mut env = String::new();
fn to_env(&self) {
let current_env_contents = match std::fs::read_to_string(".env") {
Ok(config) => config,
Err(_) => String::new(),
};

env.push_str(&format!("COMPOSE_PROFILES={}\n", self.profiles.join(",")));
env.push_str(&format!("SETUP_DATABASE={}\n", self.setup_database));
env.push_str(&format!("SU_PASSWORD={}\n", self.su_password));
env.push_str(&format!("PASSWORD={}\n", self.password));
let mut config_values: HashMap<String, String> = HashMap::new();
for line in current_env_contents.lines() {
let mut split = line.split('=');
let key = split.next().unwrap();
let value = split.next().unwrap();
config_values.insert(key.to_string(), value.to_string());
}

let mut new_config_values: HashMap<String, String> = HashMap::new();

if let Some(profiles) = &self.profiles {
new_config_values.insert(
"COMPOSE_PROFILES".to_string(),
profiles.join(",").to_string(),
);
}

if let Some(setup_database) = &self.setup_database {
new_config_values.insert("SETUP_DATABASE".to_string(), setup_database.to_string());
}

if let Some(su_password) = &self.su_password {
new_config_values.insert("SU_PASSWORD".to_string(), su_password.to_string());
}

if let Some(password) = &self.password {
new_config_values.insert("PASSWORD".to_string(), password.to_string());
}

env
if let Some(hostname) = &self.hostname {
new_config_values.insert("LILA_HOSTNAME".to_string(), hostname.to_string());
}

config_values.extend(new_config_values);

std::fs::write(
".env",
config_values
.iter()
.map(|(k, v)| format!("{k}={v}"))
.collect::<Vec<String>>()
.join("\n"),
)
.unwrap();
}
}

Expand Down Expand Up @@ -99,6 +142,7 @@ fn main() -> std::io::Result<()> {

match args[1].as_str() {
"setup" => setup(),
"hostname" => hostname(),
"gitpod-welcome" => {
gitpod_welcome();
Ok(())
Expand Down Expand Up @@ -134,16 +178,20 @@ fn setup() -> std::io::Result<()> {
(String::new(), String::new())
};

let config = Config {
profiles: services
.iter()
.filter_map(|service| service.compose_profile.clone())
.map(|profile| profile.to_string())
.collect(),
setup_database,
su_password,
password,
};
Config {
profiles: Some(
services
.iter()
.filter_map(|service| service.compose_profile.clone())
.map(|profile| profile.to_string())
.collect(),
),
setup_database: Some(setup_database),
su_password: Some(su_password),
password: Some(password),
hostname: None,
}
.to_env();

// Create a placeholder directory for each of the repos
// otherwise the directories will be created by Docker
Expand Down Expand Up @@ -204,9 +252,6 @@ fn setup() -> std::io::Result<()> {
progress.stop(format!("Clone {repo} ✓"));
}

std::fs::write(".env", config.to_env())?;
log::success("Wrote .env")?;

Ok(())
}

Expand All @@ -221,7 +266,7 @@ fn prompt_for_optional_services() -> Result<Vec<OptionalService>, Error> {
compose_profile: Some(ComposeProfile::Mobile),
repositories: vec![Repository::Mobile].into(),
},
"Mobile app",
"Mobile",
"Flutter-based mobile app",
)
.item(
Expand Down Expand Up @@ -331,6 +376,39 @@ fn prompt_for_optional_services() -> Result<Vec<OptionalService>, Error> {
.interact()
}

fn hostname() -> std::io::Result<()> {
let local_ip = match local_ip() {
Ok(ip) => ip.to_string(),
_ => "127.0.0.1".to_string(),
};

let hostname: String = match select("Select a hostname to access your local Lichess instance:")
.initial_value("localhost")
.item("localhost", "localhost", "default")
.item(
local_ip.as_str(),
local_ip.as_str(),
"Your private IP address",
)
.item("other", "Other", "Enter a custom hostname")
.interact()?
{
"other" => input("Enter a custom hostname: (It must be resolvable)").interact()?,
selection => selection.to_string(),
};

Config {
profiles: None,
setup_database: None,
su_password: None,
password: None,
hostname: Some(hostname),
}
.to_env();

Ok(())
}

fn gitpod_welcome() {
println!("{}", "################".green());
println!(
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ services:
- lila-network
environment:
- SCHEME=${SCHEME:-http}
- LILA_DOMAIN=${LILA_DOMAIN:-localhost:8080}
- PICFIT_DOMAIN=${PICFIT_DOMAIN:-localhost:3001}
- LILA_DOMAIN=${LILA_DOMAIN:-${LILA_HOSTNAME:-localhost}:8080}
- PICFIT_DOMAIN=${PICFIT_DOMAIN:-${LILA_HOSTNAME:-localhost}:3001}
volumes:
- ./repos/lila:/lila
- ./repos/chessground:/chessground
Expand All @@ -42,7 +42,7 @@ services:
- lila-network
environment:
- SCHEME=${SCHEME:-http}
- LILA_DOMAIN=${LILA_DOMAIN:-localhost:8080}
- LILA_DOMAIN=${LILA_DOMAIN:-${LILA_HOSTNAME:-localhost}:8080}
volumes:
- ./repos/lila-ws:/lila-ws
- ./conf/lila-ws.conf:/lila-ws.conf
Expand Down
5 changes: 2 additions & 3 deletions docker/mobile.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
FROM ghcr.io/cirruslabs/flutter:3.13.9
FROM ghcr.io/cirruslabs/flutter:3.16.0

RUN apt-get update
RUN apt install -y \
RUN apt install --yes \
clang \
cmake \
iputils-ping \
libgtk-3-dev \
ninja-build \
pkg-config
Expand Down
Loading

0 comments on commit acdd208

Please sign in to comment.