Skip to content

Commit a88d957

Browse files
Make a nicer homescreen with improved design and health page
1 parent 103178a commit a88d957

5 files changed

Lines changed: 144 additions & 6 deletions

File tree

src/main/java/org/example/StaticFileHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class StaticFileHandler {
1313
private byte[] fileBytes;
1414
private int statusCode;
1515

16+
1617
// Constructor for production
1718
public StaticFileHandler() {
1819
WEB_ROOT = "www";
@@ -23,7 +24,12 @@ public StaticFileHandler(String webRoot) {
2324
WEB_ROOT = webRoot;
2425
}
2526

26-
private void handleGetRequest(String uri) throws IOException {
27+
void handleGetRequest(String uri) throws IOException {
28+
29+
if (uri.isEmpty() || "/".equals(uri)) {
30+
uri = "index.html";
31+
}
32+
2733
// Sanitize URI
2834
int q = uri.indexOf('?');
2935
if (q >= 0) uri = uri.substring(0, q);
@@ -66,4 +72,7 @@ public void sendGetRequest(OutputStream outputStream, String uri) throws IOExcep
6672
outputStream.write(response.build());
6773
outputStream.flush();
6874
}
75+
public byte[] getFileBytes() { return fileBytes; }
76+
public int getStatusCode() { return statusCode; }
77+
6978
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.example;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
import com.sun.net.httpserver.HttpHandler;
5+
import java.io.IOException;
6+
7+
public class StaticFileHttpHandler implements HttpHandler {
8+
9+
private final StaticFileHandler handler;
10+
11+
public StaticFileHttpHandler() {
12+
this.handler = new StaticFileHandler();
13+
}
14+
15+
@Override
16+
public void handle(HttpExchange exchange) throws IOException {
17+
byte[] fileBytes;
18+
int statusCode;
19+
String path = exchange.getRequestURI().getPath();
20+
21+
if (path == null || path.isEmpty() || "/".equals(path)) {
22+
path = "index.html";
23+
} else if (path.startsWith("/")) {
24+
path = path.substring(1);
25+
}
26+
27+
if ("GET".equalsIgnoreCase(exchange.getRequestMethod())) {
28+
try {
29+
handler.handleGetRequest(path);
30+
fileBytes = handler.getFileBytes();
31+
statusCode = handler.getStatusCode();
32+
} catch (IOException e) {
33+
fileBytes = "500 Internal Server Error".getBytes();
34+
statusCode = 500;
35+
}
36+
37+
exchange.sendResponseHeaders(statusCode, fileBytes.length);
38+
exchange.getResponseBody().write(fileBytes);
39+
} else {
40+
fileBytes = "405 Method Not Allowed".getBytes();
41+
statusCode = 405;
42+
exchange.sendResponseHeaders(statusCode, fileBytes.length);
43+
exchange.getResponseBody().write(fileBytes);
44+
}
45+
46+
exchange.close();
47+
}
48+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.example;
2+
3+
import com.sun.net.httpserver.HttpServer;
4+
import java.net.InetSocketAddress;
5+
6+
public class TestServer {
7+
public static void main(String[] args) throws Exception {
8+
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
9+
server.createContext("/", new StaticFileHttpHandler());
10+
server.start();
11+
System.out.println("Server started on http://localhost:8080");
12+
}
13+
}

www/health.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Server is healthy! &#128522; </h1>

www/index.html

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,78 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>Welcome</title>
6-
</head>
5+
<title>JUv25 Web Server</title>
6+
<style>
7+
body {
8+
margin: 0;
9+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
10+
background: linear-gradient(135deg, #1e3c72, #2a5298, #6fb1fc);
11+
color: white;
12+
display: flex;
13+
justify-content: center;
14+
align-items: center;
15+
height: 100vh;
16+
text-align: center;
17+
}
18+
19+
.card {
20+
background: rgba(255, 255, 255, 0.1);
21+
padding: 40px;
22+
border-radius: 12px;
23+
backdrop-filter: blur(10px);
24+
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
25+
max-width: 500px;
26+
27+
28+
margin-top: 50px;
29+
}
30+
31+
32+
.card:hover {
33+
transform: translateY(-5px);
34+
box-shadow: 0 20px 50px rgba(0,0,0,0.5);
35+
}
36+
37+
h1 {
38+
margin-bottom: 15px;
39+
font-size: 2.2em;
40+
}
41+
42+
p {
43+
opacity: 0.9;
44+
font-size: 1.1em;
45+
}
46+
47+
.btn {
48+
background: linear-gradient(90deg, #2a5298, #1e3c72);
49+
margin-top: 40px;
50+
color: white;
51+
padding: 12px 25px;
52+
border-radius: 8px;
53+
font-weight: bold;
54+
text-decoration: none;
55+
transition: 0.3s;
56+
}
57+
58+
.btn:hover {
59+
transform: scale(1.05);
60+
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
61+
}
62+
63+
64+
@media (max-width: 500px) {
65+
h1 { font-size: 1.6em; }
66+
p { font-size: 1em; }
67+
.card { padding: 35px 20px; }
68+
}
69+
</style>
70+
</head>
771
<body>
8-
<h1>Website works!</h1>
9-
<p>Greetings from StaticFileHandler.</p>
72+
<div class="card">
73+
<h1>JUv25 Web Server </h1>
74+
<p>Website works! Greetings from StaticFileHandler.</p>
75+
<a href="/health.html" class="btn">Check Server Health </a>
76+
77+
</div>
1078
</body>
1179
</html>
12-

0 commit comments

Comments
 (0)