Skip to content

Commit 82aaad1

Browse files
authored
Split static resources into it's own module (#121)
* split static resources into it's own module * project version * doc * Update README.md
1 parent 71e2e00 commit 82aaad1

File tree

26 files changed

+208
-149
lines changed

26 files changed

+208
-149
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![javadoc](https://javadoc.io/badge2/io.avaje/avaje-jex/javadoc.svg?color=purple)](https://javadoc.io/doc/io.avaje/avaje-jex)
88

99
# Avaje-Jex
10-
Lightweight (~120KB) wrapper over the JDK's built-in [HTTP server](https://docs.oracle.com/en/java/javase/23/docs/api/jdk.httpserver/module-summary.html).
10+
Lightweight (~100KB) wrapper over the JDK's built-in [HTTP server](https://docs.oracle.com/en/java/javase/23/docs/api/jdk.httpserver/module-summary.html).
1111

1212
Features:
1313

@@ -30,11 +30,6 @@ var app = Jex.create()
3030
System.out.println("after request");
3131
})
3232
.error(IllegalStateException.class, (ctx, exception) -> ctx.status(500).text("Handled IllegalStateException|" + exception.getMessage()))
33-
.staticResource(
34-
b ->
35-
b.httpPath("/myResource")
36-
.resource("/public")
37-
.directoryIndex("index.html"))
3833
.port(8080)
3934
.start();
4035
```

avaje-jex-freemarker/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>avaje-jex-parent</artifactId>
66
<groupId>io.avaje</groupId>
7-
<version>3.0-RC7</version>
7+
<version>3.0-RC8</version>
88
</parent>
99

1010
<artifactId>avaje-jex-freemarker</artifactId>
@@ -18,7 +18,7 @@
1818
<dependency>
1919
<groupId>io.avaje</groupId>
2020
<artifactId>avaje-jex</artifactId>
21-
<version>3.0-RC7</version>
21+
<version>${project.version}</version>
2222
<scope>provided</scope>
2323
</dependency>
2424

@@ -39,7 +39,7 @@
3939
<dependency>
4040
<groupId>io.avaje</groupId>
4141
<artifactId>avaje-jex-test</artifactId>
42-
<version>3.0-RC7</version>
42+
<version>${project.version}</version>
4343
<scope>test</scope>
4444
</dependency>
4545

avaje-jex-htmx/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>io.avaje</groupId>
88
<artifactId>avaje-jex-parent</artifactId>
9-
<version>3.0-RC7</version>
9+
<version>3.0-RC8</version>
1010
</parent>
1111

1212
<artifactId>avaje-jex-htmx</artifactId>
@@ -24,7 +24,7 @@
2424
<dependency>
2525
<groupId>io.avaje</groupId>
2626
<artifactId>avaje-jex</artifactId>
27-
<version>3.0-RC7</version>
27+
<version>${project.version}</version>
2828
</dependency>
2929
</dependencies>
3030

avaje-jex-mustache/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>avaje-jex-parent</artifactId>
66
<groupId>io.avaje</groupId>
7-
<version>3.0-RC7</version>
7+
<version>3.0-RC8</version>
88
</parent>
99

1010
<artifactId>avaje-jex-mustache</artifactId>
@@ -18,7 +18,7 @@
1818
<dependency>
1919
<groupId>io.avaje</groupId>
2020
<artifactId>avaje-jex</artifactId>
21-
<version>3.0-RC7</version>
21+
<version>${project.version}</version>
2222
<scope>provided</scope>
2323
</dependency>
2424

@@ -40,7 +40,7 @@
4040
<dependency>
4141
<groupId>io.avaje</groupId>
4242
<artifactId>avaje-jex-test</artifactId>
43-
<version>3.0-RC7</version>
43+
<version>${project.version}</version>
4444
<scope>test</scope>
4545
</dependency>
4646

avaje-jex-static-content/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>io.avaje</groupId>
7+
<artifactId>avaje-jex-parent</artifactId>
8+
<version>3.0-RC8</version>
9+
</parent>
10+
<artifactId>avaje-jex-static-content</artifactId>
11+
<dependencies>
12+
13+
<dependency>
14+
<groupId>io.avaje</groupId>
15+
<artifactId>avaje-jex</artifactId>
16+
<version>${project.version}</version>
17+
</dependency>
18+
19+
<dependency>
20+
<groupId>io.avaje</groupId>
21+
<artifactId>avaje-jex-test</artifactId>
22+
<version>${project.version}</version>
23+
<scope>test</scope>
24+
</dependency>
25+
</dependencies>
26+
27+
</project>

avaje-jex/src/main/java/io/avaje/jex/AbstractStaticHandler.java renamed to avaje-jex-static-content/src/main/java/io/avaje/jex/staticcontent/AbstractStaticHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.jex;
1+
package io.avaje.jex.staticcontent;
22

33
import java.net.FileNameMap;
44
import java.net.URLConnection;
@@ -8,6 +8,8 @@
88

99
import com.sun.net.httpserver.HttpExchange;
1010

11+
import io.avaje.jex.Context;
12+
import io.avaje.jex.ExchangeHandler;
1113
import io.avaje.jex.http.BadRequestException;
1214
import io.avaje.jex.http.NotFoundException;
1315

avaje-jex/src/main/java/io/avaje/jex/ClassResourceLoader.java renamed to avaje-jex-static-content/src/main/java/io/avaje/jex/staticcontent/ClassResourceLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.jex;
1+
package io.avaje.jex.staticcontent;
22

33
import java.io.InputStream;
44
import java.net.URL;

avaje-jex/src/main/java/io/avaje/jex/DefaultResourceLoader.java renamed to avaje-jex-static-content/src/main/java/io/avaje/jex/staticcontent/DefaultResourceLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.avaje.jex;
1+
package io.avaje.jex.staticcontent;
22

33
import java.io.InputStream;
44
import java.net.URL;

avaje-jex/src/main/java/io/avaje/jex/StaticClassResourceHandler.java renamed to avaje-jex-static-content/src/main/java/io/avaje/jex/staticcontent/StaticClassResourceHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package io.avaje.jex;
1+
package io.avaje.jex.staticcontent;
22

33
import java.net.URL;
44
import java.nio.file.Paths;
55
import java.util.Map;
66
import java.util.function.Predicate;
77

8-
final class StaticClassResourceHandler extends AbstractStaticHandler implements ExchangeHandler {
8+
import io.avaje.jex.Context;
9+
10+
final class StaticClassResourceHandler extends AbstractStaticHandler {
911

1012
private final URL indexFile;
1113
private final URL singleFile;
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,56 @@
1-
package io.avaje.jex;
1+
package io.avaje.jex.staticcontent;
22

33
import java.net.URLConnection;
44
import java.util.function.Predicate;
55

6+
import io.avaje.jex.Context;
7+
import io.avaje.jex.Routing.HttpService;
8+
69
/** Builder for a static resource exchange handler. */
7-
public sealed interface StaticContentConfig permits StaticResourceHandlerBuilder {
10+
public sealed interface StaticContentService extends HttpService
11+
permits StaticResourceHandlerBuilder {
812

9-
/** Create and return a new static content configuration. */
10-
static StaticContentConfig create() {
11-
return StaticResourceHandlerBuilder.builder();
13+
/**
14+
* Create and return a new static content configuration.
15+
*
16+
* @param resourceRoot The file to serve, or the directory the files are located in.
17+
*/
18+
static StaticContentService createCP(String resourceRoot) {
19+
return StaticResourceHandlerBuilder.builder(resourceRoot);
1220
}
1321

14-
/** Return a new ExchangeHandler that will serve the resources */
15-
ExchangeHandler createHandler();
16-
1722
/**
18-
* Sets the HTTP path for the static resource handler.
19-
*
20-
* @param path the HTTP path prefix
21-
* @return the updated configuration
23+
* Create and return a new static class path content configuration with the `/public` directory as
24+
* the root.
2225
*/
23-
StaticContentConfig httpPath(String path);
26+
static StaticContentService createCP() {
27+
return StaticResourceHandlerBuilder.builder("/public/");
28+
}
2429

2530
/**
26-
* Gets the current HTTP path.
31+
* Create and return a new static content configuration for a File.
2732
*
28-
* @return the current HTTP path
33+
* @param resourceRoot The path of the file to serve, or the directory the files are located in.
2934
*/
30-
String httpPath();
35+
static StaticContentService createFile(String resourceRoot) {
36+
return StaticResourceHandlerBuilder.builder(resourceRoot).file();
37+
}
3138

3239
/**
33-
* Sets the file to serve, or the folder your files are located in. (default: "/public/")
40+
* Sets the HTTP path for the static resource handler.
3441
*
35-
* @param resource the root directory
42+
* @param path the HTTP path prefix
3643
* @return the updated configuration
3744
*/
38-
StaticContentConfig resource(String resource);
45+
StaticContentService httpPath(String path);
3946

4047
/**
4148
* Sets the index file to be served when a directory is requests.
4249
*
4350
* @param directoryIndex the index file
4451
* @return the updated configuration
4552
*/
46-
StaticContentConfig directoryIndex(String directoryIndex);
53+
StaticContentService directoryIndex(String directoryIndex);
4754

4855
/**
4956
* Sets a custom resource loader for loading class/module path resources. This is normally used
@@ -54,7 +61,7 @@ static StaticContentConfig create() {
5461
* @param resourceLoader the custom resource loader
5562
* @return the updated configuration
5663
*/
57-
StaticContentConfig resourceLoader(ClassResourceLoader resourceLoader);
64+
StaticContentService resourceLoader(ClassResourceLoader resourceLoader);
5865

5966
/**
6067
* Adds a new MIME type mapping to the configuration. (Default: uses {@link
@@ -65,7 +72,7 @@ static StaticContentConfig create() {
6572
* "application/javascript")
6673
* @return the updated configuration
6774
*/
68-
StaticContentConfig putMimeTypeMapping(String ext, String mimeType);
75+
StaticContentService putMimeTypeMapping(String ext, String mimeType);
6976

7077
/**
7178
* Adds a new response header to the configuration.
@@ -74,27 +81,13 @@ static StaticContentConfig create() {
7481
* @param value the header value
7582
* @return the updated configuration
7683
*/
77-
StaticContentConfig putResponseHeader(String key, String value);
84+
StaticContentService putResponseHeader(String key, String value);
7885

7986
/**
8087
* Sets a predicate to filter files based on the request context.
8188
*
8289
* @param skipFilePredicate the predicate to use
8390
* @return the updated configuration
8491
*/
85-
StaticContentConfig skipFilePredicate(Predicate<Context> skipFilePredicate);
86-
87-
/**
88-
* Sets the resource location (CLASSPATH or FILE).
89-
*
90-
* @param location the resource location
91-
* @return the updated configuration
92-
*/
93-
StaticContentConfig location(ResourceLocation location);
94-
95-
/** Resource location */
96-
enum ResourceLocation {
97-
CLASS_PATH,
98-
FILE
99-
}
92+
StaticContentService skipFilePredicate(Predicate<Context> skipFilePredicate);
10093
}

0 commit comments

Comments
 (0)