Skip to content

Commit

Permalink
Properly re-introduced ui and media serving
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterl committed Dec 4, 2020
1 parent 6ab7103 commit 4a471d1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
59 changes: 27 additions & 32 deletions cineast-api/src/main/java/org/vitrivr/cineast/api/APIEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.javalin.Javalin;
import io.javalin.http.staticfiles.Location;
import io.javalin.plugin.openapi.OpenApiOptions;
import io.javalin.plugin.openapi.OpenApiPlugin;
import io.javalin.plugin.openapi.dsl.OpenApiBuilder;
Expand Down Expand Up @@ -54,6 +55,7 @@
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -131,11 +133,13 @@ public static void stop() {
* @return Full path to the service.
*/
private static String makePath(String name) {
// return String.format("%s/%s", "", name);
return String.format("%s/%s", namespace(), name);
// return String.format("/%s/%s/%s", CONTEXT, VERSION, name);
}


/**
* Returns the namespace of the API. Concatenation of /{@link #CONTEXT}/{@link #VERSION}
* @return The REST API namespace
*/
private static String namespace() {
return String.format("/%s/%s", CONTEXT, VERSION);
}
Expand Down Expand Up @@ -204,6 +208,7 @@ private Javalin dispatchService(boolean secure) {
final int port = this.validateAndNormalizePort(secure, config);

final Javalin service = Javalin.create(serviceConfig -> {
serviceConfig.enableDevLogging();
/* Configure server (TLS, thread pool, etc.) */
serviceConfig.enableCorsForAllOrigins();
serviceConfig.server(() -> {
Expand Down Expand Up @@ -239,6 +244,19 @@ private Javalin dispatchService(boolean secure) {
/* Enable webjars to serve Swagger-UI */
serviceConfig.enableWebjars();
}
/* Serve the UI if requested statically*/
if(config.getServeUI()){
serviceConfig.addStaticFiles(config.getUiLocation(), Location.EXTERNAL);
serviceConfig.addSinglePageRoot("/", config.getUiLocation()+"/index.html", Location.EXTERNAL);
/*Arrays.asList("/gallery",
"/list",
"/mini-gallery",
"/mediaobject",
"/mediaobject/:objectId")
.forEach(url -> {
serviceConfig.addStaticFiles(url, config.getUiLocation(), Location.EXTERNAL);
});*/
}
});

/* Enable WebSocket (if configured). */
Expand All @@ -264,17 +282,7 @@ private Javalin dispatchService(boolean secure) {
});
}

/*
// TODO re-implement this functionality
if(config.getServeUI()){
service.staticFiles.externalLocation(config.getUiLocation());
service.redirect.any("/gallery", "/");
service.redirect.any("/list", "/");
service.redirect.any("/mini-gallery", "/");
service.redirect.any("/mediaobject", "/");
service.redirect.any("/mediaobject/:objectId", "/");

}*/

/* Setup HTTP/RESTful connection (if configured). */
if (config.getEnableRest() || config.getEnableRestSecure()) {
Expand All @@ -288,7 +296,7 @@ private Javalin dispatchService(boolean secure) {
LOGGER.error(ex);
});

/* Some reasonable settings */
/* General settings */
service.config.defaultContentType = "application/json";
service.config.prefer405over404 = true;

Expand Down Expand Up @@ -380,7 +388,11 @@ private void registerServingRoutes(final Javalin service, final APIConfig config
service.get("/objects/:id", new ResolvedContentRoute(
new FileSystemObjectResolver(
new File(Config.sharedConfig().getApi().getObjectLocation()),
new MediaObjectReader(Config.sharedConfig().getDatabase().getSelectorSupplier().get()))));
new MediaObjectReader(Config.sharedConfig().getDatabase().getSelectorSupplier().get()),
((baseDir, object) -> {
String ext = object.getPath().substring(object.getPath().lastIndexOf('.'));
return new File(baseDir, object.getObjectId()+ext);
}))));
}
}

Expand Down Expand Up @@ -410,23 +422,6 @@ private OpenApiOptions getJavalinOpenApiOptions(APIConfig config) {
.modelConverterFactory(() -> new ModelResolver(mapper))
.swagger(new SwaggerOptions("/swagger-ui").title("Swagger UI for Cineast Documentation"))
.reDoc(new ReDocOptions("/redoc").title("ReDoc for Cineast Documentation"));

// return new OpenApiOptions(() -> this.getOpenApi(config))
// .activateAnnotationScanningFor("org.vitrivr.cineast.api.rest")
// .modelConverterFactory(() -> new ModelResolver(mapper) /*Default JavalinModelResolver breaks with Jackson JSON objects*/)
// .jacksonMapper(mapper)
// .toJsonMapper(o -> {
// try {
// return mapper.writeValueAsString(o);
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// }
// })
// .path("/openapi-specs")
// .swagger(
// new SwaggerOptions("/swagger-ui")
// .title("Cineast Swagger Documentation")
// );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ public class FileSystemObjectResolver implements ObjectResolver {
private final MediaObjectReader lookup;
private final File baseDir;

private final ObjectToFileResolver object2File;

public FileSystemObjectResolver(File basedir, MediaObjectReader lookup){
this.lookup = lookup;
this.baseDir = basedir;
object2File = (dir, obj) -> new File(dir, obj.getPath());
}

public FileSystemObjectResolver(File basedir, MediaObjectReader lookup, ObjectToFileResolver transform){
this.lookup = lookup;
this.baseDir = basedir;
object2File = transform;
}


Expand All @@ -32,7 +41,7 @@ public ResolutionResult resolve(String id) {
}

try{
return new ResolutionResult(new File(baseDir, descriptor.getPath()));
return new ResolutionResult(object2File.resolve(baseDir, descriptor));
}catch (IOException e){
e.printStackTrace();
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.vitrivr.cineast.api.rest.resolvers;

import org.vitrivr.cineast.core.data.entities.MediaObjectDescriptor;

import java.io.File;

/**
* Functional interface to map {@link MediaObjectDescriptor} to a filename.
*/
@FunctionalInterface
public interface ObjectToFileResolver {

File resolve(File baseDir, MediaObjectDescriptor object);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class CLI {

private static final String PROMPT = "cineast> ";

private static final String LOGO = "################################################################################\u0085#                                                                              #\u0085#                @@@                           @@@                             #\u0085#                @@@                           @@@                             #\u0085#                     @@@@                                                     #\u0085#   @@@     @@@  @@@  @@@@@@@         @@@@@    @@@  &@@     @@@     @@@@@      #\u0085#   @@@@( (@@@@  @@@( @@@@@@@      %@@@@@@@@@ ,@@@( @@@@( (@@@@  @@@@@@@@@@    #\u0085#    (@@@@@@@(   @@@( @@@@   (@@@  @@@@       ,@@@(  (@@@@@@@(   @@@(          #\u0085#      @@@@@     @@@(  @@@@@@@@@@  @@@@       ,@@@(    @@@@@     @@@(          #\u0085#       @@@      @@@     @@@@@*    @@@         @@@      @@@      @@@           #\u0085#                                                                              #\u0085################################################################################";

private CLI() {
}

Expand Down Expand Up @@ -67,7 +69,7 @@ public static void start(Class<?> cliClass) {
.completer(completer)
.build();


terminal.writer().println(LOGO.replaceAll("\u0085", "\r\n"));
terminal.writer().println("Welcome to the interactive Cineast CLI.");


Expand Down

0 comments on commit 4a471d1

Please sign in to comment.