diff --git a/src/main/java/codes/thischwa/jdvs/web/BadgeController.java b/src/main/java/codes/thischwa/jdvs/web/BadgeController.java new file mode 100644 index 0000000..9af5cac --- /dev/null +++ b/src/main/java/codes/thischwa/jdvs/web/BadgeController.java @@ -0,0 +1,34 @@ +package codes.thischwa.jdvs.web; + +import codes.thischwa.jdvs.jpa.GitRepositoryRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.server.ResponseStatusException; + +@Controller +@RequiredArgsConstructor +public class BadgeController { + + private static final String BADGE_URL_PREFIX = + "https://img.shields.io/badge/javadoc%40JdVS-"; + + private static final String BADGE_URL_SUFFIX = "-green"; + + private final GitRepositoryRepository repository; + + @GetMapping("/badge/{repositoryName}") + public String badge(@PathVariable String repositoryName) { + String tag = + repository + .findByName(repositoryName) + .map(r -> r.getLastTag() != null ? r.getLastTag() : "unknown") + .orElseThrow( + () -> + new ResponseStatusException( + HttpStatus.NOT_FOUND, "Repository not found: " + repositoryName)); + return "redirect:" + BADGE_URL_PREFIX + tag + BADGE_URL_SUFFIX; + } +} diff --git a/src/main/java/codes/thischwa/jdvs/web/FaviconController.java b/src/main/java/codes/thischwa/jdvs/web/FaviconController.java new file mode 100644 index 0000000..19ad1b1 --- /dev/null +++ b/src/main/java/codes/thischwa/jdvs/web/FaviconController.java @@ -0,0 +1,16 @@ +package codes.thischwa.jdvs.web; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Controller for favicon.ico, just for reducing 404 errors in the logs. + */ +@RestController +public class FaviconController { + + @GetMapping("favicon.ico") + void returnNoFavicon() { + // see class comment + } +} diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 2a5a1f4..b61f764 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -13,7 +13,7 @@ Name Latest Version Last Update - Action + Javadoc link @@ -22,7 +22,9 @@ - - - Open Javadoc + + javadoc badge + Not yet generated