Skip to content

Commit

Permalink
add version and revision in the API
Browse files Browse the repository at this point in the history
  • Loading branch information
lfoppiano committed Jan 18, 2023
1 parent 26a151b commit 09b087a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
id 'com.github.kt3k.coveralls' version '2.12.0'
id 'com.github.johnrengelman.shadow' version '7.0.0'
id "de.undercouch.download" version "4.1.1"
id 'com.palantir.git-version' version '0.12.3'
// id 'net.researchgate.release' version '3.0.1'
}

Expand Down Expand Up @@ -203,6 +204,13 @@ ext.getArg = { propName, defaultVal ->
return project.hasProperty(propName) ? project.getProperty(propName) : defaultVal;
}

processResources {
filesMatching(["version.txt", "revision.txt"]) {
expand(project_version: project.property('version'),
project_revision: gitVersion())
}
}

/*** Packaging and distribution ***/

shadowJar {
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/grobid/core/data/ServiceInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.grobid.core.data;


public class ServiceInfo {

private String version;

private String revision;

public ServiceInfo() {
}

public ServiceInfo(String version) {
this.version = version;
}

public ServiceInfo(String version, String revision) {
this(version);
this.revision = revision;
}


public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getRevision() {
return revision;
}

public void setRevision(String revision) {
this.revision = revision;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import org.apache.commons.io.IOUtils;
import org.grobid.core.utilities.GrobidConfig;
import org.grobid.core.utilities.GrobidProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class GrobidQuantitiesConfiguration extends Configuration {

public static final Logger LOGGER = LoggerFactory.getLogger(GrobidQuantitiesConfiguration.class);

private String grobidHome;

@JsonProperty
Expand All @@ -18,6 +27,15 @@ public class GrobidQuantitiesConfiguration extends Configuration {
@JsonProperty
private String corsAllowedHeaders = "X-Requested-With,Content-Type,Accept,Origin";


// Version
private static String VERSION = null;
private static String REVISION = null;
private static final String UNKNOWN_VERSION_STR = "unknown";
private static final String GROBID_VERSION_FILE = "/version.txt";
private static final String GROBID_REVISION_FILE = "/revision.txt";


private List<GrobidConfig.ModelParameters> models = new ArrayList<>();
private int maxParallelRequests;

Expand Down Expand Up @@ -70,6 +88,40 @@ public int getMaxParallelRequests() {
return this.maxParallelRequests;
}

public static String getVersion() {
if (VERSION != null) {
return VERSION;
}
synchronized (GrobidProperties.class) {
if (VERSION == null) {
VERSION = readFromFile(GROBID_VERSION_FILE);
}
}
return VERSION;
}

public static String getRevision() {
if (REVISION != null) {
return REVISION;
}
synchronized (GrobidProperties.class) {
if (REVISION == null) {
REVISION = readFromFile(GROBID_REVISION_FILE);
}
}
return REVISION;
}

private static String readFromFile(String filePath) {
String grobidVersion = UNKNOWN_VERSION_STR;
try (InputStream is = GrobidProperties.class.getResourceAsStream(filePath)) {
grobidVersion = IOUtils.toString(is, StandardCharsets.UTF_8);
} catch (IOException e) {
LOGGER.error("Cannot read the version from resources", e);
}
return grobidVersion;
}

public String getCleanlpModelPath() {
return cleanlpModelPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.grobid.core.data.MeasurementsResponse;
import org.grobid.core.data.ServiceInfo;
import org.grobid.core.data.UnitBlock;
import org.grobid.core.engines.QuantitiesEngine;
import org.grobid.core.engines.QuantityParser;
Expand Down Expand Up @@ -32,10 +33,13 @@ public class AnnotationController {

private QuantitiesEngine engine;

private GrobidQuantitiesConfiguration configuration;

@Inject
public AnnotationController(GrobidQuantitiesConfiguration configuration,
QuantitiesEngine engine) {
this.engine = engine;
this.configuration = configuration;
}

@Path(PATH_IS_ALIVE)
Expand All @@ -59,6 +63,14 @@ public Response isAlive() {
return response;
}


@Path("/version")
@Produces(MediaType.APPLICATION_JSON)
@GET
public ServiceInfo getVersion() {
return new ServiceInfo(this.configuration.getVersion(), this.configuration.getRevision());
}

@Path(PATH_ANNOTATE_QUANTITY_PDF)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/revision.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${project_revision}
1 change: 1 addition & 0 deletions src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${project_version}

0 comments on commit 09b087a

Please sign in to comment.