-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a way to configure server for a plugin (#883)
Motivation: Currently, there's no way to configure the Armeria server outside of the server module. We can perhaps do this by providing DI, SPI, or adding a method to `Plugin`. Modification: - Add `AllReplicasPlugin` class for configuring Central Dogma server Result: - You can now add configurations to the Central Dogma server for your plugin using `AllReplicasPlugin.init()` method.
- Loading branch information
Showing
8 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
it/server/src/test/java/com/linecorp/centraldogma/it/AllReplicasPluginTest.java
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,39 @@ | ||
/* | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.centraldogma.it; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.linecorp.armeria.common.AggregatedHttpResponse; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension; | ||
|
||
final class AllReplicasPluginTest { | ||
|
||
@RegisterExtension | ||
static final CentralDogmaExtension dogma = new CentralDogmaExtension(); | ||
|
||
@Test | ||
void hello() { | ||
// hello service is registered by TestAllReplicasPlugin. | ||
final AggregatedHttpResponse res = dogma.httpClient().get("/hello").aggregate().join(); | ||
assertThat(res.status()).isSameAs(HttpStatus.OK); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
it/server/src/test/java/com/linecorp/centraldogma/it/TestAllReplicasPlugin.java
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,44 @@ | ||
/* | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.centraldogma.it; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
import com.linecorp.armeria.common.HttpResponse; | ||
import com.linecorp.armeria.common.util.UnmodifiableFuture; | ||
import com.linecorp.centraldogma.server.plugin.AllReplicasPlugin; | ||
import com.linecorp.centraldogma.server.plugin.PluginContext; | ||
import com.linecorp.centraldogma.server.plugin.PluginInitContext; | ||
|
||
public final class TestAllReplicasPlugin extends AllReplicasPlugin { | ||
|
||
@Override | ||
public void init(PluginInitContext pluginInitContext) { | ||
pluginInitContext.serverBuilder() | ||
.service("/hello", (ctx, req) -> HttpResponse.of("Hello, world!")); | ||
} | ||
|
||
@Override | ||
public CompletionStage<Void> start(PluginContext context) { | ||
return UnmodifiableFuture.completedFuture(null); | ||
} | ||
|
||
@Override | ||
public CompletionStage<Void> stop(PluginContext context) { | ||
return UnmodifiableFuture.completedFuture(null); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...erver/src/test/resources/META-INF/services/com.linecorp.centraldogma.server.plugin.Plugin
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 @@ | ||
com.linecorp.centraldogma.it.TestAllReplicasPlugin |
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
32 changes: 32 additions & 0 deletions
32
server/src/main/java/com/linecorp/centraldogma/server/plugin/AllReplicasPlugin.java
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,32 @@ | ||
/* | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.linecorp.centraldogma.server.plugin; | ||
|
||
/** | ||
* A Base class for {@link Plugin} whose {@link #target()} is {@link PluginTarget#ALL_REPLICAS}. | ||
*/ | ||
public abstract class AllReplicasPlugin implements Plugin { | ||
|
||
/** | ||
* Overrides this method to initialize the plugin using the {@link PluginInitContext}. | ||
*/ | ||
public void init(PluginInitContext pluginInitContext) {} | ||
|
||
@Override | ||
public final PluginTarget target() { | ||
return PluginTarget.ALL_REPLICAS; | ||
} | ||
} |
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
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
56 changes: 56 additions & 0 deletions
56
server/src/main/java/com/linecorp/centraldogma/server/plugin/PluginInitContext.java
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,56 @@ | ||
/* | ||
* Copyright 2023 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.centraldogma.server.plugin; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.concurrent.ScheduledExecutorService; | ||
|
||
import com.linecorp.armeria.server.ServerBuilder; | ||
import com.linecorp.centraldogma.server.CentralDogmaConfig; | ||
import com.linecorp.centraldogma.server.command.CommandExecutor; | ||
import com.linecorp.centraldogma.server.storage.project.ProjectManager; | ||
|
||
import io.micrometer.core.instrument.MeterRegistry; | ||
|
||
/** | ||
* A context that is used to pass when calling {@link AllReplicasPlugin#init(PluginInitContext)}. | ||
*/ | ||
public final class PluginInitContext extends PluginContext { | ||
|
||
private final ServerBuilder serverBuilder; | ||
|
||
/** | ||
* Creates a new instance. | ||
*/ | ||
public PluginInitContext(CentralDogmaConfig config, | ||
ProjectManager projectManager, | ||
CommandExecutor commandExecutor, | ||
MeterRegistry meterRegistry, | ||
ScheduledExecutorService purgeWorker, | ||
ServerBuilder serverBuilder) { | ||
super(config, projectManager, commandExecutor, meterRegistry, purgeWorker); | ||
this.serverBuilder = requireNonNull(serverBuilder, "serverBuilder"); | ||
} | ||
|
||
/** | ||
* Returns the {@link ServerBuilder} of the Central Dogma server. | ||
*/ | ||
public ServerBuilder serverBuilder() { | ||
return serverBuilder; | ||
} | ||
} |