Skip to content

Commit 76cfa25

Browse files
authored
Merge pull request #816 from dtolnay-contrib/token
2 parents c8c0180 + 4763493 commit 76cfa25

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

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 |
12-
| `PLAYGROUND_GITHUB_TOKEN` | **Yes** | | 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` | **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 |
1818

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

ui/src/env.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub use std::env::*;
2+
3+
pub const PLAYGROUND_GITHUB_TOKEN: &str = "PLAYGROUND_GITHUB_TOKEN";

ui/src/main.rs

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

3+
use crate::env::PLAYGROUND_GITHUB_TOKEN;
34
use serde::{Deserialize, Serialize};
4-
use snafu::Snafu;
5+
use snafu::prelude::*;
56
use std::{
67
convert::TryFrom,
7-
env,
88
net::SocketAddr,
99
path::{Path, PathBuf},
1010
sync::Arc,
@@ -14,6 +14,7 @@ const DEFAULT_ADDRESS: &str = "127.0.0.1";
1414
const DEFAULT_PORT: u16 = 5000;
1515

1616
mod asm_cleanup;
17+
mod env;
1718
mod gist;
1819
mod metrics;
1920
mod sandbox;
@@ -23,7 +24,10 @@ fn main() {
2324
// Dotenv may be unable to load environment variables, but that's ok in production
2425
let _ = dotenv::dotenv();
2526
openssl_probe::init_ssl_cert_env_vars();
26-
env_logger::init();
27+
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");
30+
env_logger::Builder::from_env(env_logger_config).init();
2731

2832
let config = Config::from_env();
2933
server_axum::serve(config);
@@ -32,7 +36,7 @@ fn main() {
3236
struct Config {
3337
address: String,
3438
cors_enabled: bool,
35-
gh_token: String,
39+
gh_token: Option<String>,
3640
metrics_token: Option<String>,
3741
port: u16,
3842
root: PathBuf,
@@ -51,8 +55,11 @@ impl Config {
5155
.and_then(|p| p.parse().ok())
5256
.unwrap_or(DEFAULT_PORT);
5357

54-
let gh_token =
55-
env::var("PLAYGROUND_GITHUB_TOKEN").expect("Must specify PLAYGROUND_GITHUB_TOKEN");
58+
let gh_token = env::var(PLAYGROUND_GITHUB_TOKEN).ok();
59+
if gh_token.is_none() {
60+
log::warn!("Environment variable {} is not set, so reading and writing GitHub gists will not work", PLAYGROUND_GITHUB_TOKEN);
61+
}
62+
5663
let metrics_token = env::var("PLAYGROUND_METRICS_TOKEN").ok();
5764

5865
let cors_enabled = env::var_os("PLAYGROUND_CORS_ENABLED").is_some();
@@ -94,11 +101,18 @@ impl Config {
94101
}
95102

96103
#[derive(Debug, Clone)]
97-
struct GhToken(Arc<String>);
104+
struct GhToken(Option<Arc<String>>);
98105

99106
impl GhToken {
100-
fn new(token: impl Into<String>) -> Self {
101-
GhToken(Arc::new(token.into()))
107+
fn new(token: &Option<String>) -> Self {
108+
GhToken(token.clone().map(Arc::new))
109+
}
110+
111+
fn must_get(&self) -> Result<String> {
112+
self.0
113+
.as_ref()
114+
.map(|s| String::clone(s))
115+
.context(NoGithubTokenSnafu)
102116
}
103117
}
104118

@@ -135,6 +149,8 @@ pub enum Error {
135149
GistCreation { source: octocrab::Error },
136150
#[snafu(display("Gist loading failed: {}", source))]
137151
GistLoading { source: octocrab::Error },
152+
#[snafu(display("{PLAYGROUND_GITHUB_TOKEN} not set up for reading/writing gists"))]
153+
NoGithubToken,
138154
#[snafu(display("Unable to serialize response: {}", source))]
139155
Serialization { source: serde_json::Error },
140156
#[snafu(display("The value {:?} is not a valid target", value))]

ui/src/server_axum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ async fn meta_gist_create(
353353
Extension(token): Extension<GhToken>,
354354
Json(req): Json<MetaGistCreateRequest>,
355355
) -> Result<Json<MetaGistResponse>> {
356-
let token = String::clone(&token.0);
356+
let token = token.must_get()?;
357357
gist::create_future(token, req.code)
358358
.await
359359
.map(Into::into)
@@ -365,7 +365,7 @@ async fn meta_gist_get(
365365
Extension(token): Extension<GhToken>,
366366
Path(id): Path<String>,
367367
) -> Result<Json<MetaGistResponse>> {
368-
let token = String::clone(&token.0);
368+
let token = token.must_get()?;
369369
gist::load_future(token, &id)
370370
.await
371371
.map(Into::into)

0 commit comments

Comments
 (0)