Skip to content

Commit 5ff43d7

Browse files
authored
Merge pull request #827 from dtolnay-contrib/uiroot
2 parents 76cfa25 + f566376 commit 5ff43d7

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,16 @@ basic HTML/CSS/JS changes, directly open in your browser the built
9696

9797
### Build and run the server
9898

99-
Configure your `.env` file as described in the [ui README](./ui/README.md).
100-
10199
```
102100
cd ui
103101
cargo run
104102
```
105103

104+
There are some optional configuration parameters described in the
105+
[ui README](./ui/README.md) which you may set in a `.env` file. The server will
106+
run with no configuration, but in order to load and save gists a GitHub token
107+
must be configured.
108+
106109
### Build or download the containers
107110
```
108111
cd compiler

ui/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ disk in this directory.
66
In production, these should be set according to your deployment method
77
of choice.
88

9-
| Key | Required | Default Value | Description |
10-
| -------------------------- | ---------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------- |
11-
| `PLAYGROUND_UI_ROOT` | **Yes** | | The path to the HTML, CSS, and Javascript files; for local testing probably you'll use: `PLAYGROUND_UI_ROOT=frontend/build` |
12-
| `PLAYGROUND_GITHUB_TOKEN` | No | | The [GitHub API token][gist] to read and write Gists |
13-
| `PLAYGROUND_UI_ADDRESS` | No | 127.0.0.1 | The address to listen on |
14-
| `PLAYGROUND_UI_PORT` | No | 5000 | The port to listen on |
15-
| `PLAYGROUND_METRICS_TOKEN` | No | | If set, will require authentication for the metrics endpoint |
16-
| `PLAYGROUND_CORS_ENABLED` | No | | If set, will enable CORS support |
17-
| `TMPDIR` | No | system-provided | Where compilation artifacts will be saved. Must be accessible to Docker |
9+
| Key | Required | Default Value | Description |
10+
| -------------------------- | -------- | ----------------- | ------------------------------------------------------------------------------------- |
11+
| `PLAYGROUND_UI_ROOT` | No | | The path to the HTML, CSS, and Javascript files (the directory containing index.html) |
12+
| `PLAYGROUND_GITHUB_TOKEN` | No | | The [GitHub API token][gist] to read and write Gists |
13+
| `PLAYGROUND_UI_ADDRESS` | No | 127.0.0.1 | The address to listen on |
14+
| `PLAYGROUND_UI_PORT` | No | 5000 | The port to listen on |
15+
| `PLAYGROUND_METRICS_TOKEN` | No | | If set, will require authentication for the metrics endpoint |
16+
| `PLAYGROUND_CORS_ENABLED` | No | | If set, will enable CORS support |
17+
| `TMPDIR` | No | system-provided | Where compilation artifacts will be saved. Must be accessible to Docker |
1818

1919
[dotenv]: https://crates.io/crates/dotenv
2020
[gist]: https://developer.github.com/v3/gists/#authentication

ui/src/env.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub use std::env::*;
22

33
pub const PLAYGROUND_GITHUB_TOKEN: &str = "PLAYGROUND_GITHUB_TOKEN";
4+
pub const PLAYGROUND_UI_ROOT: &str = "PLAYGROUND_UI_ROOT";

ui/src/main.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![deny(rust_2018_idioms)]
22

3-
use crate::env::PLAYGROUND_GITHUB_TOKEN;
3+
use crate::env::{PLAYGROUND_GITHUB_TOKEN, PLAYGROUND_UI_ROOT};
44
use serde::{Deserialize, Serialize};
55
use snafu::prelude::*;
66
use std::{
@@ -25,8 +25,8 @@ fn main() {
2525
let _ = dotenv::dotenv();
2626
openssl_probe::init_ssl_cert_env_vars();
2727

28-
// Enable warn-level logging by default. env_logger's default is error only.
29-
let env_logger_config = env_logger::Env::default().default_filter_or("warn");
28+
// Enable info-level logging by default. env_logger's default is error only.
29+
let env_logger_config = env_logger::Env::default().default_filter_or("info");
3030
env_logger::Builder::from_env(env_logger_config).init();
3131

3232
let config = Config::from_env();
@@ -44,9 +44,36 @@ struct Config {
4444

4545
impl Config {
4646
fn from_env() -> Self {
47-
let root: PathBuf = env::var_os("PLAYGROUND_UI_ROOT")
48-
.expect("Must specify PLAYGROUND_UI_ROOT")
49-
.into();
47+
let root = if let Some(root) = env::var_os(PLAYGROUND_UI_ROOT) {
48+
// Ensure it appears as an absolute path in logs to help user orient
49+
// themselves about what directory the PLAYGROUND_UI_ROOT
50+
// configuration is interpreted relative to.
51+
let mut root = PathBuf::from(root);
52+
if !root.is_absolute() {
53+
if let Ok(current_dir) = env::current_dir() {
54+
root = current_dir.join(root);
55+
}
56+
}
57+
root
58+
} else {
59+
// Note this is `env!` (compile time) while the above is
60+
// `env::var_os` (run time). We know where the ui is expected to be
61+
// relative to the source code that the server was compiled from.
62+
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
63+
.join("frontend")
64+
.join("build")
65+
};
66+
67+
let index_html = root.join("index.html");
68+
if index_html.exists() {
69+
log::info!("Serving playground frontend from {}", root.display());
70+
} else {
71+
log::error!(
72+
"Playground ui does not exist at {}\n\
73+
Playground will not work until `yarn run build` has been run or {PLAYGROUND_UI_ROOT} has been fixed",
74+
index_html.display(),
75+
);
76+
}
5077

5178
let address =
5279
env::var("PLAYGROUND_UI_ADDRESS").unwrap_or_else(|_| DEFAULT_ADDRESS.to_string());

0 commit comments

Comments
 (0)