diff --git a/build.gradle b/build.gradle index 2ac687b..916adec 100644 --- a/build.gradle +++ b/build.gradle @@ -152,6 +152,14 @@ dependencies { testCompile 'com.icegreen:greenmail:1.5.9' } +jar { + manifest { + attributes( + 'Implementation-Version': mxisdVersion() + ) + } +} + shadowJar { baseName = project.name classifier = null diff --git a/src/main/java/io/kamax/mxisd/HttpMxisd.java b/src/main/java/io/kamax/mxisd/HttpMxisd.java index 22683c8..d22ebc4 100644 --- a/src/main/java/io/kamax/mxisd/HttpMxisd.java +++ b/src/main/java/io/kamax/mxisd/HttpMxisd.java @@ -36,10 +36,13 @@ import io.kamax.mxisd.http.undertow.handler.profile.v1.InternalProfileHandler; import io.kamax.mxisd.http.undertow.handler.profile.v1.ProfileHandler; import io.kamax.mxisd.http.undertow.handler.register.v1.Register3pidRequestTokenHandler; import io.kamax.mxisd.http.undertow.handler.status.StatusHandler; +import io.kamax.mxisd.http.undertow.handler.status.VersionHandler; import io.undertow.Handlers; import io.undertow.Undertow; import io.undertow.server.HttpHandler; +import java.util.Objects; + public class HttpMxisd { // Core @@ -67,6 +70,7 @@ public class HttpMxisd { // Status endpoints .get(StatusHandler.Path, SaneHandler.around(new StatusHandler())) + .get(VersionHandler.Path, SaneHandler.around(new VersionHandler())) // Authentication endpoints .get(LoginHandler.Path, SaneHandler.around(new LoginGetHandler(m.getAuth(), m.getHttpClient()))) @@ -119,7 +123,9 @@ public class HttpMxisd { } public void stop() { - httpSrv.stop(); + // Because it might have never been initialized if an exception is thrown early + if (Objects.nonNull(httpSrv)) httpSrv.stop(); + m.stop(); } diff --git a/src/main/java/io/kamax/mxisd/Mxisd.java b/src/main/java/io/kamax/mxisd/Mxisd.java index 7c0f82a..00022c8 100644 --- a/src/main/java/io/kamax/mxisd/Mxisd.java +++ b/src/main/java/io/kamax/mxisd/Mxisd.java @@ -51,6 +51,7 @@ import io.kamax.mxisd.storage.crypto.Ed25519KeyManager; import io.kamax.mxisd.storage.crypto.KeyManager; import io.kamax.mxisd.storage.crypto.SignatureManager; import io.kamax.mxisd.storage.ormlite.OrmLiteSqlStorage; +import org.apache.commons.lang.StringUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; @@ -58,6 +59,8 @@ import java.util.ServiceLoader; public class Mxisd { + public static final String Version = StringUtils.defaultIfBlank(Mxisd.class.getPackage().getImplementationVersion(), "UNKNOWN"); + private MxisdConfig cfg; private CloseableHttpClient httpClient; @@ -86,7 +89,7 @@ public class Mxisd { private void build() { httpClient = HttpClients.custom() - .setUserAgent("mxisd") + .setUserAgent("mxisd/" + Version) .setMaxConnPerRoute(Integer.MAX_VALUE) .setMaxConnTotal(Integer.MAX_VALUE) .build(); diff --git a/src/main/java/io/kamax/mxisd/MxisdStandaloneExec.java b/src/main/java/io/kamax/mxisd/MxisdStandaloneExec.java index e5375df..bf0f7f9 100644 --- a/src/main/java/io/kamax/mxisd/MxisdStandaloneExec.java +++ b/src/main/java/io/kamax/mxisd/MxisdStandaloneExec.java @@ -37,21 +37,33 @@ public class MxisdStandaloneExec { public static void main(String[] args) { try { - log.info("------------- mxisd starting -------------"); MxisdConfig cfg = null; - Iterator argsIt = Arrays.asList(args).iterator(); while (argsIt.hasNext()) { String arg = argsIt.next(); - if (StringUtils.equals("-c", arg)) { + if (StringUtils.equalsAny(arg, "-h", "--help", "-?", "--usage")) { + System.out.println("Available arguments:" + System.lineSeparator()); + System.out.println(" -h, --help Show this help message"); + System.out.println(" --version Print the version then exit"); + System.out.println(" -c, --config Set the configuration file location"); + System.out.println(" "); + System.exit(0); + } else if (StringUtils.equalsAny(arg, "-c", "--config")) { String cfgFile = argsIt.next(); cfg = YamlConfigLoader.loadFromFile(cfgFile); + } else if (StringUtils.equals("--version", arg)) { + System.out.println(Mxisd.Version); + System.exit(0); } else { - log.info("Invalid argument: {}", arg); + System.err.println("Invalid argument: " + arg); + System.err.println("Try '--help' for available arguments"); System.exit(1); } } + log.info("mxisd starting"); + log.info("Version: {}", Mxisd.Version); + if (Objects.isNull(cfg)) { cfg = YamlConfigLoader.tryLoadFromFile("mxisd.yaml").orElseGet(MxisdConfig::new); } @@ -59,11 +71,11 @@ public class MxisdStandaloneExec { HttpMxisd mxisd = new HttpMxisd(cfg); Runtime.getRuntime().addShutdownHook(new Thread(() -> { mxisd.stop(); - log.info("------------- mxisd stopped -------------"); + log.info("mxisd stopped"); })); mxisd.start(); - log.info("------------- mxisd started -------------"); + log.info("mxisd started"); } catch (ConfigurationException e) { log.error(e.getDetailedMessage()); log.error(e.getMessage()); diff --git a/src/main/java/io/kamax/mxisd/http/undertow/handler/status/StatusHandler.java b/src/main/java/io/kamax/mxisd/http/undertow/handler/status/StatusHandler.java index a5dfa0e..4183081 100644 --- a/src/main/java/io/kamax/mxisd/http/undertow/handler/status/StatusHandler.java +++ b/src/main/java/io/kamax/mxisd/http/undertow/handler/status/StatusHandler.java @@ -26,7 +26,7 @@ import io.undertow.server.HttpServerExchange; public class StatusHandler extends BasicHttpHandler { - public static final String Path = "/_matrix/identity/status"; + public static final String Path = "/status"; @Override public void handleRequest(HttpServerExchange exchange) { diff --git a/src/main/java/io/kamax/mxisd/http/undertow/handler/status/VersionHandler.java b/src/main/java/io/kamax/mxisd/http/undertow/handler/status/VersionHandler.java new file mode 100644 index 0000000..5118789 --- /dev/null +++ b/src/main/java/io/kamax/mxisd/http/undertow/handler/status/VersionHandler.java @@ -0,0 +1,48 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2019 Kamax Sarl + * + * https://www.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.http.undertow.handler.status; + +import com.google.gson.JsonObject; +import io.kamax.matrix.json.GsonUtil; +import io.kamax.mxisd.Mxisd; +import io.kamax.mxisd.http.undertow.handler.BasicHttpHandler; +import io.undertow.server.HttpServerExchange; + +public class VersionHandler extends BasicHttpHandler { + + public static final String Path = "/version"; + + private final String body; + + public VersionHandler() { + JsonObject server = new JsonObject(); + server.addProperty("name", "mxisd"); + server.addProperty("version", Mxisd.Version); + + body = GsonUtil.getPrettyForLog(GsonUtil.makeObj("server", server)); + } + + @Override + public void handleRequest(HttpServerExchange exchange) { + respondJson(exchange, body); + } + +}