Skip to content

Commit

Permalink
GH-90 Implement Vue.js based frontend for Reposilite (Resolve #90)
Browse files Browse the repository at this point in the history
GH-58 Repository browser (Resolve #58)
  • Loading branch information
dzikoysk committed May 29, 2020
1 parent 521d5a8 commit def8065
Show file tree
Hide file tree
Showing 25 changed files with 956 additions and 270 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@
<developerConnection>scm:git:https://github.com/dzikoysk/reposilite.git</developerConnection>
<url>https://github.com/dzikoysk/reposilite</url>
</scm>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.panda_lang.reposilite.config.ConfigurationLoader;
import org.panda_lang.reposilite.console.Console;
import org.panda_lang.reposilite.frontend.Frontend;
import org.panda_lang.reposilite.frontend.FrontendLoader;
import org.panda_lang.reposilite.metadata.MetadataService;
import org.panda_lang.reposilite.repository.RepositoryService;
import org.panda_lang.reposilite.stats.StatsService;
Expand All @@ -43,8 +42,8 @@ public final class Reposilite {
private final RepositoryService repositoryService = new RepositoryService();
private final Authenticator authenticator = new Authenticator(tokenService);
private final ReposiliteHttpServer reactiveHttpServer = new ReposiliteHttpServer(this);
private final Frontend frontend = Frontend.createInstance();
private Configuration configuration;
private Frontend frontend;
private boolean stopped;
private long uptime;

Expand All @@ -69,9 +68,6 @@ public void launch(String[] args) throws Exception {
ConfigurationLoader configurationLoader = new ConfigurationLoader();
this.configuration = configurationLoader.load();

FrontendLoader frontendLoader = new FrontendLoader();
this.frontend = frontendLoader.loadFrontend(ReposiliteConstants.FRONTEND_FILE_NAME);

getLogger().info("--- Loading data");
tokenService.load();
statsService.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public final class ReposiliteConstants {

public static final String STATS_FILE_NAME = "stats.yml";

public static final String FRONTEND_FILE_NAME = "index.html";

private ReposiliteConstants() { }

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void start(Configuration configuration, Runnable onStart) {
LookupController lookupController = new LookupController(reposilite.getFrontend(), lookupService);

this.javalin = Javalin.create(this::config)
.get("/", new FrontendController(reposilite))
.get("/api/*", new IndexApiController(reposilite))
.get("/js/app.js", new FrontendController(reposilite))
.get("/*", lookupController)
.head("/*", lookupController)
.put("/*", new DeployController(reposilite))
Expand All @@ -64,8 +64,9 @@ void start(Configuration configuration, Runnable onStart) {
}

private void config(JavalinConfig config) {
config.server(() -> new Server(new QueuedThreadPool(4 * Runtime.getRuntime().availableProcessors())));
config.server(() -> new Server(new QueuedThreadPool(2 * Runtime.getRuntime().availableProcessors())));
config.showJavalinBanner = false;
config.enableCorsForOrigin("http://localhost:8080/");
}

void stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public final class IndexApiController implements RepositoryController {
private final Configuration configuration;
private final RepositoryService repositoryService;


public IndexApiController(Reposilite reposilite) {
this.configuration = reposilite.getConfiguration();
this.repositoryService = reposilite.getRepositoryService();
}

@Override
public Context handleContext(Context ctx) {
Reposilite.getLogger().info(ctx.req.getRequestURI() + " API");

String uri = RepositoryUtils.normalizeUri(configuration, StringUtils.replaceFirst(ctx.req.getRequestURI(), "/api/", StringUtils.EMPTY));
File requestedFile = repositoryService.getFile(uri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,32 @@

package org.panda_lang.reposilite.frontend;

import org.panda_lang.reposilite.utils.FilesUtils;
import org.panda_lang.utilities.commons.StringUtils;
import org.panda_lang.utilities.commons.function.Lazy;

import java.util.function.Supplier;

public final class Frontend {

private final String content;
private final Lazy<String> index;
private final Lazy<String> app;

public Frontend(String content) {
this.content = content;
public Frontend(Supplier<String> index, Supplier<String> app) {
this.index = new Lazy<>(index);
this.app = new Lazy<>(app);
}

public String forMessage(String message) {
return StringUtils.replace(content, "{{message}}", message);
return StringUtils.replace(index.get(), "{{message}}", message);
}

public String getApp() {
return app.get();
}

public static Frontend createInstance() {
return new Frontend(() -> FilesUtils.getResource("/frontend/index.html"), () -> FilesUtils.getResource("/frontend/js/app.js"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import io.javalin.http.Context;
import io.javalin.http.Handler;
import org.panda_lang.reposilite.Reposilite;
import org.panda_lang.reposilite.RepositoryController;

public class FrontendController implements Handler {
public class FrontendController implements RepositoryController {

private final Reposilite reposilite;

Expand All @@ -29,8 +30,12 @@ public FrontendController(Reposilite reposilite) {
}

@Override
public void handle(Context context) {
context.header("Content-Type", "text/html").result(reposilite.getFrontend().forMessage("maven repository"));
public Context handleContext(Context ctx) {
ctx.res.setCharacterEncoding("utf-8");

return ctx
.header("Content-Type", "application/javascript")
.result(reposilite.getFrontend().getApp());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.panda_lang.reposilite.utils.FilesUtils;
import org.panda_lang.utilities.commons.FileUtils;
import org.panda_lang.utilities.commons.StringUtils;
import org.panda_lang.utilities.commons.function.Lazy;

import java.io.File;
import java.io.IOException;
Expand All @@ -38,10 +39,10 @@ public final class MetadataService {

private final Map<String, String> metadataCache = new HashMap<>();

private final XmlMapper xmlMapper = XmlMapper.xmlBuilder()
private final Lazy<XmlMapper> xmlMapper = new Lazy<>(() -> XmlMapper.xmlBuilder()
.serializationInclusion(Include.NON_NULL)
.defaultUseWrapper(false)
.build();
.build());

public @Nullable String generateMetadata(Repository repository, String[] requested) throws IOException {
File metadataFile = RepositoryUtils.toRequestedFile(repository, requested);
Expand Down Expand Up @@ -136,7 +137,7 @@ public final class MetadataService {
}

private String toMetadataFile(File metadataFile, Metadata metadata) throws IOException {
String serializedMetadata = xmlMapper.writeValueAsString(metadata);
String serializedMetadata = xmlMapper.get().writeValueAsString(metadata);
FileUtils.overrideFile(metadataFile, serializedMetadata);
metadataCache.put(metadataFile.getPath(), serializedMetadata);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.panda_lang.reposilite.metadata.MetadataService;
import org.panda_lang.reposilite.utils.Result;

import javax.servlet.MultipartConfigElement;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -41,7 +40,6 @@ public final class DeployController implements Handler {
private final Configuration configuration;
private final Authenticator authenticator;
private final MetadataService metadataService;
private final MultipartConfigElement configElement = new MultipartConfigElement(".temp");

public DeployController(Reposilite reposilite) {
this.frontend = reposilite.getFrontend();
Expand Down Expand Up @@ -98,16 +96,4 @@ public Result<Context, String> deploy(Context context) {
}
}

/*
public ServletInputStream getInputStream(Request request) throws IOException {
HttpServletRequest raw = request.raw();
if (raw instanceof ServletRequestWrapper) {
return ((ServletRequestWrapper) raw).getRequest().getInputStream();
}
return raw.getInputStream();
}
*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ public void handle(Context ctx) {
.serveLocal(ctx)
.orElse(localError -> lookupService.serveProxied(ctx).orElse(proxiedError -> Result.error(localError)));

lookupResponse.getError().peek(error -> ctx.status(HttpStatus.SC_NOT_FOUND)
.contentType("text/html")
.result(frontend.forMessage(error))
);

lookupResponse.getError().peek(error -> {
ctx.res.setCharacterEncoding("UTF-8");
ctx.status(HttpStatus.SC_NOT_FOUND)
.contentType("text/html")
.result(frontend.forMessage(error));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import com.google.common.io.Files;
import org.apache.commons.io.FileUtils;
import org.panda_lang.reposilite.Reposilite;
import org.panda_lang.utilities.commons.IOUtils;
import org.panda_lang.utilities.commons.StringUtils;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -93,4 +95,8 @@ public static String getExtension(String name) {
return occurrence == -1 ? StringUtils.EMPTY : name.substring(occurrence + 1);
}

public static String getResource(String name) {
return IOUtils.toString(Reposilite.class.getResourceAsStream(name), StandardCharsets.UTF_8);
}

}
1 change: 1 addition & 0 deletions reposilite-backend/src/main/resources/frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href="data:;base64,iVBORw0KGgo="><title>Reposilite</title><link href="https://fonts.googleapis.com/css2?family=Manrope:wght@300;500&display=swap" rel=stylesheet><script src=https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js></script><script>window.REPOSILITE_MESSAGE = '{{message}}'</script><link href=/js/app.js rel=preload as=script></head><body><noscript><strong>We're sorry but reposilite-frontend doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=/js/app.js></script></body></html>
Loading

0 comments on commit def8065

Please sign in to comment.