Skip to content

Commit 384bc67

Browse files
committed
Refactoring api (removed old, added player endpoints)
1 parent 751f847 commit 384bc67

22 files changed

Lines changed: 301 additions & 1044 deletions

api/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@
4949
<artifactId>librespot-core</artifactId>
5050
<version>${project.version}</version>
5151
</dependency>
52+
53+
<dependency>
54+
<groupId>io.undertow</groupId>
55+
<artifactId>undertow-core</artifactId>
56+
<version>2.0.27.Final</version>
57+
</dependency>
5258
</dependencies>
5359
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package xyz.gianlu.librespot.api;
2+
3+
import io.undertow.Undertow;
4+
import io.undertow.server.RoutingHandler;
5+
import io.undertow.websockets.WebSocketProtocolHandshakeHandler;
6+
import org.apache.log4j.Logger;
7+
import org.jetbrains.annotations.NotNull;
8+
import xyz.gianlu.librespot.api.handlers.PlayerHandler;
9+
import xyz.gianlu.librespot.core.Session;
10+
11+
public class ApiServer {
12+
private static final Logger LOGGER = Logger.getLogger(ApiServer.class);
13+
private final int port;
14+
private Undertow undertow = null;
15+
16+
public ApiServer(int port) {
17+
this.port = port;
18+
19+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
20+
if (undertow != null) undertow.stop();
21+
}));
22+
}
23+
24+
private static void prepareHandlers(@NotNull RoutingHandler root, @NotNull Session session) {
25+
root.post("/player/{cmd}", new PlayerHandler(session))
26+
.get("/events", new WebSocketProtocolHandshakeHandler(new EventsDispatcher(session)));
27+
}
28+
29+
public void start(@NotNull Session session) {
30+
RoutingHandler handler = new RoutingHandler();
31+
prepareHandlers(handler, session);
32+
33+
undertow = Undertow.builder().addHttpListener(port, "", handler).build();
34+
undertow.start();
35+
LOGGER.info(String.format("Server started on port %d!", port));
36+
}
37+
38+
public void stop() {
39+
if (undertow != null) {
40+
undertow.stop();
41+
undertow = null;
42+
}
43+
44+
LOGGER.info("Server stopped!");
45+
}
46+
47+
public void restart(@NotNull Session session) {
48+
stop();
49+
start(session);
50+
}
51+
}

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

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package xyz.gianlu.librespot.api;
2+
3+
import io.undertow.websockets.WebSocketConnectionCallback;
4+
import io.undertow.websockets.core.WebSocketChannel;
5+
import io.undertow.websockets.spi.WebSocketHttpExchange;
6+
import org.jetbrains.annotations.NotNull;
7+
import xyz.gianlu.librespot.core.Session;
8+
9+
public class EventsDispatcher implements WebSocketConnectionCallback {
10+
private final Session session;
11+
12+
public EventsDispatcher(@NotNull Session session) {
13+
this.session = session;
14+
}
15+
16+
@Override
17+
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
18+
// TODO
19+
}
20+
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import xyz.gianlu.librespot.AbsConfiguration;
44
import xyz.gianlu.librespot.FileConfiguration;
5-
import xyz.gianlu.librespot.api.server.ApiServer;
6-
import xyz.gianlu.librespot.api.server.ZeroconfApiServer;
75
import xyz.gianlu.librespot.core.AuthConfiguration;
86
import xyz.gianlu.librespot.core.Session;
97
import xyz.gianlu.librespot.core.ZeroconfServer;
@@ -17,17 +15,14 @@
1715
*/
1816
public class Main {
1917

20-
public static void main(String[] args) throws IOException, GeneralSecurityException, Session.SpotifyAuthenticationException, MercuryClient.MercuryException {
18+
public static void main(String[] args) throws IOException, MercuryClient.MercuryException, GeneralSecurityException, Session.SpotifyAuthenticationException {
19+
ApiServer server = new ApiServer(24879);
20+
2121
AbsConfiguration conf = new FileConfiguration(args);
2222
if (conf.authStrategy() == AuthConfiguration.Strategy.ZEROCONF) {
23-
ZeroconfServer.create(conf).addSessionListener(new ZeroconfApiServer(24879));
23+
ZeroconfServer.create(conf).addSessionListener(server::restart);
2424
} else {
25-
Session session = new Session.Builder(conf).create();
26-
27-
ApiServer server = new ApiServer(24879);
28-
server.registerHandler(new PlayerHandler(session));
29-
server.registerHandler(new MetadataHandler(session));
30-
server.registerHandler(new MercuryHandler(session));
25+
server.start(new Session.Builder(conf).create());
3126
}
3227
}
3328
}

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

Lines changed: 0 additions & 71 deletions
This file was deleted.

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)