Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 031eb7c

Browse files
✨ Frontend
1 parent cf13c18 commit 031eb7c

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

‎Server.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import java.io.*;
22
import java.net.*;
3+
import java.nio.file.Files;
4+
import java.nio.file.Paths;
5+
36
import com.sun.net.httpserver.*;
47

58
public class Server {
@@ -8,13 +11,34 @@ public static void main(String[] args) throws Exception {
811

912
var server = HttpServer.create(port, 0);
1013

14+
server.createContext("/", new FrontendHandler());
1115
server.createContext("/randomNumber", new RandomNumberHandler());
1216

1317
server.setExecutor(null);
1418

1519
server.start();
1620
}
1721

22+
static class FrontendHandler implements HttpHandler {
23+
@Override
24+
public void handle(HttpExchange request) throws IOException {
25+
var appPath = Paths.get("app.html");
26+
27+
var app = new String(Files.readAllBytes(appPath));
28+
29+
var appBytesLength = app.getBytes().length;
30+
31+
request.sendResponseHeaders(200, appBytesLength);
32+
33+
var body = request.getResponseBody();
34+
35+
body.write(app.getBytes());
36+
37+
body.close();
38+
39+
}
40+
}
41+
1842
static class RandomNumberHandler implements HttpHandler {
1943
@Override
2044
public void handle(HttpExchange request) throws IOException {

‎app.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Random Number Generator</title>
5+
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<script defer type="module">
8+
import {
9+
html,
10+
render,
11+
} from "https://unpkg.com/htm/preact/index.mjs?module";
12+
import { useState } from "https://unpkg.com/preact/hooks/dist/hooks.module.js?module";
13+
14+
function App() {
15+
const [number, setNumber] = useState(0);
16+
17+
return html`
18+
<div class="container">
19+
<h1>NENSS</h1>
20+
<p>
21+
<button
22+
onClick=${async () => {
23+
const response = await fetch("/randomNumber");
24+
setNumber(await response.json());
25+
}}
26+
>
27+
Get random number
28+
</button>
29+
</p>
30+
<p>${number}</p>
31+
</div>
32+
`;
33+
}
34+
35+
render(html`<${App} />`, document.body);
36+
</script>
37+
38+
<style>
39+
* {
40+
margin: 0;
41+
}
42+
43+
.container {
44+
background-color: lightsalmon;
45+
gap: 10px;
46+
height: 100vh;
47+
48+
display: flex;
49+
flex-direction: column;
50+
justify-content: center;
51+
align-items: center;
52+
}
53+
</style>
54+
</head>
55+
56+
<body></body>
57+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /