Skip to content

Commit f62f43d

Browse files
authored
feat!: Add url to api,stomp and greylog (#1056)
closes #985
1 parent 2a751f7 commit f62f43d

File tree

13 files changed

+46
-58
lines changed

13 files changed

+46
-58
lines changed

helm/blueapi/values.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ tracing:
9595
# Config for the worker goes here, will be mounted into a config file
9696
worker:
9797
api:
98-
host: 0.0.0.0 # Allow non-loopback traffic
99-
port: 8000
98+
url: http://0.0.0.0:8000/ # Allow non-loopback traffic
10099
env:
101100
sources:
102101
- kind: deviceFunctions
@@ -112,8 +111,7 @@ worker:
112111
auth:
113112
username: guest
114113
password: guest
115-
host: rabbitmq
116-
port: 61613
114+
url: http://rabbitmq:61613/
117115
scratch:
118116
root: /blueapi-plugins/scratch
119117
repositories: []
@@ -123,8 +121,7 @@ worker:
123121
level: "INFO"
124122
graylog:
125123
enabled: False
126-
host: "graylog-log-target.diamond.ac.uk"
127-
port: 12232
124+
url: http://graylog-log-target.diamond.ac.uk:12232/
128125
initContainer:
129126
enabled: false
130127

src/blueapi/cli/cli.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,13 @@ def listen_to_events(obj: dict) -> None:
186186
config: ApplicationConfig = obj["config"]
187187
if not config.stomp.enabled:
188188
raise BlueskyStreamingError("Message bus needs to be configured")
189+
assert config.stomp.url.host is not None, "Stomp URL missing host"
190+
assert config.stomp.url.port is not None, "Stomp URL missing port"
189191
event_bus_client = EventBusClient(
190192
StompClient.for_broker(
191193
broker=Broker(
192-
host=config.stomp.host,
193-
port=config.stomp.port,
194+
host=config.stomp.url.host,
195+
port=config.stomp.url.port,
194196
auth=config.stomp.auth,
195197
)
196198
)
@@ -205,7 +207,7 @@ def on_event(
205207

206208
print(
207209
"Subscribing to all bluesky events from "
208-
f"{config.stomp.host}:{config.stomp.port}",
210+
f"{config.stomp.url.host}:{config.stomp.url.port}",
209211
file=sys.stderr,
210212
)
211213
with event_bus_client:

src/blueapi/client/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ def from_config(cls, config: ApplicationConfig) -> "BlueapiClient":
5656
... # Swallow exceptions
5757
rest = BlueapiRestClient(config.api, session_manager=session_manager)
5858
if config.stomp.enabled:
59+
assert config.stomp.url.host is not None, "Stomp URL missing host"
60+
assert config.stomp.url.port is not None, "Stomp URL missing port"
5961
client = StompClient.for_broker(
6062
broker=Broker(
61-
host=config.stomp.host,
62-
port=config.stomp.port,
63+
host=config.stomp.url.host,
64+
port=config.stomp.url.port,
6365
auth=config.stomp.auth,
6466
)
6567
)

src/blueapi/client/rest.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _request_and_deserialize(
248248
get_exception: Callable[[requests.Response], Exception | None] = _exception,
249249
params: Mapping[str, Any] | None = None,
250250
) -> T:
251-
url = self._url(suffix)
251+
url = self._config.url.unicode_string().removesuffix("/") + suffix
252252
# Get the trace context to propagate to the REST API
253253
carr = get_context_propagator()
254254
response = requests.request(
@@ -266,7 +266,3 @@ def _request_and_deserialize(
266266
raise NoContent(target_type)
267267
deserialized = TypeAdapter(target_type).validate_python(response.json())
268268
return deserialized
269-
270-
def _url(self, suffix: str) -> str:
271-
base_url = f"{self._config.protocol}://{self._config.host}:{self._config.port}"
272-
return f"{base_url}{suffix}"

src/blueapi/config.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pydantic import (
1212
BaseModel,
1313
Field,
14+
HttpUrl,
1415
TypeAdapter,
1516
ValidationError,
1617
field_validator,
@@ -44,8 +45,7 @@ class StompConfig(BlueapiBaseModel):
4445
"event publishing",
4546
default=False,
4647
)
47-
host: str = Field(description="STOMP broker host", default="localhost")
48-
port: int = Field(description="STOMP broker port", default=61613)
48+
url: HttpUrl = HttpUrl("http://localhost:61613")
4949
auth: BasicAuthentication | None = Field(
5050
description="Auth information for communicating with STOMP broker, if required",
5151
default=None,
@@ -84,8 +84,7 @@ class EnvironmentConfig(BlueapiBaseModel):
8484

8585
class GraylogConfig(BlueapiBaseModel):
8686
enabled: bool = False
87-
host: str = "localhost"
88-
port: int = 5555
87+
url: HttpUrl = HttpUrl("http://localhost:5555")
8988

9089

9190
class LoggingConfig(BlueapiBaseModel):
@@ -101,9 +100,7 @@ class CORSConfig(BlueapiBaseModel):
101100

102101

103102
class RestConfig(BlueapiBaseModel):
104-
host: str = "localhost"
105-
port: int = 8000
106-
protocol: str = "http"
103+
url: HttpUrl = HttpUrl("http://localhost:8000")
107104
cors: CORSConfig | None = None
108105

109106

src/blueapi/log.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ def set_up_graylog_handler(
7373
logger: Logger to attach handler to
7474
logging_config: LoggingConfig
7575
"""
76+
assert logging_config.graylog.url.host is not None, "Graylog URL missing host"
77+
assert logging_config.graylog.url.port is not None, "Graylog URL missing port"
7678
graylog_handler = GELFTCPHandler(
77-
logging_config.graylog.host, logging_config.graylog.port
79+
logging_config.graylog.url.host,
80+
logging_config.graylog.url.port,
7881
)
7982
graylog_handler.setLevel(logging_config.level)
8083

src/blueapi/service/interface.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ def worker() -> TaskWorker:
6969
def stomp_client() -> StompClient | None:
7070
stomp_config: StompConfig = config().stomp
7171
if stomp_config.enabled:
72+
assert stomp_config.url.host is not None, "Stomp URL missing host"
73+
assert stomp_config.url.port is not None, "Stomp URL missing port"
7274
client = StompClient.for_broker(
7375
broker=Broker(
74-
host=stomp_config.host,
75-
port=stomp_config.port,
76-
auth=stomp_config.auth, # type: ignore
76+
host=stomp_config.url.host,
77+
port=stomp_config.url.port,
78+
auth=stomp_config.auth,
7779
)
7880
)
7981

src/blueapi/service/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,9 @@ def start(config: ApplicationConfig):
509509
http_capture_headers_server_response=[",*"],
510510
)
511511
app.state.config = config
512-
513-
uvicorn.run(app, host=config.api.host, port=config.api.port)
512+
assert config.api.url.host is not None, "API URL missing host"
513+
assert config.api.url.port is not None, "API URL missing port"
514+
uvicorn.run(app, host=config.api.url.host, port=config.api.url.port)
514515

515516

516517
async def add_api_version_header(
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
api:
2-
host: a.fake.host
3-
port: 12345
2+
url: http://a.fake.host:12345
43
stomp:
54
enabled: true
6-
host: localhost
7-
port: 61613
5+
url: https://localhost:61613
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
api:
2-
host: a.fake.host
3-
port: 12345
2+
url: http://a.fake.host:12345

0 commit comments

Comments
 (0)