Перенос из 3 лабы
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package org.example.controller;
|
||||
|
||||
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.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/health")
|
||||
public class HealthController {
|
||||
|
||||
private final DataSource dataSource;
|
||||
private final AtomicBoolean ready = new AtomicBoolean(true);
|
||||
|
||||
public HealthController(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@GetMapping({"/live"})
|
||||
public ResponseEntity<Map<String, String>> live() {
|
||||
return ResponseEntity.ok(Map.of("status", "ok"));
|
||||
}
|
||||
|
||||
@GetMapping("/ready")
|
||||
public ResponseEntity<Map<String, String>> ready(){
|
||||
if (!ready.get()){
|
||||
return ResponseEntity.status(503).body(Map.of("status", "NOT_READY"));
|
||||
}
|
||||
try (Connection c = dataSource.getConnection()){
|
||||
if (!c.isValid(1)){
|
||||
return ResponseEntity.status(503).body(Map.of("status", "DB_DOWN"));
|
||||
}
|
||||
} catch (Exception e){
|
||||
return ResponseEntity.status(503).body(Map.of("status", "DB_DOWN"));
|
||||
}
|
||||
return ResponseEntity.ok(Map.of("status", "ok"));
|
||||
}
|
||||
|
||||
public void markNotReady() {
|
||||
ready.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user