-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add a simple UI to the WebServer SSE example.
- Update README.md (align with multipart) - Update endpoints and test to add count and delay parameters - Add logging provider and logging.properties
- Loading branch information
1 parent
39db5d7
commit 33f2d78
Showing
8 changed files
with
271 additions
and
102 deletions.
There are no files selected for viewing
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
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
60 changes: 60 additions & 0 deletions
60
examples/webserver/sse/src/main/java/io/helidon/examples/webserver/sse/Main.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.examples.webserver.sse; | ||
|
||
import io.helidon.logging.common.LogConfig; | ||
import io.helidon.webserver.WebServer; | ||
import io.helidon.webserver.http.HttpRules; | ||
import io.helidon.webserver.staticcontent.StaticContentService; | ||
|
||
/** | ||
* This application provides a simple service with a UI to exercise Server Sent Events (SSE). | ||
*/ | ||
class Main { | ||
|
||
private Main() { | ||
} | ||
|
||
/** | ||
* Executes the example. | ||
* | ||
* @param args command line arguments, ignored | ||
*/ | ||
public static void main(String[] args) { | ||
// load logging configuration | ||
LogConfig.configureRuntime(); | ||
|
||
WebServer server = WebServer.builder() | ||
.routing(Main::routing) | ||
.port(8080) | ||
.build() | ||
.start(); | ||
|
||
System.out.println("WEB server is up! http://localhost:" + server.port()); | ||
} | ||
|
||
/** | ||
* Updates the routing rules. | ||
* | ||
* @param rules routing rules | ||
*/ | ||
static void routing(HttpRules rules) { | ||
rules.register("/", StaticContentService.builder("WEB") | ||
.welcomeFileName("index.html") | ||
.build()) | ||
.register("/api", new SseService()); | ||
} | ||
} |
33 changes: 0 additions & 33 deletions
33
examples/webserver/sse/src/main/java/io/helidon/examples/webserver/sse/SseMain.java
This file was deleted.
Oops, something went wrong.
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
107 changes: 107 additions & 0 deletions
107
examples/webserver/sse/src/main/resources/WEB/index.html
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<!DOCTYPE html> | ||
<!-- | ||
Copyright (c) 2024 Oracle and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<html lang="en"> | ||
<meta charset="utf-8"/> | ||
<head> | ||
<title>Helidon Examples WebServer SSE</title> | ||
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> | ||
<style> | ||
.container { | ||
width: 400px; | ||
} | ||
.controls { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
} | ||
.controls input { | ||
flex-grow: 1; | ||
} | ||
.controls input:not(:first-child) { | ||
margin-left: 10px; | ||
} | ||
.events { | ||
overflow-y: auto; | ||
height: 400px; | ||
margin: 10px 0 10px 0; | ||
background-color: #f3f4f9; | ||
color: #2F4F4F; | ||
} | ||
.events span { | ||
display: block; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Text events</h1> | ||
<div id="text_events" class="container"> | ||
<div class="controls"> | ||
<input type="button" value="start" /> | ||
<input type="button" value="stop" disabled/> | ||
</div> | ||
<div class="events"></div> | ||
</div> | ||
<h1>JSON events</h1> | ||
<div id="json_events" class="container"> | ||
<div class="controls"> | ||
<input type="button" value="start" /> | ||
<input type="button" value="stop" disabled/> | ||
</div> | ||
<div class="events"></div> | ||
</div> | ||
<script type="text/javascript"> | ||
$(document).ready(() => { | ||
class Events { | ||
constructor(rootEltId, uri, eventType) { | ||
this.uri = uri; | ||
this.eventSource = null; | ||
this.rootElt = $(rootEltId); | ||
this.eventsElt = this.rootElt.find('.events'); | ||
this.startButton = this.rootElt.find('input[value="start"]'); | ||
this.stopButton = this.rootElt.find('input[value="stop"]'); | ||
this.startButton.click(() => { | ||
this.eventsElt.empty(); | ||
this.eventSource = new EventSource(this.uri); | ||
this.eventSource.addEventListener('error', () => { | ||
// server side stops after count | ||
// prevent restart | ||
this.eventSource.close(); | ||
this.toggle(false); | ||
}); | ||
this.eventSource.addEventListener(eventType, event => { | ||
this.eventsElt.append(`<span>${event.data}</span>`); | ||
}); | ||
this.toggle(true); | ||
}); | ||
this.stopButton.click(() => { | ||
this.eventSource.close(); | ||
this.toggle(false); | ||
}); | ||
} | ||
toggle(active) { | ||
this.startButton[0].disabled = active; | ||
this.stopButton[0].disabled = !active; | ||
} | ||
} | ||
new Events('#text_events', '/api/sse_text?count=30&delay=1', 'my-event'); | ||
new Events('#json_events', '/api/sse_json?count=10&delay=1', 'message'); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
21 changes: 21 additions & 0 deletions
21
examples/webserver/sse/src/main/resources/logging.properties
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# | ||
# Copyright (c) 2018, 2024 Oracle and/or its affiliates. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
handlers=java.util.logging.ConsoleHandler | ||
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS.%1$tL %5$s%6$s%n | ||
# Global logging level. Can be overridden by specific loggers | ||
.level=INFO | ||
io.helidon.webserver.level=INFO |
Oops, something went wrong.