Skip to content

Commit 9286d69

Browse files
committed
Bug Fix: Static Files are now being properly served in Production.
1 parent 5999734 commit 9286d69

13 files changed

Lines changed: 159 additions & 18 deletions

File tree

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM ubuntu:latest
2+
LABEL authors="safi-io"
3+
4+
ENTRYPOINT ["top", "-b"]

dependency-reduced-pom.xml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.safi</groupId>
5+
<artifactId>mtws</artifactId>
6+
<version>1.0</version>
7+
<build>
8+
<plugins>
9+
<plugin>
10+
<artifactId>maven-surefire-plugin</artifactId>
11+
<version>3.1.2</version>
12+
</plugin>
13+
<plugin>
14+
<artifactId>maven-shade-plugin</artifactId>
15+
<version>3.5.0</version>
16+
<executions>
17+
<execution>
18+
<phase>package</phase>
19+
<goals>
20+
<goal>shade</goal>
21+
</goals>
22+
<configuration>
23+
<transformers>
24+
<transformer>
25+
<mainClass>webserver.Server</mainClass>
26+
</transformer>
27+
</transformers>
28+
</configuration>
29+
</execution>
30+
</executions>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
<dependencies>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<version>4.13.1</version>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.hamcrest</groupId>
43+
<artifactId>hamcrest-core</artifactId>
44+
<version>1.3</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.junit.jupiter</groupId>
49+
<artifactId>junit-jupiter</artifactId>
50+
<version>5.14.0</version>
51+
<scope>test</scope>
52+
<exclusions>
53+
<exclusion>
54+
<artifactId>junit-jupiter-params</artifactId>
55+
<groupId>org.junit.jupiter</groupId>
56+
</exclusion>
57+
</exclusions>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.junit.jupiter</groupId>
61+
<artifactId>junit-jupiter-api</artifactId>
62+
<version>5.14.0</version>
63+
<scope>test</scope>
64+
<exclusions>
65+
<exclusion>
66+
<artifactId>opentest4j</artifactId>
67+
<groupId>org.opentest4j</groupId>
68+
</exclusion>
69+
<exclusion>
70+
<artifactId>junit-platform-commons</artifactId>
71+
<groupId>org.junit.platform</groupId>
72+
</exclusion>
73+
<exclusion>
74+
<artifactId>apiguardian-api</artifactId>
75+
<groupId>org.apiguardian</groupId>
76+
</exclusion>
77+
</exclusions>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.junit.jupiter</groupId>
81+
<artifactId>junit-jupiter-engine</artifactId>
82+
<version>5.14.0</version>
83+
<scope>test</scope>
84+
<exclusions>
85+
<exclusion>
86+
<artifactId>junit-platform-engine</artifactId>
87+
<groupId>org.junit.platform</groupId>
88+
</exclusion>
89+
<exclusion>
90+
<artifactId>apiguardian-api</artifactId>
91+
<groupId>org.apiguardian</groupId>
92+
</exclusion>
93+
</exclusions>
94+
</dependency>
95+
</dependencies>
96+
<properties>
97+
<maven.compiler.target>17</maven.compiler.target>
98+
<maven.compiler.source>17</maven.compiler.source>
99+
<junit.jupiter.version>5.14.0</junit.jupiter.version>
100+
</properties>
101+
</project>

pom.xml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>com.safi</groupId>
8-
<artifactId>multi-threaded-web-server</artifactId>
9-
<version>1.0-SNAPSHOT</version>
8+
<artifactId>mtws</artifactId>
9+
<version>1.0</version>
1010

1111
<properties>
1212
<maven.compiler.source>17</maven.compiler.source>
@@ -59,12 +59,37 @@
5959

6060
<build>
6161
<plugins>
62+
6263
<!-- Surefire for running tests -->
6364
<plugin>
6465
<groupId>org.apache.maven.plugins</groupId>
6566
<artifactId>maven-surefire-plugin</artifactId>
6667
<version>3.1.2</version>
6768
</plugin>
69+
70+
<!-- Create executable fat jar -->
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-shade-plugin</artifactId>
74+
<version>3.5.0</version>
75+
<executions>
76+
<execution>
77+
<phase>package</phase>
78+
<goals>
79+
<goal>shade</goal>
80+
</goals>
81+
<configuration>
82+
<transformers>
83+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
84+
<mainClass>webserver.Server</mainClass>
85+
</transformer>
86+
</transformers>
87+
</configuration>
88+
</execution>
89+
</executions>
90+
</plugin>
91+
6892
</plugins>
6993
</build>
94+
7095
</project>

post_data.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ name = Test
1010
message = testing from mobile.
1111
email = test@mobile.com
1212
----
13+
name = mighty-test
14+
message = yoooo!
15+
email = mighty-test@mail.com
16+
----

src/main/java/webserver/handler/RequestHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public void handle(InputStream in, OutputStream out) throws Exception {
2929
byte[] buffer = new byte[1024];
3030
int byteReads = in.read(buffer);
3131

32+
if (byteReads == -1) {
33+
return;
34+
}
35+
3236
String clientRequestInformation =
3337
new String(buffer, 0, byteReads);
3438

src/main/java/webserver/handler/getrequests/APIHandler.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import webserver.util.HttpResponse;
77

88
import java.io.IOException;
9+
import java.io.InputStream;
910
import java.io.OutputStream;
1011
import java.nio.file.Files;
1112
import java.nio.file.Path;
@@ -25,9 +26,11 @@ public APIHandler(OutputStream out, String path) {
2526
public void handle() {
2627
Supplier<String> handler = EndPointRegistry.routes.get(path);
2728

28-
String responseBody, contentType = "application/json";
29+
String responseBody = "", contentType = "application/json";
2930
int statusCode = 200;
3031

32+
InputStream is = null;
33+
3134
boolean isError = false;
3235

3336
if (handler != null) {
@@ -36,19 +39,12 @@ public void handle() {
3639
isError = true;
3740
statusCode = 404;
3841
contentType = "text/html";
39-
40-
Path errorPath = Paths.get("src/webserver/errors/404.html");
41-
42-
try {
43-
responseBody = Files.readString(errorPath);
44-
} catch (IOException e) {
45-
responseBody = "<h1>404 Not Found</h1>";
46-
}
42+
is = getClass().getResourceAsStream("/errors/404.html");
4743
}
4844

4945
try {
5046
if (isError) {
51-
HttpResponse response = new HttpResponse(out, statusCode, contentType, responseBody.getBytes());
47+
HttpResponse response = new HttpResponse(out, statusCode, contentType, is.readAllBytes());
5248
response.sendResponse();
5349
} else {
5450
JsonObject apiResponse = JsonParser.parseString(responseBody).getAsJsonObject();

src/main/java/webserver/handler/getrequests/StaticFileHandler.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import webserver.util.MimeTypes;
55

66
import java.io.IOException;
7+
import java.io.InputStream;
78
import java.io.OutputStream;
89
import java.nio.file.Files;
910
import java.nio.file.Path;
@@ -20,27 +21,30 @@ public StaticFileHandler(OutputStream out, String path) {
2021
}
2122

2223
public void handle() {
23-
Path filePath = Paths.get("resources" + path);
24+
25+
InputStream isResource = getClass().getResourceAsStream(path);
2426
byte[] body;
2527

2628
int statusCode = 200;
2729
String contentType = "text/plain";
2830

2931
try {
30-
body = Files.readAllBytes(filePath);
32+
body = isResource.readAllBytes();
3133

32-
String fileName = filePath.getFileName().toString();
34+
String fileName = path.substring(path.lastIndexOf("/") + 1);
3335
int dotIndex = fileName.lastIndexOf(".");
3436
if (dotIndex != -1) {
3537
String ext = fileName.substring(dotIndex + 1);
3638
contentType = MimeTypes.MIME_TYPES.get(ext);
3739
}
3840

39-
} catch (IOException e) {
41+
} catch (NullPointerException | IOException e) {
4042
statusCode = 404;
4143

4244
try {
43-
body = Files.readAllBytes(Paths.get("resources/errors/404.html"));
45+
InputStream is = getClass().getResourceAsStream("/errors/404.html");
46+
body = is.readAllBytes();
47+
4448
contentType = "text/html";
4549
} catch (IOException ex) {
4650
body = "<h1>404 Not Found</h1>".getBytes();

src/main/java/webserver/handler/postrequets/PostReqHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import webserver.util.HttpResponse;
66

77
import java.io.IOException;
8+
import java.io.InputStream;
89
import java.io.OutputStream;
910
import java.nio.file.Files;
1011
import java.nio.file.Paths;
@@ -45,7 +46,9 @@ public static void postReqResponse(OutputStream out, String clientRequestInforma
4546
} else {
4647
byte[] body;
4748
try {
48-
body = Files.readAllBytes(Paths.get("resources/errors/404.html"));
49+
InputStream is = PostReqHandler.class.getResourceAsStream("/errors/404.html");
50+
body = is.readAllBytes();
51+
4952
HttpResponse response = new HttpResponse(out, 404, "text/html", body);
5053
response.sendResponse();
5154
} catch (IOException e) {

0 commit comments

Comments
 (0)