Skip to content

Commit

Permalink
#30 re-implementing splash and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hlafaille committed Aug 9, 2024
1 parent a31d981 commit e065c70
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@
<artifactId>snakeyaml</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.15</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.2.1-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/io/kerosenelabs/atc/Main.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
package io.kerosenelabs.atc;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.concurrent.ThreadLocalRandom;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

import io.kerosenelabs.atc.constant.AnsiCodes;
import io.kerosenelabs.kindling.HttpRequest;
import io.kerosenelabs.kindling.HttpResponse;
import io.kerosenelabs.kindling.KindlingServer;
import io.kerosenelabs.kindling.constant.HttpMethod;
import io.kerosenelabs.kindling.exception.KindlingException;
import io.kerosenelabs.kindling.handler.RequestHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {
private static String getSplashArt() {
URL resourceUrl = Resources.getResource("art.txt");
if (resourceUrl == null) {
return "(no art)";
}
try {
return AnsiCodes.COLOR_CYAN.getCode() + Resources.toString(resourceUrl, Charsets.UTF_8)
+ AnsiCodes.RESET.getCode();
} catch (IOException e) {
return "(failed to load art)";
}
}

private static String getRandomSplashText() {
URL resourceUrl = Resources.getResource("splash.txt");
if (resourceUrl == null) {
return "(no splash text)";
}
String splashTextContent;
try {
splashTextContent = Resources.toString(resourceUrl, Charsets.UTF_8);
} catch (IOException e) {
return "(failed to load splash text)";
}
String[] splashTextMessages = splashTextContent.split("\n");
return AnsiCodes.COLOR_BLUE.getCode()
+ splashTextMessages[ThreadLocalRandom.current().nextInt(0, splashTextMessages.length)]
+ AnsiCodes.RESET.getCode() + "\n\n";
}

public static void main(String[] args) throws KindlingException {
System.out.println(getSplashArt());
System.out.println(getRandomSplashText());
log.info("Starting up Kindling server");
KindlingServer server = KindlingServer.getInstance();

server.installRequestHandler(new RequestHandler() {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/kerosenelabs/atc/constant/AnsiCodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.kerosenelabs.atc.constant;

public enum AnsiCodes {
RESET("\u001b[0m"),
COLOR_CYAN("\u001b[36m"),
COLOR_BLUE("\u001b[34m");

private String code;

AnsiCodes(String code) {
this.code = code;
}

public String getCode() {
return this.code;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/splash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ We're drowning in requests over here. Just kidding, it's what we do best.
Technically, the name ATC doesn't make sense. Chances are you're using a wire.
Insert funny splash message here.
Hi :)
We're cooking with kindling, now!
We're cooking with Kindling, now!

0 comments on commit e065c70

Please sign in to comment.