This commit is contained in:
@@ -1,19 +1,41 @@
|
||||
package com.kaidi.oa.web;
|
||||
|
||||
import com.kaidi.oa.common.ApiResp;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.util.Map;
|
||||
|
||||
/** Liveness probe. */
|
||||
/** Readiness probe used by the installer and online-update rollback gate. */
|
||||
@RestController
|
||||
@RequestMapping("/api/oa")
|
||||
public class HealthController {
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
public HealthController(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@GetMapping("/health")
|
||||
public ApiResp<Map<String, String>> health() {
|
||||
return ApiResp.ok(Map.of("status", "UP"));
|
||||
public ResponseEntity<ApiResp<Map<String, String>>> health() {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet result = statement.executeQuery("SELECT 1")) {
|
||||
if (connection.isValid(2) && result.next() && result.getInt(1) == 1) {
|
||||
return ResponseEntity.ok(ApiResp.ok(Map.of("status", "UP", "database", "UP")));
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// Health responses deliberately avoid exposing connection details.
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
|
||||
.body(new ApiResp<>(50301, "database unavailable", Map.of("status", "DOWN", "database", "DOWN")));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user