Skip to content

Commit 9e7ef0a

Browse files
committed
adds CorsHeaders to allow usage from javascript
1 parent fdacbb9 commit 9e7ef0a

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

api/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,10 @@
5656
<artifactId>undertow-core</artifactId>
5757
<version>2.0.27.Final</version>
5858
</dependency>
59+
<dependency>
60+
<groupId>com.stijndewitt.undertow.cors</groupId>
61+
<artifactId>undertow-cors-filter</artifactId>
62+
<version>0.4.0</version>
63+
</dependency>
5964
</dependencies>
6065
</project>

api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package xyz.gianlu.librespot.api;
22

3+
import com.stijndewitt.undertow.cors.AllowAll;
4+
import com.stijndewitt.undertow.cors.Filter;
35
import io.undertow.Undertow;
46
import io.undertow.server.RoutingHandler;
57
import org.apache.log4j.Logger;
@@ -32,7 +34,12 @@ public void start(@NotNull Session session) {
3234
RoutingHandler handler = new RoutingHandler();
3335
prepareHandlers(handler, session);
3436

35-
undertow = Undertow.builder().addHttpListener(port, "", handler).build();
37+
Filter corsFilter = new Filter(handler);
38+
corsFilter.setPolicyClass(AllowAll.class.getCanonicalName());
39+
corsFilter.setPolicyParam(null);
40+
corsFilter.setUrlPattern(".*");
41+
42+
undertow = Undertow.builder().addHttpListener(port, "", corsFilter).build();
3643
undertow.start();
3744
LOGGER.info(String.format("Server started on port %d!", port));
3845
}

0 commit comments

Comments
 (0)