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

Allow setting UI and API ports via env var #7

Merged
merged 1 commit into from
Aug 19, 2022

Conversation

resnickmicah
Copy link
Collaborator

Make sure the UI and API respect any values set for ACTIX_PORT and TRUNK_SERVE_PORT. Have these values default to ACTIX_PORT=8080 and TRUNK_SERVE_PORT=80 in the absence of any .env file.

@resnickmicah
Copy link
Collaborator Author

resnickmicah commented Aug 18, 2022

On my machine, running with rootless docker the default values (not copying .env.sample to .env) results in the following output:

~/Documents/rs/yew-actix-template$ make up
docker compose -f docker/docker-compose.yaml up
WARN[0000] The "OAUTH_CLIENT_ID" variable is not set. Defaulting to a blank string. 
WARN[0000] The "OAUTH_AUTH_URL" variable is not set. Defaulting to a blank string. 
WARN[0000] The "OAUTH_TOKEN_URL" variable is not set. Defaulting to a blank string. 
WARN[0000] The "OAUTH_CLIENT_SECRET" variable is not set. Defaulting to a blank string. 
[+] Running 4/4
 ⠿ Container docker-postgres-1   Created                                                                                                                                                                                                               0.1s
 ⠿ Container docker-yew-ui-1     Created                                                                                                                                                                                                               0.1s
 ⠿ Container docker-actix-api-1  Created                                                                                                                                                                                                               0.1s
 ⠿ Container docker-dbmate-1     Created                                                                                                                                                                                                               0.1s
Attaching to docker-actix-api-1, docker-dbmate-1, docker-postgres-1, docker-yew-ui-1
docker-postgres-1   | The files belonging to this database system will be owned by user "postgres".
docker-postgres-1   | This user must also own the server process.
docker-postgres-1   | 
docker-postgres-1   | The database cluster will be initialized with locale "en_US.utf8".
docker-postgres-1   | The default database encoding has accordingly been set to "UTF8".
docker-postgres-1   | The default text search configuration will be set to "english".
docker-postgres-1   | 
docker-postgres-1   | Data page checksums are disabled.
docker-postgres-1   | 
docker-postgres-1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
docker-postgres-1   | creating subdirectories ... ok
docker-postgres-1   | selecting dynamic shared memory implementation ... posix
docker-postgres-1   | selecting default max_connections ... 100
docker-postgres-1   | selecting default shared_buffers ... 128MB
docker-postgres-1   | selecting default time zone ... Etc/UTC
docker-postgres-1   | creating configuration files ... ok
docker-postgres-1   | running bootstrap script ... ok
docker-postgres-1   | performing post-bootstrap initialization ... ok
Error response from daemon: driver failed programming external connectivity on endpoint docker-yew-ui-1 (ff329d5f0589ddde262492c66d29e6beddaf69846d6688ec1ba40e67620aaacd): Error starting userland proxy: error while calling PortManager.AddPort(): cannot expose privileged port 80, you can add 'net.ipv4.ip_unprivileged_port_start=80' to /etc/sysctl.conf (currently 1024), or set CAP_NET_BIND_SERVICE on rootlesskit binary, or choose a larger port number (>= 1024): listen tcp4 0.0.0.0:80: bind: permission denied
make: *** [Makefile:6: up] Error 1

@resnickmicah
Copy link
Collaborator Author

resnickmicah commented Aug 18, 2022

Running on this branch with the following values for my .env file results in the following:
.env (secrets redacted):

OAUTH_CLIENT_ID=(secret)
OAUTH_AUTH_URL=https://accounts.google.com/o/oauth2/v2/auth
OAUTH_TOKEN_URL=https://oauth2.googleapis.com/token
OAUTH_CLIENT_SECRET=(secret)
TRUNK_SERVE_PORT=8081
ACTIX_PORT=8086

Abridged output from yew-ui (last few lines):

docker-yew-ui-1     | warning: unused `Result` that must be used
docker-yew-ui-1     |    --> src/main.rs:125:9
docker-yew-ui-1     |     |
docker-yew-ui-1     | 125 |         document().unchecked_into::<HtmlDocument>().set_cookie("");
docker-yew-ui-1     |     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
docker-yew-ui-1     |     |
docker-yew-ui-1     |     = note: `#[warn(unused_must_use)]` on by default
docker-yew-ui-1     |     = note: this `Result` may be an `Err` variant, which should be handled
docker-yew-ui-1     | 
docker-yew-ui-1     | warning: `yew-ui` (bin "yew-ui") generated 1 warning
docker-yew-ui-1     |     Finished dev [unoptimized + debuginfo] target(s) in 29.31s
docker-yew-ui-1     | 2022-08-18T00:00:53.285060Z  INFO fetching cargo artifacts
docker-yew-ui-1     | 2022-08-18T00:00:53.425205Z  INFO processing WASM for yew-ui
docker-yew-ui-1     | 2022-08-18T00:00:53.460014Z  INFO using system installed binary app=wasm-bindgen version=0.2.82
docker-yew-ui-1     | 2022-08-18T00:00:53.460225Z  INFO calling wasm-bindgen for yew-ui
docker-yew-ui-1     | 2022-08-18T00:00:53.783398Z  INFO copying generated wasm-bindgen artifacts
docker-yew-ui-1     | 2022-08-18T00:00:53.786152Z  INFO applying new distribution
docker-yew-ui-1     | 2022-08-18T00:00:53.786719Z  INFO  success
docker-yew-ui-1     | 2022-08-18T00:00:53.788364Z  INFO  serving static assets at -> /
docker-yew-ui-1     | 2022-08-18T00:00:53.788393Z  INFO  server listening at http://0.0.0.0:8081

Abridged output from actix-api (last few lines):

docker-actix-api-1  |     Finished dev [unoptimized + debuginfo] target(s) in 13.60s
docker-actix-api-1  |      Running `target/debug/actix-api`
docker-actix-api-1  | [2022-08-18T00:00:36Z INFO  actix_server::builder] Starting 8 workers
docker-actix-api-1  | [2022-08-18T00:00:36Z INFO  actix_server::server] Actix runtime found; starting in Actix runtime

@resnickmicah
Copy link
Collaborator Author

@darioalessandro let me know if you'd like the Postgres port parametrized as well. Wasn't as big a priority for me because it wasn't running on a privileged port, but might as well since I did the actix API port and that wasn't running on anything privileged either.

const ACTIX_PORT: &str = std::env!("ACTIX_PORT");
const UI_PORT: &str = std::env!("TRUNK_SERVE_PORT");
const UI_HOST: &str = std::env!("TRUNK_SERVE_HOST");
const AFTER_LOGIN_URL: &'static str = concat!("http://localhost:", std::env!("TRUNK_SERVE_PORT"));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt kinda iffy about this line (specifically having to repeat the same std::env expression on lines 29 and 31), but it was the most concise-looking way I found to append TRUNK_SERVE_PORT to a string slice after a brief bit of googling. Let me know if there's a better way or if you prefer the accepted answer from that stack overflow post, I'm still very much a Rust newbie.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo this is fine.

- ACTIX_PORT=8080
- TRUNK_SERVE_PORT=80
- ACTIX_PORT=${ACTIX_PORT:-8080}
- TRUNK_SERVE_PORT=${TRUNK_SERVE_PORT:-80}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@resnickmicah this is awesome!!

I did not know you could provide defaults like this!!!

ports:
- "80:80"
- "${TRUNK_SERVE_PORT:-80}:${TRUNK_SERVE_PORT:-80}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaaaaas!!

- RUST_LOG=info
- PG_URL=postgres://postgres:docker@postgres:5432/actix-api-db?sslmode=disable
ports:
- "8080:8080"
- "${ACTIX_PORT:-8080}:${ACTIX_PORT:-8080}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!!

const ACTIX_PORT: &str = std::env!("ACTIX_PORT");
const UI_PORT: &str = std::env!("TRUNK_SERVE_PORT");
const UI_HOST: &str = std::env!("TRUNK_SERVE_HOST");
const AFTER_LOGIN_URL: &'static str = concat!("http://localhost:", std::env!("TRUNK_SERVE_PORT"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo this is fine.

@darioalessandro darioalessandro merged commit 17b7a65 into main Aug 19, 2022
@darioalessandro darioalessandro deleted the allow-setting-ports-via-env-var branch August 19, 2022 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants