Remove edit, cleanup, rename
This commit is contained in:
319
src/main/java/tech/nevets/signaturecardgen/Card.java
Normal file
319
src/main/java/tech/nevets/signaturecardgen/Card.java
Normal file
@@ -0,0 +1,319 @@
|
||||
package tech.nevets.signaturecardgen;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import javax.imageio.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Card {
|
||||
public static final Map<String, Card> CARD_SESSIONS = new HashMap<>();
|
||||
|
||||
// ---------------- COLORS ---------------- //
|
||||
private static final Color WHITE = new Color(255, 255, 255);
|
||||
private static final Color GREEN = new Color(101, 141, 27);
|
||||
|
||||
// ---------------- FONTS ---------------- //
|
||||
private static final Font ARIAL65 = new Font("Arial", Font.PLAIN, 65);
|
||||
private static final Font ARIAL55 = new Font("Arial", Font.PLAIN, 55);
|
||||
private static final Font ARIAL45 = new Font("Arial", Font.PLAIN, 45);
|
||||
private static final Font ARIAL45I = new Font("Arial", Font.ITALIC, 45);
|
||||
private static final Font ARIAL44I = new Font("Arial", Font.ITALIC, 44);
|
||||
private static final Font ARIAL40I = new Font("Arial", Font.ITALIC, 40);
|
||||
private static final Font ARIAL38I = new Font("Arial", Font.ITALIC, 38);
|
||||
|
||||
// ---------------- LAYERS ---------------- //
|
||||
private final BufferedImage background;
|
||||
private final BufferedImage nameLayer = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
|
||||
private final BufferedImage titleLayer = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
|
||||
private final BufferedImage emailLayer = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
|
||||
private final BufferedImage locationLayer = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
|
||||
private final BufferedImage phoneNumbersLayer = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
// ---------------- Working Images ---------------- //
|
||||
private Graphics2D graphics;
|
||||
private final BufferedImage rawImage = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
|
||||
private BufferedImage workingImage = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
|
||||
|
||||
// ---------------- DATA ---------------- //
|
||||
public final String id;
|
||||
private String name;
|
||||
private String title;
|
||||
private String email;
|
||||
private Location location;
|
||||
private String extension;
|
||||
private String directNumber;
|
||||
private String cellNumber;
|
||||
private int size;
|
||||
private boolean hasDirectNumber;
|
||||
private boolean hasCellNumber;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs an empty card.<br>
|
||||
* Calls:<br>{@link tech.nevets.signaturecardgen.Card#Card(String, String, String, String, String, String, String, int)} with empty strings and full size image
|
||||
*/
|
||||
public Card() {
|
||||
this("", "", "", "", "", "", "", 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that fills out entire Card details.<br>
|
||||
* Calls:<br>{@link tech.nevets.signaturecardgen.Card#Card(String, String, String, String, String, String, String, int)}
|
||||
* @param json Json object from frontend
|
||||
*/
|
||||
public Card(JsonObject json) {
|
||||
this(
|
||||
json.get("name").getAsString(),
|
||||
json.get("title").getAsString(),
|
||||
json.get("email").getAsString(),
|
||||
json.get("locationId").getAsString(),
|
||||
json.get("extension").getAsString(),
|
||||
json.get("directNumber").getAsString(),
|
||||
json.get("cellNumber").getAsString(),
|
||||
json.get("size").getAsInt()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that fills out entire Card details.
|
||||
* @param name User's Name
|
||||
* @param title User's Title
|
||||
* @param email User's Email
|
||||
* @param locationId User's Location ID
|
||||
* @param extension User's Extension (can be empty <b>if</b> {@link tech.nevets.signaturecardgen.Card#directNumber} is populated)
|
||||
* @param directNumber User's Direct Number (can be empty <b>if</b> {@link tech.nevets.signaturecardgen.Card#extension} is populated)
|
||||
* @param cellNumber User's Cell Phone Number (can be empty)
|
||||
* @param size Size to set the Card to (0 for Outlook 2016 size, 1 for full size)
|
||||
*/
|
||||
public Card(String name, String title, String email, String locationId, String extension, String directNumber, String cellNumber, int size) {
|
||||
this.id = getUniqueId();
|
||||
this.name = name;
|
||||
this.title = title;
|
||||
this.email = email;
|
||||
Location location = Location.getLocation(locationId);
|
||||
background = location.getBackground();
|
||||
this.extension = extension;
|
||||
this.directNumber = directNumber;
|
||||
this.cellNumber = cellNumber;
|
||||
this.hasDirectNumber = this.directNumber.length() > 0;
|
||||
this.hasCellNumber = this.cellNumber.length() > 0;
|
||||
this.size = size;
|
||||
CARD_SESSIONS.put(id, this);
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void setLocation(Location location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public void setExtension(String extension) {
|
||||
this.extension = extension;
|
||||
hasDirectNumber = false;
|
||||
this.directNumber = "";
|
||||
}
|
||||
|
||||
public void setDirectNumber(String directNumber) {
|
||||
this.directNumber = directNumber;
|
||||
hasDirectNumber = directNumber.length() > 0;
|
||||
this.extension = "";
|
||||
}
|
||||
|
||||
public void setCellNumber(String cellNumber) {
|
||||
this.cellNumber = cellNumber;
|
||||
hasCellNumber = cellNumber.length() > 0;
|
||||
}
|
||||
|
||||
public void setSize(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public BufferedImage getBackground() {
|
||||
return background;
|
||||
}
|
||||
|
||||
private void clearLayer(BufferedImage layer) {
|
||||
Graphics2D g = layer.createGraphics();
|
||||
g.setComposite(AlphaComposite.Clear);
|
||||
g.fillRect(0, 0, 1080, 602);
|
||||
g.dispose();
|
||||
}
|
||||
|
||||
private void renderLayer(BufferedImage layer, String content, Color textColor, Font font, int x, int y) {
|
||||
graphics = layer.createGraphics();
|
||||
setAntiAlias(graphics);
|
||||
graphics.setColor(textColor);
|
||||
graphics.setFont(font);
|
||||
graphics.drawString(content, x, y);
|
||||
graphics.dispose();
|
||||
}
|
||||
|
||||
private void renderResizableLayer(BufferedImage layer, String content, Color textColor, int maxLength, Font defaultFont, Font smallFont, int x, int y) {
|
||||
graphics = layer.createGraphics();
|
||||
setAntiAlias(graphics);
|
||||
graphics.setColor(textColor);
|
||||
if (fitsDimensions(content, defaultFont, maxLength)) graphics.setFont(defaultFont);
|
||||
else graphics.setFont(smallFont);
|
||||
graphics.drawString(content, x, y);
|
||||
graphics.dispose();
|
||||
}
|
||||
|
||||
public BufferedImage renderNameLayer() {
|
||||
clearLayer(nameLayer);
|
||||
renderResizableLayer(nameLayer, name, WHITE, 970, ARIAL65, ARIAL55, 85, 112);
|
||||
return nameLayer;
|
||||
}
|
||||
|
||||
public BufferedImage renderTitleLayer() {
|
||||
clearLayer(titleLayer);
|
||||
renderResizableLayer(titleLayer, title, WHITE, 970, ARIAL45I, ARIAL40I, 89, 176);
|
||||
return titleLayer;
|
||||
}
|
||||
|
||||
public BufferedImage renderEmailLayer() {
|
||||
clearLayer(emailLayer);
|
||||
renderLayer(emailLayer, email, WHITE, ARIAL45, 62, (380 + getOffsets(0)));
|
||||
return emailLayer;
|
||||
}
|
||||
|
||||
public BufferedImage renderLocationLayer() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String address = location.getAddress();
|
||||
String[] splitAddr = address.split(",");
|
||||
if (fitsDimensions(address, ARIAL38I, 700)) {
|
||||
sb.append(address);
|
||||
} else {
|
||||
int lineOneLength = 0;
|
||||
int newLineIndex = 0;
|
||||
for (String addrPart : splitAddr) {
|
||||
if (fitsDimensions(addrPart, ARIAL38I, (700 - lineOneLength))) {
|
||||
sb.append(addrPart).append(",");
|
||||
lineOneLength += stringWidth(addrPart, ARIAL38I);
|
||||
newLineIndex += addrPart.length();
|
||||
} else {
|
||||
sb.append(addrPart).append(",");
|
||||
}
|
||||
}
|
||||
sb.delete(newLineIndex - 2, newLineIndex);
|
||||
sb.insert(newLineIndex - 2, "\n");
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
}
|
||||
clearLayer(locationLayer);
|
||||
renderLayer(locationLayer, location.getName(), GREEN, ARIAL44I, 59, (447 + getOffsets(0)));
|
||||
renderLayer(locationLayer, sb.toString(), GREEN, ARIAL38I, 59, (491 + getOffsets(0)));
|
||||
return locationLayer;
|
||||
}
|
||||
|
||||
public BufferedImage renderPhoneNumbersLayer() {
|
||||
clearLayer(phoneNumbersLayer);
|
||||
if (hasDirectNumber) {
|
||||
renderLayer(phoneNumbersLayer, ("W: " + directNumber), GREEN, ARIAL38I, 59, (540 + getOffsets(1)));
|
||||
} else {
|
||||
renderLayer(phoneNumbersLayer, ("W: " + location.getNumber() + " x" + extension), GREEN, ARIAL38I, 59, (540 + getOffsets(1)));
|
||||
}
|
||||
if (hasCellNumber) {
|
||||
renderLayer(phoneNumbersLayer, ("C: " + cellNumber), GREEN, ARIAL38I, 59, 540);
|
||||
}
|
||||
return phoneNumbersLayer;
|
||||
}
|
||||
|
||||
public void renderImage() {
|
||||
clearLayer(rawImage);
|
||||
graphics = rawImage.createGraphics();
|
||||
|
||||
graphics.drawImage(background, 0, 0, null);
|
||||
graphics.drawImage(nameLayer, 0, 0, null);
|
||||
graphics.drawImage(titleLayer, 0, 0, null);
|
||||
graphics.drawImage(emailLayer, 0, 0, null);
|
||||
graphics.drawImage(locationLayer, 0, 0, null);
|
||||
graphics.drawImage(phoneNumbersLayer, 0, 0, null);
|
||||
|
||||
graphics.dispose();
|
||||
resizeImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets offsets for necessary layers
|
||||
* @param type <br>0: Both double number and long address<br>1: Only double number
|
||||
* @return Negative integer value for pixel offset
|
||||
*/
|
||||
private int getOffsets(int type) {
|
||||
int totalOffset = 0;
|
||||
int doubleNumberOffset = hasCellNumber ? 40 : 0;
|
||||
int longAddrOffset = fitsDimensions(location.getAddress(), ARIAL38I, 700) ? 0 : 40;
|
||||
switch (type) {
|
||||
case 0 -> {
|
||||
totalOffset -= doubleNumberOffset;
|
||||
totalOffset -= longAddrOffset;
|
||||
}
|
||||
case 1 -> totalOffset -= doubleNumberOffset;
|
||||
}
|
||||
return totalOffset;
|
||||
}
|
||||
|
||||
public void resizeImage() {
|
||||
resizeImage(this.size);
|
||||
}
|
||||
|
||||
public void resizeImage(int size) {
|
||||
BufferedImage resizedImage;
|
||||
switch (size) {
|
||||
case 0 -> resizedImage = resizeImage(300, 167, false);
|
||||
case 1 -> resizedImage = rawImage;
|
||||
default -> resizedImage = background;
|
||||
}
|
||||
workingImage = resizedImage;
|
||||
}
|
||||
|
||||
public BufferedImage resizeImage(int width, int height, boolean antiAlias) {
|
||||
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g = resizedImage.createGraphics();
|
||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||
if (antiAlias) g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g.drawImage(rawImage, 0, 0, width, height, null);
|
||||
g.dispose();
|
||||
return resizedImage;
|
||||
}
|
||||
|
||||
public byte[] toByteArray() throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(workingImage, "png", baos);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
private void setAntiAlias(Graphics2D g) {
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
}
|
||||
|
||||
private static final Canvas C = new Canvas();
|
||||
private static boolean fitsDimensions(String text, Font font, int maxLength) {
|
||||
return C.getFontMetrics(font).stringWidth(text) < maxLength;
|
||||
}
|
||||
private static int stringWidth(String text, Font font) {
|
||||
return C.getFontMetrics(font).stringWidth(text);
|
||||
}
|
||||
|
||||
private static String getUniqueId() {
|
||||
String uuid = UUID.randomUUID().toString().split("-")[4];
|
||||
while (true) {
|
||||
String finalUuid = uuid;
|
||||
if (CARD_SESSIONS.entrySet().stream().noneMatch(card -> card.getKey().equals(finalUuid))) {
|
||||
return uuid;
|
||||
}
|
||||
uuid = UUID.randomUUID().toString().split("-")[4];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package tech.nevets.signaturecardgen;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import spark.Request;
|
||||
import spark.Response;
|
||||
import spark.Route;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class GenerateRoute implements Route {
|
||||
@Override
|
||||
public Object handle(Request req, Response res) {
|
||||
Card card;
|
||||
String id = req.queryParams("id");
|
||||
if (!id.isEmpty()) {
|
||||
card = Card.CARD_SESSIONS.get(id);
|
||||
if (card == null) {
|
||||
res.type("text/plain");
|
||||
res.status(404);
|
||||
return "Card with that id not found.";
|
||||
}
|
||||
} else {
|
||||
JsonObject data;
|
||||
try {
|
||||
data = new Gson().fromJson(req.body(), JsonObject.class);
|
||||
} catch (JsonSyntaxException e) {
|
||||
res.status(422);
|
||||
return "Not valid json: " + e.getMessage();
|
||||
}
|
||||
System.out.println("JSON Data: " + data.toString());
|
||||
card = new Card(data);
|
||||
}
|
||||
card.renderImage();
|
||||
|
||||
res.type("image/png");
|
||||
try {
|
||||
res.status(200);
|
||||
return card.toByteArray();
|
||||
} catch (IOException e) {
|
||||
res.status(500);
|
||||
return "Error getting image stream: " + e.getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
115
src/main/java/tech/nevets/signaturecardgen/LiveGenWebSocket.java
Normal file
115
src/main/java/tech/nevets/signaturecardgen/LiveGenWebSocket.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package tech.nevets.signaturecardgen;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.*;
|
||||
import org.eclipse.jetty.websocket.api.annotations.*;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@WebSocket
|
||||
public class LiveGenWebSocket {
|
||||
|
||||
private Card card;
|
||||
|
||||
@OnWebSocketConnect
|
||||
public void connected(Session session) {}
|
||||
|
||||
@OnWebSocketClose
|
||||
public void closed(Session session, int statusCode, String reason) {
|
||||
card = null;
|
||||
}
|
||||
|
||||
@OnWebSocketError
|
||||
public void error(Session session, Throwable throwable) {
|
||||
if (throwable instanceof CloseException) {
|
||||
System.out.println("Connection with session address " + session.getRemoteAddress() + " closed");
|
||||
} else {
|
||||
throwable.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@OnWebSocketMessage
|
||||
public void message(Session session, String message) throws IOException {
|
||||
RemoteEndpoint client = session.getRemote();
|
||||
|
||||
System.out.println("WS Msg: " + message);
|
||||
String[] splitMsg = message.split(";");
|
||||
String messageType = splitMsg[0];
|
||||
|
||||
if (splitMsg.length == 1) {
|
||||
if (messageType.equals("start")) {
|
||||
card = new Card();
|
||||
client.sendString("success;" + card.id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
String messageContent = splitMsg[1];
|
||||
|
||||
switch (messageType) {
|
||||
case "continue" -> {
|
||||
card = Card.CARD_SESSIONS.get(messageContent);
|
||||
if (card == null) {
|
||||
card = new Card();
|
||||
client.sendString("notExist;Card with that id does not exist, a new card has been created.");
|
||||
} else {
|
||||
client.sendString("success;Card loaded successfully.");
|
||||
}
|
||||
}
|
||||
case "background" -> {
|
||||
client.sendString("background");
|
||||
client.sendBytes(byteBufferFromImage(card.getBackground()));
|
||||
}
|
||||
case "location" -> {
|
||||
card.setLocation(Location.getLocation(messageContent));
|
||||
client.sendString("location");
|
||||
client.sendBytes(byteBufferFromImage(card.renderLocationLayer()));
|
||||
}
|
||||
case "name" -> {
|
||||
card.setName(messageContent);
|
||||
client.sendString("name");
|
||||
client.sendBytes(byteBufferFromImage(card.renderNameLayer()));
|
||||
}
|
||||
case "title" -> {
|
||||
card.setTitle(messageContent);
|
||||
client.sendString("title");
|
||||
client.sendBytes(byteBufferFromImage(card.renderTitleLayer()));
|
||||
}
|
||||
case "email" -> {
|
||||
card.setEmail(messageContent);
|
||||
client.sendString("email");
|
||||
client.sendBytes(byteBufferFromImage(card.renderEmailLayer()));
|
||||
}
|
||||
case "extension" -> {
|
||||
card.setExtension(messageContent);
|
||||
client.sendString("extension");
|
||||
client.sendBytes(byteBufferFromImage(card.renderPhoneNumbersLayer()));
|
||||
}
|
||||
case "directNumber" -> {
|
||||
card.setDirectNumber(messageContent);
|
||||
client.sendString("directNumber");
|
||||
client.sendBytes(byteBufferFromImage(card.renderPhoneNumbersLayer()));
|
||||
}
|
||||
case "cellNumber" -> {
|
||||
card.setCellNumber(messageContent);
|
||||
client.sendString("cellNumber");
|
||||
client.sendBytes(byteBufferFromImage(card.renderPhoneNumbersLayer()));
|
||||
}
|
||||
default -> client.sendString("Unknown Message Type: " + messageType);
|
||||
}
|
||||
}
|
||||
|
||||
private ByteBuffer byteBufferFromImage(BufferedImage image) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
ImageIO.write(image, "png", baos);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return ByteBuffer.wrap(("Error writing image: " + e.getMessage()).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
return ByteBuffer.wrap(baos.toByteArray());
|
||||
}
|
||||
}
|
||||
90
src/main/java/tech/nevets/signaturecardgen/Location.java
Normal file
90
src/main/java/tech/nevets/signaturecardgen/Location.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package tech.nevets.signaturecardgen;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Location {
|
||||
public static final List<Location> LOCATIONS = new ArrayList<>();
|
||||
|
||||
private final String id;
|
||||
private final String name;
|
||||
private final String address;
|
||||
private final String number;
|
||||
private transient BufferedImage background;
|
||||
|
||||
public Location(String id, String name, String address, String number) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.number = number;
|
||||
loadBackground();
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public BufferedImage getBackground() {
|
||||
return background;
|
||||
}
|
||||
|
||||
public void loadBackground() {
|
||||
URL backgroundURL = this.getClass().getResource("/backgrounds/" + id + ".png");
|
||||
if (backgroundURL == null) backgroundURL = this.getClass().getResource("/backgrounds/default.png");
|
||||
try {
|
||||
assert backgroundURL != null;
|
||||
background = ImageIO.read(backgroundURL);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{ \"id\": " + id + ", \"name\": " + name + ", \"address\": " + address + ", \"number\": " + number + " }";
|
||||
}
|
||||
|
||||
public static void loadLocations () {
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
JsonArray jsonFile = gson.fromJson(new FileReader("./locations.json"), JsonArray.class);
|
||||
for (JsonElement element : jsonFile.asList()) {
|
||||
Location location = gson.fromJson(element, Location.class);
|
||||
location.loadBackground();
|
||||
LOCATIONS.add(location);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Location getLocation (String locationId) {
|
||||
for (Location loc : LOCATIONS) {
|
||||
if (loc.getId().equals(locationId)) {
|
||||
return loc;
|
||||
}
|
||||
}
|
||||
return new Location("", "", "", "");
|
||||
}
|
||||
}
|
||||
44
src/main/java/tech/nevets/signaturecardgen/Main.java
Normal file
44
src/main/java/tech/nevets/signaturecardgen/Main.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package tech.nevets.signaturecardgen;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static spark.Spark.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Location.loadLocations();
|
||||
|
||||
port(8080);
|
||||
|
||||
webSocket("/backend/generate/live", LiveGenWebSocket.class);
|
||||
|
||||
path("/backend", () -> {
|
||||
before("/*", (request, response) -> response.header("Access-Control-Allow-Origin", "*"));
|
||||
|
||||
options("/*", (request, response) -> {
|
||||
String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
|
||||
if (accessControlRequestHeaders != null) response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
|
||||
|
||||
String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
|
||||
if (accessControlRequestMethod != null) response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
|
||||
|
||||
return "OK";
|
||||
});
|
||||
|
||||
get("/heartbeat", (req, res) -> {
|
||||
res.status(200);
|
||||
res.type("application/json");
|
||||
return "{ \"up\":true }";
|
||||
});
|
||||
|
||||
get("/data/locations", (req, res) -> {
|
||||
res.status(200);
|
||||
res.type("application/json");
|
||||
return new FileInputStream("locations.json");
|
||||
});
|
||||
|
||||
|
||||
post("/generate", new GenerateRoute());
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user