Add REST endpoints: / and /api/health with MySQL and Redis status
This commit is contained in:
20
src/main/java/com/vrobot/api/config/RedisConfig.java
Normal file
20
src/main/java/com/vrobot/api/config/RedisConfig.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.vrobot.api.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RedisConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||||
|
RedisTemplate<String, String> template = new RedisTemplate<>();
|
||||||
|
template.setConnectionFactory(connectionFactory);
|
||||||
|
template.setKeySerializer(new StringRedisSerializer());
|
||||||
|
template.setValueSerializer(new StringRedisSerializer());
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package com.vrobot.api.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class HealthController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public Map<String, Object> home() {
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
response.put("service", "Spring Boot API");
|
||||||
|
response.put("status", "running");
|
||||||
|
response.put("timestamp", LocalDateTime.now());
|
||||||
|
response.put("endpoints", Map.of(
|
||||||
|
"health", "/api/health",
|
||||||
|
"actuator", "/actuator/health"
|
||||||
|
));
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/api/health")
|
||||||
|
public Map<String, Object> health() {
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
|
||||||
|
boolean mysqlConnected = false;
|
||||||
|
try {
|
||||||
|
entityManager.createNativeQuery("SELECT 1").getSingleResult();
|
||||||
|
mysqlConnected = true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean redisConnected = false;
|
||||||
|
try {
|
||||||
|
redisTemplate.opsForValue().set("health-check", LocalDateTime.now().toString());
|
||||||
|
String value = redisTemplate.opsForValue().get("health-check");
|
||||||
|
redisConnected = value != null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
response.put("status", mysqlConnected && redisConnected ? "UP" : "DOWN");
|
||||||
|
response.put("timestamp", LocalDateTime.now());
|
||||||
|
response.put("mysql", mysqlConnected ? "connected" : "disconnected");
|
||||||
|
response.put("redis", redisConnected ? "connected" : "disconnected");
|
||||||
|
response.put("message", "Spring Boot API for vrobot.shop");
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user