Skip to content

Commit

Permalink
Add cli command to start mch-hub server
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvinn8 committed Oct 17, 2024
1 parent a18474a commit e989652
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ subprojects {

tasks.withType<JavaCompile>() {
options.encoding = "UTF-8"
options.release = 17
}

tasks.withType<Javadoc>() {
Expand Down
1 change: 1 addition & 0 deletions mch-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repositories {

dependencies {
implementation(project(":mch"))
implementation(project(":mch-hub-server"))
implementation("com.google.inject:guice:6.0.0")
implementation("info.picocli:picocli:4.7.4")
annotationProcessor("info.picocli:picocli-codegen:4.7.4")
Expand Down
2 changes: 2 additions & 0 deletions mch-cli/src/main/java/ca/bkaw/mch/cli/MchCli.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.bkaw.mch.cli;

import ca.bkaw.mch.cli.ftp.FtpCommand;
import ca.bkaw.mch.cli.hub.HubCommand;
import ca.bkaw.mch.cli.research.ResearchCommand;
import ca.bkaw.mch.cli.sftp.SftpCommand;
import ca.bkaw.mch.cli.world.WorldCommand;
Expand All @@ -11,6 +12,7 @@
CommitCommand.class,
CatCommand.class,
FtpCommand.class,
HubCommand.class,
InitCommand.class,
LogCommand.class,
ResearchCommand.class,
Expand Down
47 changes: 47 additions & 0 deletions mch-cli/src/main/java/ca/bkaw/mch/cli/hub/HubCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ca.bkaw.mch.cli.hub;

import ca.bkaw.mch.hub.MchHub;
import ca.bkaw.mch.hub.MchHubServer;
import picocli.CommandLine.Command;
import picocli.CommandLine.ExitCode;
import picocli.CommandLine.Parameters;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.CountDownLatch;

@Command(name = "hub")
public class HubCommand {
@Command(name = "start")
public int start(
@Parameters(index = "0", defaultValue = "0.0.0.0")
InetAddress host,
@Parameters(index = "1", defaultValue = "4148")
int port
) {
try {
MchHubServer server = MchHub.start(new InetSocketAddress(host, port));
if (server == null) {
return ExitCode.USAGE;
}

CountDownLatch latch = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
System.out.println("\nStopping mch-hub server");
server.stop(0);
} finally {
latch.countDown();
}
}));

latch.await();

return ExitCode.OK;
} catch (Throwable e) {
System.err.println("mch-hub-server crashed");
e.printStackTrace();
return ExitCode.SOFTWARE;
}
}
}
5 changes: 3 additions & 2 deletions mch-hub-server/src/main/java/ca/bkaw/mch/hub/MchHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.nio.file.Path;

public class MchHub {
public static void start(InetSocketAddress addr) throws IOException {
public static MchHubServer start(InetSocketAddress addr) throws IOException {
MchHubServer server = new MchHubServer();

Path configPath = Path.of("mch-hub.toml");
Expand All @@ -20,7 +20,7 @@ public static void start(InetSocketAddress addr) throws IOException {
Config reposConfig = config.get("repos");
if (reposConfig == null) {
System.out.println("mch-hub-server is not configured. Add a repository to mch-hub.toml to get started!");
return;
return null;
}
for (Config.Entry entry : reposConfig.entrySet()) {
String key = entry.getKey();
Expand All @@ -44,6 +44,7 @@ public static void start(InetSocketAddress addr) throws IOException {

server.start(addr);
System.out.println("Starting mch-hub-server on port " + addr.getPort());
return server;
}

public static void main(String[] args) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public void addRepo(String identifier, MchRepository repository) {
this.httpServer.createContext("/repo/" + identifier + "/v1/restoreFile", handler::restoreFile);
this.httpServer.createContext("/repo/" + identifier + "/v1/list", handler::list);
}

public void stop(int delay) {
this.httpServer.stop(delay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static RepoViewerConfig fromConfig(Config config) throws IOException {
}
}
} else {
trackedWorldSha1 = trackedWorlds.getFirst();
trackedWorldSha1 = trackedWorlds.iterator().next();
}
if (trackedWorldSha1 == null) {
throw new RuntimeException("Unable to find tracked world.");
Expand Down

0 comments on commit e989652

Please sign in to comment.