-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstrate Configuring Exported Static Values via Bazel Config Flags
This example now demonstrates how Claro programs can leverage Bazel's builtin functionality to satisfy arbitrary configuration needs without the language itself being burdened with syntax for such a thing. This is a demonstration of compile-time configurability that comes with no runtime overhead of checking flags and such as you'd find in other existing Guice-like dependency injection frameworks. Now, if you'd like to point the Buggy Buggies AI demo server at a local instance of the Buggy Buggies server you can just run the following command to configure the dev build instead of the prod build (in a real project, defaults would probably be inverted): ``` $ bazel run //src/java/com/claro/claro_programs/demo_server/buggy_buggies:buggy_buggies_http_server --//src/java/com/claro/claro_programs/demo_server/buggy_buggies/buggy_buggies_service:env_flag=dev ``` See: https://bazel.build/docs/configurable-attributes.
- Loading branch information
1 parent
6557728
commit 0c6bdce
Showing
5 changed files
with
45 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 37 additions & 5 deletions
42
src/java/com/claro/claro_programs/demo_server/buggy_buggies/buggy_buggies_service/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,48 @@ | ||
load("//src/java/com/claro:claro_build_rules.bzl", "claro_module") | ||
load("@bazel_skylib//rules:common_settings.bzl", "string_flag") | ||
|
||
SRCS = ["buggy_buggies_move_response.claro"] | ||
|
||
# This example now demonstrates how Claro programs can leverage Bazel's builtin functionality to satisfy arbitrary | ||
# configuration needs without the language itself being burdened with syntax for such a thing. This is a demonstration | ||
# of compile-time configurability that comes with no runtime overhead of checking flags and such as you'd find in other | ||
# existing Guice-like dependency injection frameworks. | ||
claro_module( | ||
name = "buggy_buggies_client", | ||
module_api_file = "buggy_buggies_client.claro_module_api", | ||
srcs = [ | ||
"buggy_buggies_client.claro", | ||
"buggy_buggies_move_response.claro", | ||
], | ||
srcs = select({ | ||
":dev": ["buggy_buggies_client_DEV.claro"] + SRCS, | ||
":prod": ["buggy_buggies_client_PROD.claro"] + SRCS, | ||
}), | ||
optional_stdlib_deps = ["http"], | ||
visibility = [ | ||
"//src/java/com/claro/claro_programs/demo_server/buggy_buggies:__pkg__", | ||
"//src/java/com/claro/claro_programs/demo_server/buggy_buggies/endpoint_handlers:__pkg__", | ||
], | ||
) | ||
) | ||
|
||
# For demonstration purposes only, I'm defining a brand new flag here, so that it's obvious where all of the moving | ||
# pieces in the above claro_module() target come from. | ||
# | ||
# If you want to actually override the default flag value, set following flag in your `bazel run` command: | ||
# --//src/java/com/claro/claro_programs/demo_server/buggy_buggies/buggy_buggies_service:env_flag=dev | ||
string_flag( | ||
name = "env_flag", | ||
values = [ | ||
"dev", | ||
"prod", | ||
], | ||
build_setting_default = "prod", | ||
) | ||
config_setting( | ||
name = "dev", | ||
flag_values = { | ||
":env_flag": "dev" | ||
} | ||
) | ||
config_setting( | ||
name = "prod", | ||
flag_values = { | ||
":env_flag": "prod" | ||
} | ||
) |
5 changes: 5 additions & 0 deletions
5
...o_programs/demo_server/buggy_buggies/buggy_buggies_service/buggy_buggies_client_DEV.claro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
provider static_HTTP_CLIENT() -> HttpClient<BuggyBuggies> { | ||
# This now only needs to happen once throughout the entire program. | ||
return http::getHttpClient("http://localhost:4000"); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters