-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRestateHttpEndpointBean.java
More file actions
129 lines (112 loc) · 4.12 KB
/
RestateHttpEndpointBean.java
File metadata and controls
129 lines (112 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.springboot;
import dev.restate.sdk.auth.signing.RestateRequestIdentityVerifier;
import dev.restate.sdk.endpoint.Endpoint;
import dev.restate.sdk.http.vertx.HttpEndpointRequestHandler;
import dev.restate.sdk.http.vertx.RestateHttpServer;
import io.vertx.core.http.HttpServer;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;
/**
* Restate HTTP Endpoint serving {@link RestateComponent}.
*
* @see Component
*/
@Component
@EnableConfigurationProperties({RestateHttpServerProperties.class, RestateEndpointProperties.class})
public class RestateHttpEndpointBean implements InitializingBean, SmartLifecycle {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final ApplicationContext applicationContext;
private final RestateEndpointProperties restateEndpointProperties;
private final RestateHttpServerProperties restateHttpServerProperties;
private volatile boolean running;
private HttpServer server;
public RestateHttpEndpointBean(
ApplicationContext applicationContext,
RestateEndpointProperties restateEndpointProperties,
RestateHttpServerProperties restateHttpServerProperties) {
this.applicationContext = applicationContext;
this.restateEndpointProperties = restateEndpointProperties;
this.restateHttpServerProperties = restateHttpServerProperties;
}
@Override
public void afterPropertiesSet() {
Map<String, Object> restateComponents =
applicationContext.getBeansWithAnnotation(RestateComponent.class);
if (restateComponents.isEmpty()) {
logger.info("No @RestateComponent discovered");
// Don't start anything, if no service is registered
return;
}
var builder = Endpoint.builder();
for (Object component : restateComponents.values()) {
builder = builder.bind(component);
}
if (restateEndpointProperties.isEnablePreviewContext()) {
builder = builder.enablePreviewContext();
}
if (restateEndpointProperties.getIdentityKey() != null) {
builder.withRequestIdentityVerifier(
RestateRequestIdentityVerifier.fromKey(restateEndpointProperties.getIdentityKey()));
}
this.server =
RestateHttpServer.fromHandler(
HttpEndpointRequestHandler.fromEndpoint(
builder.build(),
this.restateHttpServerProperties.isDisableBidirectionalStreaming()));
}
@Override
public void start() {
if (this.server != null) {
try {
this.server
.listen(this.restateHttpServerProperties.getPort())
.toCompletionStage()
.toCompletableFuture()
.get();
logger.info("Started Restate Spring HTTP server on port {}", this.server.actualPort());
} catch (Exception e) {
logger.error(
"Error when starting Restate Spring HTTP server on port {}",
this.restateHttpServerProperties.getPort(),
e);
}
this.running = true;
}
}
@Override
public void stop() {
if (this.server != null) {
try {
this.server.close().toCompletionStage().toCompletableFuture().get();
logger.info("Stopped Restate Spring HTTP server");
} catch (Exception e) {
logger.error("Error when stopping the Restate Spring HTTP server", e);
}
this.running = false;
}
}
@Override
public boolean isRunning() {
return this.running;
}
public int actualPort() {
if (this.server == null) {
return -1;
}
return this.server.actualPort();
}
}