diff --git a/README.md b/README.md index 5276c8a..1780d6f 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ Steps: - Add a configuration property inside you `application.properties` file: `greeting=World` - Modify `BookmarkResource` to read this configuration property and log it at startup: - Use `@ConfigProperty` to inject the property inside a `greeting` variable. - - Create a `@PostConstruct` method that log it via `log.info("Hello {}", greeting)` using [SLFJ](https://www.slf4j.org/). + - Create a `@PostConstruct` method that log it via `LOGGER.infof("Hello %s", greeting)` using [Jboss logging](https://docs.jboss.org/jbosslogging/latest/org/jboss/logging/Logger.html). - Test using `mvn quarkus:dev` you should see `Hello World` in the console. - Add a new line to `application.properties` file: `%dev.greeting=Dev` this is an override of the same property for the dev profile. - Test using `mvn quarkus:dev` you should see `Hello Dev` in the console. diff --git a/bookmark-message-consumer/pom.xml b/bookmark-message-consumer/pom.xml index c51bd3d..c05b200 100644 --- a/bookmark-message-consumer/pom.xml +++ b/bookmark-message-consumer/pom.xml @@ -9,7 +9,7 @@ 2.22.0 1.8 1.8 - 0.22.0 + 0.28.1 UTF-8 UTF-8 diff --git a/bookmark-service/pom.xml b/bookmark-service/pom.xml index eb4fd31..675c4e6 100644 --- a/bookmark-service/pom.xml +++ b/bookmark-service/pom.xml @@ -9,7 +9,7 @@ 2.22.0 1.8 1.8 - 0.26.1 + 0.28.1 UTF-8 UTF-8 @@ -42,19 +42,22 @@ io.quarkus quarkus-smallrye-health + io.quarkus quarkus-jdbc-postgresql + + io.quarkus quarkus-smallrye-metrics - - - - + + io.quarkus + quarkus-smallrye-reactive-messaging-amqp + @@ -74,6 +77,15 @@ quarkus-junit5 test + + + + + + io.rest-assured + rest-assured + test + io.rest-assured rest-assured diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/AppHealthCheck.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/AppHealthCheck.java new file mode 100644 index 0000000..11cb5bf --- /dev/null +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/AppHealthCheck.java @@ -0,0 +1,14 @@ +package fr.loicmathieu.bookmarkit; + +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; +import org.eclipse.microprofile.health.Readiness; + +@Readiness +public class AppHealthCheck implements HealthCheck { + + @Override + public HealthCheckResponse call() { + return HealthCheckResponse.builder().name("bookmark").withData("is", "always").up().build(); + } +} diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/Bookmark.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/Bookmark.java index 0f70044..5daff8f 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/Bookmark.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/Bookmark.java @@ -1,9 +1,11 @@ package fr.loicmathieu.bookmarkit; +import io.quarkus.hibernate.orm.panache.PanacheEntity; + import javax.persistence.Entity; @Entity -public class Bookmark { +public class Bookmark extends PanacheEntity { public String title; public String url; public String description; diff --git a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java index a232188..8972591 100644 --- a/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java +++ b/bookmark-service/src/main/java/fr/loicmathieu/bookmarkit/BookmarkResource.java @@ -1,7 +1,22 @@ package fr.loicmathieu.bookmarkit; +import io.smallrye.reactive.messaging.annotations.Channel; +import io.smallrye.reactive.messaging.annotations.Emitter; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.microprofile.metrics.annotation.Counted; +import org.eclipse.microprofile.metrics.annotation.Timed; +import org.eclipse.microprofile.openapi.annotations.Operation; +import org.jboss.logging.Logger; + +import javax.annotation.PostConstruct; +import javax.transaction.Transactional; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -12,23 +27,72 @@ @Consumes(MediaType.APPLICATION_JSON) public class BookmarkResource { + private static final Logger LOGGER = Logger.getLogger(BookmarkResource.class); + + @ConfigProperty(name = "greeting") + private String greeting; + + @Channel("bookmarks") + private Emitter emitter; + + @PostConstruct + void init() { + LOGGER.infof("Hello %s", greeting); + } + + @GET + @Operation(summary = "List all bookmarks") + @Counted(name = "listBookmarks.count") + @Timed(name = "listBookmarks.time") public List listBookmarks() { - throw new UnsupportedOperationException("not yet implemented"); + return Bookmark.listAll(); } - public Bookmark getBookmark(Long id) { - throw new UnsupportedOperationException("not yet implemented"); + @GET + @Path("{id}") + @Operation(summary = "Get a bookmark") + @Counted(name = "getBookmark.count") + @Timed(name = "getBookmark.time") + public Bookmark getBookmark(@PathParam("id") Long id) { + return Bookmark.findById(id); } + @POST + @Transactional + @Operation(summary = "Create a bookmark") + @Counted(name = "createBookmark.count") + @Timed(name = "createBookmark.time") public Response createBookmark(Bookmark bookmark) { - throw new UnsupportedOperationException("not yet implemented"); + bookmark.persist(); + this.emitter.send(bookmark); + return Response.status(Response.Status.CREATED).entity(bookmark).build(); } - public Response updateBookmark(Bookmark bookmark, Long id) { - throw new UnsupportedOperationException("not yet implemented"); + @PUT + @Path("{id}") + @Transactional + @Operation(summary = "Update a bookmark") + @Counted(name = "updateBookmark.count") + @Timed(name = "updateBookmark.time") + public void updateBookmark(Bookmark bookmark, @PathParam("id") Long id) { + Bookmark entity = Bookmark.findById(id); + entity.description = bookmark.description; + entity.location = bookmark.location; + entity.title = bookmark.title; + entity.url = bookmark.url; } - public Response deleteBookmark(Long id) { - throw new UnsupportedOperationException("not yet implemented"); + @DELETE + @Path("{id}") + @Transactional + @Operation(summary = "Delete a bookmark") + @Counted(name = "deleteBookmark.count") + @Timed(name = "deleteBookmark.time") + public Response deleteBookmark(@PathParam("id") Long id) { + Bookmark bookmark = Bookmark.findById(id); + if (bookmark != null) { + bookmark.delete(); + } + return Response.noContent().build(); } } diff --git a/bookmark-service/src/main/resources/application.properties b/bookmark-service/src/main/resources/application.properties index 4747645..f8b6963 100644 --- a/bookmark-service/src/main/resources/application.properties +++ b/bookmark-service/src/main/resources/application.properties @@ -1,3 +1,10 @@ +# Configures the AMQP broker credentials. +amqp-username=quarkus +amqp-password=quarkus +# Configure the AMQP connector to write to the `bookmark` address +mp.messaging.outgoing.bookmarks.connector=smallrye-amqp +mp.messaging.outgoing.bookmarks.address=bookmarks +mp.messaging.outgoing.bookmarks.durable=true # Datasource configuration quarkus.datasource.url=jdbc:postgresql://localhost:5432/bookmarkit quarkus.datasource.driver=org.postgresql.Driver @@ -6,3 +13,6 @@ quarkus.datasource.password=quarkus # drop and create the database at startup (use `update` to only update the schema) quarkus.hibernate-orm.database.generation=drop-and-create quarkus.hibernate-orm.sql-load-script=import.sql + +greeting=World +%dev.greeting=Dev diff --git a/geoip-service/pom.xml b/geoip-service/pom.xml index eb6f13f..4a095b9 100644 --- a/geoip-service/pom.xml +++ b/geoip-service/pom.xml @@ -9,7 +9,7 @@ UTF-8 2.22.0 - 0.26.1 + 0.28.1 UTF-8 1.8 1.8