Skip to content

Commit

Permalink
ACCEPT_HTTP1 -> DISABLE_GRPC_WEB
Browse files Browse the repository at this point in the history
  • Loading branch information
TinyKitten committed Dec 2, 2024
1 parent 15a3956 commit 3da996a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- ..:/workspace:cached
environment:
DATABASE_URL: mariadb://stationapi:password@db-dc/stationapi
ACCEPT_HTTP1: true
DISABLE_GRPC_WEB: false
HOST: 0.0.0.0
ports:
- 50051:50051
Expand Down
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## API
DATABASE_URL=mysql://root:password@localhost:3306/stationapi
ACCEPT_HTTP1=false
DISABLE_GRPC_WEB=false

## Migration
MYSQL_USER=
Expand Down
2 changes: 1 addition & 1 deletion compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- migration
environment:
DATABASE_URL: mysql://stationapi:password@db/stationapi
ACCEPT_HTTP1: true
DISABLE_GRPC_WEB: false
HOST: 0.0.0.0
ports:
- 50051:50051
Expand Down
5 changes: 2 additions & 3 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ services:
build:
context: .
dockerfile: ./docker/api/Dockerfile.dev
command: cargo watch -s "cargo run -p stationapi"
command: bacon run -- --bin stationapi
depends_on:
- db
volumes:
- .:/app
environment:
DATABASE_URL: mariadb://stationapi:password@db/stationapi
ACCEPT_HTTP1: true
ENABLE_ALL_STATIONS_RPC: true
DISABLE_GRPC_WEB: false
HOST: 0.0.0.0
ports:
- 50051:50051
Expand Down
32 changes: 20 additions & 12 deletions stationapi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async fn run() -> std::result::Result<(), anyhow::Error> {

tokio::spawn(station_api_service_status(health_reporter.clone()));

let accept_http1 = fetch_http1_flag();
let disable_grpc_web = fetch_disable_grpc_web_flag();
let addr = fetch_addr()?;

let db_url = fetch_database_url();
Expand All @@ -79,12 +79,20 @@ async fn run() -> std::result::Result<(), anyhow::Error> {

info!("StationAPI Server listening on {}", addr);

Server::builder()
.accept_http1(accept_http1)
.add_service(tonic_web::enable(health_service))
.add_service(tonic_web::enable(svc))
.serve(addr)
.await?;
if disable_grpc_web == true {
Server::builder()
.add_service(health_service)
.add_service(svc)
.serve(addr)
.await?;
} else {
Server::builder()
.accept_http1(true)
.add_service(tonic_web::enable(health_service))
.add_service(tonic_web::enable(svc))
.serve(addr)
.await?;
}

Ok(())
}
Expand Down Expand Up @@ -120,13 +128,13 @@ fn fetch_database_url() -> String {
Err(VarError::NotUnicode(_)) => panic!("$DATABASE_URL should be written in Unicode."),
}
}
fn fetch_http1_flag() -> bool {
match env::var("ACCEPT_HTTP1") {
Ok(s) => s.parse().expect("Failed to parse $ACCEPT_HTTP1"),
fn fetch_disable_grpc_web_flag() -> bool {
match env::var("DISABLE_GRPC_WEB") {
Ok(s) => s.parse().expect("Failed to parse $DISABLE_GRPC_WEB"),
Err(env::VarError::NotPresent) => {
warn!("$ACCEPT_HTTP1 is not set. Falling back to false.");
warn!("$DISABLE_GRPC_WEB is not set. Falling back to false.");
false
}
Err(VarError::NotUnicode(_)) => panic!("$ACCEPT_HTTP1 should be written in Unicode."),
Err(VarError::NotUnicode(_)) => panic!("$DISABLE_GRPC_WEB should be written in Unicode."),
}
}

0 comments on commit 3da996a

Please sign in to comment.