Skip to content

Commit 41641c0

Browse files
committed
Add Hilla Chat App
1 parent a264040 commit 41641c0

31 files changed

+14212
-0
lines changed

hilla-chat-app/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/target/
2+
.idea/
3+
.vscode/
4+
.settings
5+
.project
6+
.classpath
7+
8+
*.iml
9+
.DS_Store
10+
11+
# The following files are generated/updated by vaadin-maven-plugin
12+
node_modules/
13+
frontend/generated/
14+
pnpmfile.js
15+
vite.generated.ts
16+
17+
# Browser drivers for local integration tests
18+
drivers/
19+
# Error screenshots generated by TestBench for failed integration tests
20+
error-screenshots/
21+
webpack.generated.js
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2007-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import java.io.*;
17+
import java.net.*;
18+
import java.nio.channels.*;
19+
import java.util.Properties;
20+
21+
public class MavenWrapperDownloader {
22+
23+
private static final String WRAPPER_VERSION = "0.5.6";
24+
/**
25+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is
26+
* provided.
27+
*/
28+
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
29+
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
30+
31+
/**
32+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl
33+
* property to use instead of the default one.
34+
*/
35+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH = ".mvn/wrapper/maven-wrapper.properties";
36+
37+
/**
38+
* Path where the maven-wrapper.jar will be saved to.
39+
*/
40+
private static final String MAVEN_WRAPPER_JAR_PATH = ".mvn/wrapper/maven-wrapper.jar";
41+
42+
/**
43+
* Name of the property which should be used to override the default download
44+
* url for the wrapper.
45+
*/
46+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
47+
48+
public static void main(String args[]) {
49+
System.out.println("- Downloader started");
50+
File baseDirectory = new File(args[0]);
51+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
52+
53+
// If the maven-wrapper.properties exists, read it and check if it contains a
54+
// custom
55+
// wrapperUrl parameter.
56+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
57+
String url = DEFAULT_DOWNLOAD_URL;
58+
if (mavenWrapperPropertyFile.exists()) {
59+
FileInputStream mavenWrapperPropertyFileInputStream = null;
60+
try {
61+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
62+
Properties mavenWrapperProperties = new Properties();
63+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
64+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
65+
} catch (IOException e) {
66+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
67+
} finally {
68+
try {
69+
if (mavenWrapperPropertyFileInputStream != null) {
70+
mavenWrapperPropertyFileInputStream.close();
71+
}
72+
} catch (IOException e) {
73+
// Ignore ...
74+
}
75+
}
76+
}
77+
System.out.println("- Downloading from: " + url);
78+
79+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
80+
if (!outputFile.getParentFile().exists()) {
81+
if (!outputFile.getParentFile().mkdirs()) {
82+
System.out.println(
83+
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
84+
}
85+
}
86+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
87+
try {
88+
downloadFileFromURL(url, outputFile);
89+
System.out.println("Done");
90+
System.exit(0);
91+
} catch (Throwable e) {
92+
System.out.println("- Error downloading");
93+
e.printStackTrace();
94+
System.exit(1);
95+
}
96+
}
97+
98+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
99+
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
100+
String username = System.getenv("MVNW_USERNAME");
101+
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
102+
Authenticator.setDefault(new Authenticator() {
103+
@Override
104+
protected PasswordAuthentication getPasswordAuthentication() {
105+
return new PasswordAuthentication(username, password);
106+
}
107+
});
108+
}
109+
URL website = new URL(urlString);
110+
ReadableByteChannel rbc;
111+
rbc = Channels.newChannel(website.openStream());
112+
FileOutputStream fos = new FileOutputStream(destination);
113+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
114+
fos.close();
115+
rbc.close();
116+
}
117+
118+
}
49.5 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

hilla-chat-app/.npmrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# NOTICE: this is an auto-generated file
3+
#
4+
# This file sets the default parameters for manual `pnpm install`.
5+
#
6+
shamefully-hoist=true

hilla-chat-app/.prettierrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
singleQuote: true,
3+
printWidth: 80,
4+
bracketSameLine: true,
5+
};

hilla-chat-app/LICENSE.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org>

hilla-chat-app/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Reactive Spring chat web app demo
2+
3+
This demo app shows how to build a simple full-stack reactive chat app using Hilla, Spring Boot, Project Reactor and Lit.
4+
The backend is in Java, the frontend in TypeScript.
5+
6+
Technologies used:
7+
8+
- [Hilla](https://hilla.dev)
9+
-- Backend [Spring Boot](https://spring.io/projects/spring-boot) & [Project Reactor](https://projectreactor.io/)
10+
-- Frontend [Lit](https://lit.dev)
11+
12+
## YouTube Tutorial
13+
14+
https://youtu.be/WqTE94FmPu0
15+
16+
## Requirements
17+
18+
- Java 11+
19+
20+
## How to run the app
21+
22+
You can run the app with the included Maven wrapper:
23+
24+
```
25+
./mvnw
26+
```

hilla-chat-app/frontend/index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>hilla-chat</title>
7+
<style>
8+
body {
9+
margin: 0;
10+
width: 100vw;
11+
height: 100vh;
12+
}
13+
14+
#outlet {
15+
height: 100%;
16+
}
17+
</style>
18+
<!-- index.ts is included here automatically (either by the dev server or during the build) -->
19+
</head>
20+
21+
<body>
22+
<div id="outlet"></div>
23+
</body>
24+
</html>

hilla-chat-app/frontend/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Router } from '@vaadin/router';
2+
import { routes } from './routes';
3+
import { appStore } from './stores/app-store';
4+
5+
export const router = new Router(document.querySelector('#outlet'));
6+
7+
router.setRoutes(routes);
8+
9+
window.addEventListener('vaadin-router-location-changed', (e) => {
10+
appStore.setLocation((e as CustomEvent).detail.location);
11+
const title = appStore.currentViewTitle;
12+
if (title) {
13+
document.title = title + ' | ' + appStore.applicationName;
14+
} else {
15+
document.title = appStore.applicationName;
16+
}
17+
});

hilla-chat-app/frontend/routes.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Route } from '@vaadin/router';
2+
import './views/empty/empty-view';
3+
4+
export type ViewRoute = Route & {
5+
title?: string;
6+
icon?: string;
7+
children?: ViewRoute[];
8+
};
9+
10+
export const views: ViewRoute[] = [
11+
// place routes below (more info https://hilla.dev/docs/routing)
12+
{
13+
path: '',
14+
component: 'empty-view',
15+
icon: 'la la-file',
16+
title: 'Empty',
17+
},
18+
];
19+
export const routes: ViewRoute[] = [...views];
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { RouterLocation } from '@vaadin/router';
2+
import { makeAutoObservable } from 'mobx';
3+
4+
export class AppStore {
5+
applicationName = 'hilla-chat';
6+
7+
// The location, relative to the base path, e.g. "hello" when viewing "/hello"
8+
location = '';
9+
10+
currentViewTitle = '';
11+
12+
constructor() {
13+
makeAutoObservable(this);
14+
}
15+
16+
setLocation(location: RouterLocation) {
17+
const serverSideRoute = location.route?.path == '(.*)';
18+
if (location.route && !serverSideRoute) {
19+
this.location = location.route.path;
20+
} else if (location.pathname.startsWith(location.baseUrl)) {
21+
this.location = location.pathname.substr(location.baseUrl.length);
22+
} else {
23+
this.location = location.pathname;
24+
}
25+
if (serverSideRoute) {
26+
this.currentViewTitle = document.title; // Title set by server
27+
} else {
28+
this.currentViewTitle = (location?.route as any)?.title || '';
29+
}
30+
}
31+
}
32+
export const appStore = new AppStore();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import url('line-awesome/dist/line-awesome/css/line-awesome.min.css');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"lumoImports" : [ "typography", "color", "spacing", "badge", "utility" ]
3+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { html } from 'lit';
2+
import { customElement, state } from 'lit/decorators.js';
3+
import { View } from '../../views/view';
4+
import '@vaadin/vaadin-messages';
5+
import '@vaadin/vaadin-text-field';
6+
import Message from 'Frontend/generated/com/example/application/ChatEndpoint/Message';
7+
import { ChatEndpoint } from 'Frontend/generated/endpoints';
8+
import { TextFieldChangeEvent } from '@vaadin/vaadin-text-field';
9+
10+
@customElement('empty-view')
11+
export class EmptyView extends View {
12+
@state() messages: Message[] = [];
13+
@state() userName = '';
14+
15+
get formattedMessages() {
16+
return this.messages.map((m) => ({
17+
...m,
18+
time: new Date(m.time).toLocaleTimeString('en-US'),
19+
}));
20+
}
21+
22+
render() {
23+
return html`
24+
<vaadin-message-list
25+
class="flex-grow"
26+
.items=${this.formattedMessages}></vaadin-message-list>
27+
<div class="flex p-s gap-s items-baseline">
28+
<vaadin-text-field
29+
placeholder="Name"
30+
@change=${this.userNameChange}></vaadin-text-field>
31+
<vaadin-message-input
32+
class="flex-grow"
33+
@submit=${this.submit}></vaadin-message-input>
34+
</div>
35+
`;
36+
}
37+
38+
userNameChange(e: TextFieldChangeEvent) {
39+
this.userName = e.target.value;
40+
}
41+
42+
submit(e: CustomEvent) {
43+
ChatEndpoint.send({
44+
text: e.detail.value,
45+
userName: this.userName,
46+
});
47+
}
48+
49+
connectedCallback() {
50+
super.connectedCallback();
51+
this.classList.add('flex', 'flex-col', 'h-full', 'box-border');
52+
ChatEndpoint.join().onNext(
53+
(message) => (this.messages = [...this.messages, message])
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)