Skip to content

Commit

Permalink
Switch to use web-dependency-locator, rather than web-bundler
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Apr 30, 2024
1 parent a607f0b commit 7d1c744
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ ObjectStore
*.ipr
*.iws
.idea

# VS Code
.vscode
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ mvn package
```
Run:
```bash
docker run --ulimit memlock=-1:-1 -it --rm=true --memory-swappiness=0 \
docker run --ulimit memlock=-1:-1 -it --rm=true \
--name postgres-quarkus-rest-http-crud \
-e POSTGRES_USER=restcrud \
-e POSTGRES_PASSWORD=restcrud \
-e POSTGRES_DB=rest-crud \
-p 5432:5432 postgres:10.5
java -jar target/todo-backend-1.0-SNAPSHOT-runner.jar
-p 5432:5432 postgres:14
java -jar target/quarkus-app/quarkus-run.jar
```

Then, open: http://localhost:8080/
Expand All @@ -36,13 +36,13 @@ mvn clean package -Pnative
```
Run:
```bash
docker run --ulimit memlock=-1:-1 -it --rm=true --memory-swappiness=0 \
docker run --ulimit memlock=-1:-1 -it --rm=true \
--name postgres-quarkus-rest-http-crud \
-e POSTGRES_USER=restcrud \
-e POSTGRES_PASSWORD=restcrud \
-e POSTGRES_DB=rest-crud \
-p 5432:5432 postgres:10.5
target/todo-backend-*-runner
-p 5432:5432 postgres:14
./target/todo-backend-1.0-SNAPSHOT-runner
```
## Other links

Expand Down
26 changes: 17 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.quarkus.sample</groupId>
<artifactId>todo-backend</artifactId>
Expand All @@ -14,8 +13,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.9.3</quarkus.platform.version>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>999-SNAPSHOT</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.1.2</surefire-plugin.version>
<failsafe-plugin.version>2.22.2</failsafe-plugin.version>
Expand Down Expand Up @@ -73,20 +72,29 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-info</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-websockets</artifactId>
</dependency>

<!-- For the UI -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-web-dependency-locator</artifactId>
</dependency>
<!-- <dependency>
<groupId>io.quarkiverse.web-bundler</groupId>
<artifactId>quarkus-web-bundler</artifactId>
<version>1.4.0</version>
</dependency>
</dependency>-->
<dependency>
<groupId>org.mvnpm.at.mvnpm</groupId>
<artifactId>vaadin-webcomponents</artifactId>
<version>24.3.10</version>
<scope>provided</scope>
<version>24.3.11</version>
<!--<scope>provided</scope>-->
<scope>runtime</scope>
</dependency>

<!-- Testing -->
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/quarkus/sample/TodoResource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.quarkus.sample;

import io.quarkus.sample.audit.AuditType;
import io.quarkus.panache.common.Sort;
import io.vertx.core.eventbus.EventBus;
import jakarta.inject.Inject;

import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
Expand All @@ -22,6 +25,9 @@
@Tag(name = "Todo Resource", description = "All Todo Operations")
public class TodoResource {

@Inject
EventBus bus;

@OPTIONS
@Operation(hidden = true)
public Response opt() {
Expand Down Expand Up @@ -50,6 +56,7 @@ public Todo getOne(@PathParam("id") Long id) {
@Operation(description = "Create a new todo")
public Response create(@Valid Todo item) {
item.persist();
bus.publish(AuditType.TODO_ADDED.name(), item);
return Response.status(Status.CREATED).entity(item).build();
}

Expand All @@ -59,11 +66,18 @@ public Response create(@Valid Todo item) {
@Operation(description = "Update an exiting todo")
public Response update(@Valid Todo todo, @PathParam("id") Long id) {
Todo entity = Todo.findById(id);
if(entity.completed!=todo.completed && todo.completed){
bus.publish(AuditType.TODO_CHECKED.name(), todo);
}else if(entity.completed!=todo.completed && !todo.completed){
bus.publish(AuditType.TODO_UNCHECKED.name(), todo);
}

entity.id = id;
entity.completed = todo.completed;
entity.order = todo.order;
entity.title = todo.title;
entity.url = todo.url;

return Response.ok(entity).build();
}

Expand All @@ -85,6 +99,7 @@ public Response deleteOne(@PathParam("id") Long id) {
throw new WebApplicationException("Todo with id of " + id + " does not exist.", Status.NOT_FOUND);
}
entity.delete();
bus.publish(AuditType.TODO_REMOVED.name(), entity);
return Response.noContent().build();
}

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/io/quarkus/sample/audit/AuditLogEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.sample.audit;

import jakarta.enterprise.inject.spi.CDI;
import jakarta.json.bind.Jsonb;
import jakarta.websocket.DecodeException;
import jakarta.websocket.Decoder;
import jakarta.websocket.EncodeException;
import jakarta.websocket.Encoder;
import jakarta.websocket.EndpointConfig;

public class AuditLogEncoder implements Encoder.Text<AuditLogSocket.AuditLogEntry>, Decoder.Text<AuditLogSocket.AuditLogEntry> {

private final Jsonb jsonb;

public AuditLogEncoder() {
this.jsonb = CDI.current().select(Jsonb.class).get();
}

@Override
public String encode(AuditLogSocket.AuditLogEntry object) throws EncodeException {
return jsonb.toJson(object);
}

@Override
public AuditLogSocket.AuditLogEntry decode(String s) throws DecodeException {
return jsonb.fromJson(s, AuditLogSocket.AuditLogEntry.class);
}

@Override
public boolean willDecode(String s) {
return true;
}

@Override
public void init(EndpointConfig config) {

}

@Override
public void destroy() {

}
}
58 changes: 58 additions & 0 deletions src/main/java/io/quarkus/sample/audit/AuditLogSocket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.quarkus.sample.audit;

import io.quarkus.logging.Log;
import io.quarkus.sample.Todo;
import io.quarkus.vertx.ConsumeEvent;
import io.vertx.core.impl.ConcurrentHashSet;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import java.util.Set;

@ServerEndpoint(value = "/audit", encoders = AuditLogEncoder.class, decoders = AuditLogEncoder.class)
@ApplicationScoped
public class AuditLogSocket {

Set<Session> sessions = new ConcurrentHashSet<>();

public record AuditLogEntry(AuditType type, Todo todo) {
}

@OnOpen
public void onOpen(Session session) {
sessions.add(session);
}

@ConsumeEvent("TODO_ADDED")
public void add(Todo todo) {
log(new AuditLogEntry(AuditType.TODO_ADDED, todo));
}

@ConsumeEvent("TODO_CHECKED")
public void check(Todo todo) {
log(new AuditLogEntry(AuditType.TODO_CHECKED, todo));
}

@ConsumeEvent("TODO_UNCHECKED")
public void uncheck(Todo todo) {
log(new AuditLogEntry(AuditType.TODO_UNCHECKED, todo));
}

@ConsumeEvent("TODO_REMOVED")
public void remove(Todo todo) {
log(new AuditLogEntry(AuditType.TODO_REMOVED, todo));
}

private void log(AuditLogEntry entry){
sessions.forEach(s -> {
s.getAsyncRemote().sendObject(entry, result -> {
if (result.getException() != null) {
Log.error("Unable to send message: " + result.getException());
}
});
});

}

}
5 changes: 5 additions & 0 deletions src/main/java/io/quarkus/sample/audit/AuditType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkus.sample.audit;

public enum AuditType {
TODO_ADDED, TODO_CHECKED, TODO_UNCHECKED, TODO_REMOVED
}
6 changes: 6 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ quarkus.smallrye-openapi.info-contact-url=http://todos.com/contact
quarkus.smallrye-openapi.info-license-name=Apache 2.0
quarkus.smallrye-openapi.info-license-url=https://www.apache.org/licenses/LICENSE-2.0.html
quarkus.swagger-ui.always-include=true

# DB (Prod mode)
%prod.quarkus.datasource.db-kind=postgresql
%prod.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/rest-crud?loggerLevel=OFF
%prod.quarkus.datasource.password=restcrud
%prod.quarkus.datasource.username=restcrud
1 change: 1 addition & 0 deletions src/main/resources/web/app/todos-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class TodosApp extends LitElement {
display: flex;
flex-direction: column;
justify-content: flex-start;
height: 100%;
}
`;

Expand Down
80 changes: 80 additions & 0 deletions src/main/resources/web/app/todos-audit-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {LitElement, html, css} from 'lit';
import '@vaadin/grid';
import { columnBodyRenderer } from '@vaadin/grid/lit.js';

class TodosAuditLog extends LitElement {

static webSocket;
static serverUri;

static styles = css`
`;

static properties = {
_entries: {type: Array, state: true}
};

constructor() {
super();
this._entries = [];
if (!TodosAuditLog.logWebSocket) {
if (window.location.protocol === "https:") {
TodosAuditLog.serverUri = "wss:";
} else {
TodosAuditLog.serverUri = "ws:";
}
TodosAuditLog.serverUri += "//" + window.location.host + "/audit";
TodosAuditLog.connect();
}
this._eventAuditEntry = (event) => this._receiveAuditEntry(event.detail);
}

connectedCallback() {
super.connectedCallback();
document.addEventListener('eventAuditEntryEvent', this._eventAuditEntry, false);
}

disconnectedCallback() {
document.removeEventListener('eventAuditEntryEvent', this._eventAuditEntry, false);
super.disconnectedCallback();
}

render() {
return html`<vaadin-grid .items="${this._entries}">
<vaadin-grid-column header="Action" ${columnBodyRenderer(this._typeRenderer, [])}></vaadin-grid-column>
<vaadin-grid-column path="todo.id"></vaadin-grid-column>
<vaadin-grid-column path="todo.title"></vaadin-grid-column>
<vaadin-grid-column path="todo.completed"></vaadin-grid-column>
</vaadin-grid>`;
}

_typeRenderer(entry) {
return html`${this._formatTodoType(entry.type)}`;
}

_formatTodoType(str) {
return str.replace(/^TODO_(.*)$/, function(match, p1) {
return p1.toLowerCase();
});
}

_receiveAuditEntry(entry) {
this._entries = [entry, ...this._entries];
}

static connect() {
TodosAuditLog.webSocket = new WebSocket(TodosAuditLog.serverUri);
TodosAuditLog.webSocket.onmessage = function (event) {
var auditentry = JSON.parse(event.data);
const eventAuditEntryEvent = new CustomEvent('eventAuditEntryEvent', {detail: auditentry});
document.dispatchEvent(eventAuditEntryEvent);
}
TodosAuditLog.webSocket.onclose = function (event) {
setTimeout(function () {
TodosAuditLog.connect();
}, 100);
};
}

}
customElements.define('todos-audit-log', TodosAuditLog);
Loading

0 comments on commit 7d1c744

Please sign in to comment.