Skip to content

Commit 8c6b7eb

Browse files
committed
Adjust log files, httpserver
1 parent 09d9943 commit 8c6b7eb

10 files changed

Lines changed: 65 additions & 22 deletions

File tree

src/main/java/simplexity/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import simplexity.commands.HelpCommand;
1010
import simplexity.commands.ReloadCommand;
1111
import simplexity.config.TTSConfig;
12+
import simplexity.httpserver.LocalServer;
1213
import simplexity.setup.PollySetup;
1314

1415
import java.util.Scanner;
@@ -27,6 +28,7 @@ public static void main(String[] args) {
2728
registerCommands(commandManager);
2829
TTSConfig.getInstance().reloadConfig();
2930
PollySetup.setupPollyAndSpeech();
31+
LocalServer.run();
3032
while (true) {
3133
String input = scanner.nextLine();
3234
if (input.equals("--exit")) {

src/main/java/simplexity/commands/CommandManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public boolean runCommand(String command) {
2222
return true;
2323
}
2424
if (!commands.containsKey(command)){
25-
Util.logAndPrint(logger, "Commands does not contain key " + command, Level.INFO);
2625
return false;
2726
}
2827
commands.get(command).execute();

src/main/java/simplexity/commands/ExitCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package simplexity.commands;
22

33
import org.slf4j.event.Level;
4-
import simplexity.httpserver.AuthServer;
4+
import simplexity.httpserver.LocalServer;
55
import simplexity.messages.Output;
66
import simplexity.util.Util;
77

@@ -14,7 +14,7 @@ public ExitCommand(String name, String usage) {
1414
@Override
1515
public void execute() {
1616
Util.logAndPrint(logger, Output.SHUTTING_DOWN, Level.INFO);
17-
AuthServer.stop();
17+
LocalServer.stop();
1818
System.exit(0);
1919
}
2020
}

src/main/java/simplexity/commands/ReloadCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.slf4j.event.Level;
44
import simplexity.config.TTSConfig;
5+
import simplexity.httpserver.LocalServer;
56
import simplexity.messages.Output;
67
import simplexity.util.Util;
78

@@ -13,6 +14,8 @@ public ReloadCommand(String name, String usage) {
1314
@Override
1415
public void execute() {
1516
TTSConfig.getInstance().reloadConfig();
17+
LocalServer.stop();
18+
LocalServer.run();
1619
Util.logAndPrint(logger, Output.RELOAD_MESSAGE, Level.ERROR);
1720
}
1821
}

src/main/java/simplexity/config/ConfigDefaults.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ public class ConfigDefaults {
44
public static final String AWS_REGION = "aws-region= \"US_EAST_1\"\n";
55
public static final String AWS_ACCESS_KEY = "aws-access-id= \"\"\n";
66
public static final String AWS_SECRET_KEY = "aws-secret-key= \"\"\n";
7+
public static final String SERVER_PORT = "server-port= 3000";
78
public static final String REPLACE_TEXT = """
89
replace-text {
9-
"**"= "<prosody volume=\\"x-loud\\" pitch=\\"low\\" rate=\\"slow\\">"
10-
"/*"= "</prosody>"
10+
" **"= "<prosody volume=\\"x-loud\\" pitch=\\"low\\" rate=\\"slow\\">"
11+
"** "= "</prosody>"
1112
"*/"= "</prosody>"
12-
"~~"= "<amazon:effect name=\\"whispered\\">"
13-
"/~"= "</amazon:effect>"
13+
" ~~"= "<amazon:effect name=\\"whispered\\">"
14+
"~~ "= "</amazon:effect>"
1415
"~/"= "</amazon:effect>"
15-
"__"= "<emphasis level=\\"strong\\">"
16-
"/_"= "</emphasis>"
16+
" __"= "<emphasis level=\\"strong\\">"
17+
"__ "= "</emphasis>"
1718
"_/"= "</emphasis>"
18-
"++"= "<prosody volume=\\"x-loud\\" rate=\\"x-fast\\" pitch=\\"x-high\\">"
19-
"/+"= "</prosody>"
19+
" ++"= "<prosody volume=\\"x-loud\\" rate=\\"x-fast\\" pitch=\\"x-high\\">"
20+
"++ "= "</prosody>"
2021
"+/"= "</prosody>"
21-
"!!"= "<say-as interpret-as=\\"expletive\\">"
22-
"/!"= "</say-as>"
22+
" !!"= "<say-as interpret-as=\\"expletive\\">"
23+
"!! "= "</say-as>"
2324
"!/"= "</say-as>"
2425
" - "= "<break time=\\"300ms\\"/>"
2526
"<3"= "heart emoji"

src/main/java/simplexity/config/SimplexityFileHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ private void createConfigFile(){
3838
writer.write(ConfigDefaults.REPLACE_TEXT);
3939
writer.write(ConfigDefaults.DEFAULT_VOICE);
4040
writer.write(ConfigDefaults.VOICE_PREFIXES);
41+
writer.write(ConfigDefaults.SERVER_PORT);
4142
} catch (Exception exception){
4243
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.ERROR);
4344
}

src/main/java/simplexity/config/TTSConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class TTSConfig {
2222
private Region awsRegion;
2323
private VoiceId defaultVoice;
2424
private String awsAccessID, awsSecretKey;
25+
private int port;
2526

2627
private TTSConfig(){}
2728
private static TTSConfig instance;
@@ -51,6 +52,7 @@ public void reloadConfig(){
5152
reloadRegion(config);
5253
reloadDefaultVoice(config);
5354
reloadStrings(config);
55+
reloadInts(config);
5456

5557
}
5658

@@ -109,6 +111,10 @@ private void reloadStrings(Config config){
109111

110112
}
111113

114+
private void reloadInts(Config config){
115+
port = config.getInt("server-port");
116+
}
117+
112118

113119
public String getAwsAccessID() {
114120
return awsAccessID;
@@ -117,4 +123,8 @@ public String getAwsAccessID() {
117123
public String getAwsSecretKey() {
118124
return awsSecretKey;
119125
}
126+
127+
public int getPort() {
128+
return port;
129+
}
120130
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package simplexity.httpserver;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
import com.sun.net.httpserver.HttpHandler;
5+
6+
import java.io.IOException;
7+
import java.io.OutputStream;
8+
9+
public class ChatHandler implements HttpHandler {
10+
@Override
11+
public void handle(HttpExchange exchange) throws IOException {
12+
String response = "<html><body><h1>Hello World!</h1></body></html>";
13+
sendResponse(exchange, response, 200);
14+
}
15+
16+
private static void sendResponse(HttpExchange exchange, String response, int statusCode) throws IOException {
17+
exchange.sendResponseHeaders(statusCode, response.getBytes().length);
18+
OutputStream outputStream = exchange.getResponseBody();
19+
outputStream.write(response.getBytes());
20+
outputStream.close();
21+
}
22+
}

src/main/java/simplexity/httpserver/AuthServer.java renamed to src/main/java/simplexity/httpserver/LocalServer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package simplexity.httpserver;
22

3-
import com.sun.net.httpserver.HttpServer;
3+
import com.sun.net.httpserver.HttpHandler;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66
import org.slf4j.event.Level;
7+
import simplexity.config.TTSConfig;
78
import simplexity.messages.Errors;
89
import simplexity.util.Util;
910

1011
import java.io.IOException;
1112
import java.net.InetSocketAddress;
1213

13-
public class AuthServer {
14-
private static final Logger logger = LoggerFactory.getLogger(AuthServer.class);
15-
public static HttpServer server;
14+
public class LocalServer {
15+
private static final Logger logger = LoggerFactory.getLogger(LocalServer.class);
16+
public static com.sun.net.httpserver.HttpServer server;
1617

1718
public static void run() {
1819
try {
@@ -28,8 +29,10 @@ public static void stop() {
2829

2930
private static void setupServer() {
3031
try {
31-
server = HttpServer.create(new InetSocketAddress(3000), 0);
32+
server = com.sun.net.httpserver.HttpServer.create(
33+
new InetSocketAddress(TTSConfig.getInstance().getPort()), 0);
3234
server.setExecutor(null);
35+
server.createContext("/", new ChatHandler());
3336
server.start();
3437
} catch (IOException exception) {
3538
Util.logAndPrint(logger, Errors.CAUGHT_EXCEPTION.replace("%error%", exception.getMessage()), Level.TRACE);

src/main/resources/logback.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<configuration>
33

4-
<!-- Define a file appender that creates a new log file every time the application is run -->
4+
<!-- Define a file appender that creates unique log files -->
55
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
6-
<file>logs/application-${currentDate}.log</file>
6+
<!-- Add a timestamp to ensure unique filenames for logs -->
7+
<file>logs/application-${currentDate}-${currentTime}.log</file>
78
<append>false</append>
89
<encoder>
910
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
1011
</encoder>
1112
</appender>
1213

13-
<!-- Log at the DEBUG level -->
14+
<!-- Log at the INFO level -->
1415
<root level="INFO">
1516
<appender-ref ref="FILE"/>
1617
</root>
1718

18-
<!-- Set the current date in the format YYYY-MM-DD -->
19+
<!-- Set the current date and time -->
1920
<timestamp key="currentDate" datePattern="yyyy-MM-dd"/>
21+
<timestamp key="currentTime" datePattern="HH-mm-ss"/>
2022

2123
</configuration>

0 commit comments

Comments
 (0)