Skip to content

Commit

Permalink
ui fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Loki-Afro committed Oct 19, 2023
1 parent 69f1525 commit 926daa7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/de/svs/Namespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ public class Namespace {
@ConfigProperty(name = "namespace.activationHours", defaultValue = "48")
int activationHours;

@ConfigProperty(name = "externalHostName", defaultValue = "localhost")
String externalHostName;

@CheckedTemplate
public static class Templates {
public static native TemplateInstance namespace(String defaultNamespace, String message);
public static native TemplateInstance namespace(String host, String defaultNamespace, String message);
}


@GET
@Produces(MediaType.TEXT_HTML)
public TemplateInstance get(@QueryParam("namespace") Optional<String> namespace, @QueryParam("redirected-from-503") Optional<Boolean> gotRedirectedFrom503) {
return Templates.namespace(namespace.orElse(""),
return Templates.namespace(this.externalHostName,
namespace.orElse(""),
gotRedirectedFrom503.filter(Boolean::booleanValue)
.map(b -> "You got here because your namespace appears to be deactivated")
.orElse(null));
Expand Down Expand Up @@ -71,7 +75,7 @@ public TemplateInstance post(@FormParam("namespace") String namespace, @FormPara
message = namespace + " not found";
}
Log.info(message);
return Templates.namespace(namespace, message);
return Templates.namespace(this.externalHostName, namespace, message);
}

@PUT
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/de/svs/og/RandomGiphyGif.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.svs.og;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.StreamingOutput;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.resteasy.reactive.RestResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Optional;

@Path("/og")
public class RandomGiphyGif {

@ConfigProperty(name = "giphyapikey")
Optional<String> giphyApiKey;


@Path("/image.gif")
@GET
@Produces("image/gif")
public RestResponse<StreamingOutput> getRandomGif() throws InterruptedException, IOException, URISyntaxException {
if (giphyApiKey.isPresent()) {
String apiKey = giphyApiKey.get();

URI uri = URI.create("https://api.giphy.com/v1/gifs/random?api_key=" + apiKey + "&tag=cat");
String gihpyUrl = new ObjectMapper().readTree(uri.toURL()).get("data").get("images").get("original").get("url").textValue();

HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(new URI(gihpyUrl)).GET().build();
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());

// stream gets closed after it has been fully copied
InputStream inputStream = response.body();
return RestResponse.ok(inputStream::transferTo, "image/gif");
} else {
return RestResponse.notFound();
}
}

}
Binary file added src/main/resources/META-INF/resources/cat.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/main/resources/templates/Namespace/namespace.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<head>
<meta charset="UTF-8">
<title>Namespace Activator</title>
<meta property="og:type" content="website" />
<meta property="og:title" content="pustekuchen" />
<meta property="og:image" content="https://{host}/og/image.gif" />
<style>
body {
background-color: black;
Expand Down

0 comments on commit 926daa7

Please sign in to comment.