Skip to content

Commit

Permalink
#32 : Started work on restart endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gazbert committed Jun 2, 2019
1 parent eef0123 commit 55e3c67
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 6 deletions.
3 changes: 1 addition & 2 deletions bxbot-core/src/main/java/com/gazbert/bxbot/BXBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.gazbert.bxbot;

import com.gazbert.bxbot.core.engine.TradingEngine;
Expand All @@ -30,7 +29,7 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* BX-bot - here be the main boot app.
* The BX-bot application.
*
* @author gazbert
*/
Expand Down
30 changes: 30 additions & 0 deletions bxbot-rest-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down Expand Up @@ -90,6 +98,28 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.8</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 gazbert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.gazbert.bxbot.rest.api.v1.runtime;

import com.gazbert.bxbot.services.runtime.BotRestartService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static com.gazbert.bxbot.rest.api.v1.runtime.AbstractRuntimeController.RUNTIME_ENDPOINT_BASE_URI;

/**
* Controller for directing Bot restart requests.
*
* @author gazbert
* @since 1.0
*/
@RestController
@RequestMapping(RUNTIME_ENDPOINT_BASE_URI)
public class BotRestartController extends AbstractRuntimeController {

private static final Logger LOG = LogManager.getLogger();
private static final String RESTART_RESOURCE_PATH = "/restart";
private final BotRestartService botRestartService;

@Autowired
public BotRestartController(BotRestartService botRestartService) {
this.botRestartService = botRestartService;
}

/**
* Restarts the bot.
*
* @param user the authenticated user making the request.
* @return 200 OK on success, some other HTTP status code otherwise.
*/
@RequestMapping(value = RESTART_RESOURCE_PATH, method = RequestMethod.POST)
public ResponseEntity<?> restart(@AuthenticationPrincipal User user) {

LOG.info("POST " + RESTART_RESOURCE_PATH + " - restart() - caller: " + user.getUsername());

botRestartService.restart();
return buildResponseEntity(null, HttpStatus.OK);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import static com.gazbert.bxbot.rest.api.v1.runtime.AbstractRuntimeController.RUNTIME_ENDPOINT_BASE_URI;

/**
* Controller for directing Bot Status requests.
* Controller for directing Bot status requests.
*
* @author gazbert
* @since 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
Expand Down Expand Up @@ -73,6 +74,10 @@ public class TestEmailAlertsConfigController extends AbstractConfigControllerTes
@MockBean
private TradingEngine tradingEngine;

// Need this even though not used in the test directly because Spring loads it on startup...
@MockBean
private RestartEndpoint restartEndpoint;

@Before
public void setupBeforeEachTest() {
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
Expand Down Expand Up @@ -77,6 +78,10 @@ public class TestEngineConfigController extends AbstractConfigControllerTest {
@MockBean
private EmailAlerter emailAlerter;

// Need this even though not used in the test directly because Spring loads it on startup...
@MockBean
private RestartEndpoint restartEndpoint;

@Before
public void setupBeforeEachTest() {
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
Expand Down Expand Up @@ -95,6 +96,10 @@ public class TestExchangeConfigController extends AbstractConfigControllerTest {
@MockBean
private EmailAlerter emailAlerter;

// Need this even though not used in the test directly because Spring loads it on startup...
@MockBean
private RestartEndpoint restartEndpoint;

@Before
public void setupBeforeEachTest() {
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
Expand Down Expand Up @@ -89,6 +90,9 @@ public class TestMarketConfigController extends AbstractConfigControllerTest {
@MockBean
private TradingEngine tradingEngine;

// Need this even though not used in the test directly because Spring loads it on startup...
@MockBean
private RestartEndpoint restartEndpoint;

@Before
public void setupBeforeEachTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
Expand Down Expand Up @@ -92,6 +93,10 @@ public class TestStrategyConfigController extends AbstractConfigControllerTest {
@MockBean
private EmailAlerter emailAlerter;

// Need this even though not used in the test directly because Spring loads it on startup...
@MockBean
private RestartEndpoint restartEndpoint;

@Before
public void setupBeforeEachTest() {
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
Expand Down Expand Up @@ -78,6 +79,10 @@ public class TestBotStatusController extends AbstractRuntimeControllerTest {
@MockBean
private EmailAlerter emailAlerter;

// Need this even though not used in the test directly because Spring loads it on startup...
@MockBean
private RestartEndpoint restartEndpoint;

@Before
public void setupBeforeEachTest() {
mockMvc = MockMvcBuilders.webAppContextSetup(ctx).addFilter(springSecurityFilterChain).build();
Expand Down
8 changes: 8 additions & 0 deletions bxbot-services/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.gazbert.bxbot.services.impl;

import com.gazbert.bxbot.domain.engine.EngineConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 gazbert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.gazbert.bxbot.services.runtime;

/**
* The Bot restart service.
*
* @author gazbert
*/
public interface BotRestartService {

void restart();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 gazbert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.gazbert.bxbot.services.runtime.impl;

import com.gazbert.bxbot.services.runtime.BotRestartService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.stereotype.Service;

/**
* Implementation of the Bot restart service.
*
* @author gazbert
*/
@Service("botRestartService")
public class BotRestartServiceImpl implements BotRestartService {

private static final Logger LOG = LogManager.getLogger();

private RestartEndpoint restartEndpoint;

@Autowired
public BotRestartServiceImpl(RestartEndpoint restartEndpoint) {
this.restartEndpoint = restartEndpoint;
}

@Override
public void restart() {
final Object result = restartEndpoint.restart();
LOG.info(() -> "Restart result: " + result);
}
}
7 changes: 5 additions & 2 deletions config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# The port the Spring Boot container will listen on for incoming HTTP requests.
# Defaults to 8080 if not set. Setting it to -1 disables the port.
# REST API not ready for production yet, so port is disabled.
server.port=-1
server.port=8080

# The Spring Boot management port.
# Setting it to -1 disables the port.
Expand All @@ -25,8 +25,11 @@ logging.config=./config/log4j2.xml

# Used to access the bot's REST API.
# REST API not ready for production yet, so security is disabled.
spring.security.user.name=bxbot-ui-server
spring.security.user.name=bxbot-ui
spring.security.user.password=changeme
management.endpoint.restart.enabled=true

# TODO - what do we *really* need enabled?
#management.security.enabled=true
#management.security.roles=ACTUATOR

Expand Down
Loading

0 comments on commit 55e3c67

Please sign in to comment.