-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/mouda/backend/common/HealthCheckController.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 |
---|---|---|
@@ -1,14 +1,38 @@ | ||
package mouda.backend.common; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
@RestController | ||
public class HealthCheckController { | ||
|
||
private static final String HOST_IP = "127.0.0.1"; | ||
private static final String HOST_NAME = "localhost"; | ||
|
||
private final AtomicBoolean isTerminating = new AtomicBoolean(false); | ||
|
||
@GetMapping("/health") | ||
public ResponseEntity<Void> checkHealth() { | ||
if (isTerminating.get()) { | ||
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); | ||
} | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping("/termination") | ||
public ResponseEntity<Void> terminate(HttpServletRequest request) { | ||
String remoteAddr = request.getRemoteAddr(); | ||
|
||
if (HOST_IP.equals(remoteAddr) || HOST_NAME.equals(remoteAddr)) { | ||
return ResponseEntity.ok().build(); | ||
} | ||
return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); | ||
} | ||
} |