Compare commits

...

4 commits

Author SHA1 Message Date
4b60f59956 Add support to use chat hitory for context in private chats 2023-05-21 20:03:34 +02:00
9243b410ab Increase timeout for OpenAI API to 5 minutes
The API is very slow lately...
2023-05-21 19:05:33 +02:00
6d537db12e Move dependency version management from module to parent pom
This should simplify updating dependencies and reduce dependency
resolution overhead.
2023-05-21 19:04:20 +02:00
d160f01b52 Intorduce HikariCP for JDBC connection pooling 2023-05-21 18:48:45 +02:00
14 changed files with 176 additions and 72 deletions

View file

@ -18,16 +18,23 @@
<artifactId>db</artifactId> <artifactId>db</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.javacord</groupId> <groupId>org.javacord</groupId>
<artifactId>javacord</artifactId> <artifactId>javacord</artifactId>
<version>3.8.0</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId> <artifactId>log4j-to-slf4j</artifactId>
<version>2.20.0</version> <scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -1,5 +1,7 @@
package de.hhhammer.dchat.bot; package de.hhhammer.dchat.bot;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import de.hhhammer.dchat.bot.openai.ChatGPTService; import de.hhhammer.dchat.bot.openai.ChatGPTService;
import de.hhhammer.dchat.db.ServerDBService; import de.hhhammer.dchat.db.ServerDBService;
import de.hhhammer.dchat.db.UserDBService; import de.hhhammer.dchat.db.UserDBService;
@ -31,10 +33,18 @@ public class App {
} }
var chatGPTService = new ChatGPTService(openaiApiKey, HttpClient.newHttpClient()); var chatGPTService = new ChatGPTService(openaiApiKey, HttpClient.newHttpClient());
var serverDBService = new ServerDBService(postgresUrl, postgresUser, postgresPassword);
var userDBService = new UserDBService(postgresUrl, postgresUser, postgresPassword);
var discordBot = new DiscordBot(serverDBService, userDBService, chatGPTService, discordApiKey); var config = new HikariConfig();
discordBot.run(); config.setJdbcUrl(postgresUrl);
config.setUsername(postgresUser);
config.setPassword(postgresPassword);
try (var ds = new HikariDataSource(config)) {
var serverDBService = new ServerDBService(ds);
var userDBService = new UserDBService(ds);
var discordBot = new DiscordBot(serverDBService, userDBService, chatGPTService, discordApiKey);
discordBot.run();
}
} }
} }

View file

@ -12,6 +12,8 @@ import org.javacord.api.interaction.SlashCommand;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class DiscordBot implements Runnable { public class DiscordBot implements Runnable {
@ -36,9 +38,10 @@ public class DiscordBot implements Runnable {
.setToken(discordApiKey) .setToken(discordApiKey)
.login() .login()
.join(); .join();
var future = new CompletableFuture<Void>();
Runtime.getRuntime().addShutdownHook(Thread.ofVirtual().unstarted(() -> { Runtime.getRuntime().addShutdownHook(Thread.ofVirtual().unstarted(() -> {
logger.info("Shutting down Discord application"); logger.info("Shutting down Discord application");
discordApi.disconnect(); discordApi.disconnect().thenAccept(future::complete);
})); }));
var token = SlashCommand.with("tokens", "Check how many tokens where spend on this server") var token = SlashCommand.with("tokens", "Check how many tokens where spend on this server")
.createGlobal(discordApi) .createGlobal(discordApi)
@ -69,5 +72,11 @@ public class DiscordBot implements Runnable {
// Print the invite url of your bot // Print the invite url of your bot
logger.info("You can invite the bot by using the following url: " + discordApi.createBotInvite()); logger.info("You can invite the bot by using the following url: " + discordApi.createBotInvite());
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
} }
} }

View file

@ -33,7 +33,7 @@ public class ServerMessageHandler implements MessageHandler {
var systemMessage = this.serverDBService.getConfig(String.valueOf(serverId)).get().systemMessage(); var systemMessage = this.serverDBService.getConfig(String.valueOf(serverId)).get().systemMessage();
var request = event.getMessage().getType() == MessageType.REPLY ? var request = event.getMessage().getType() == MessageType.REPLY ?
new ChatGPTRequestBuilder() new ChatGPTRequestBuilder()
.contextRequest(event.getMessage() .replyRequest(event.getMessage()
.getMessageReference() .getMessageReference()
.map(MessageReference::getMessage) .map(MessageReference::getMessage)
.flatMap(m -> m) .flatMap(m -> m)

View file

@ -24,9 +24,14 @@ public class UserMessageHandler implements MessageHandler {
@Override @Override
public void handle(MessageCreateEvent event) throws ResponseException, IOException, InterruptedException { public void handle(MessageCreateEvent event) throws ResponseException, IOException, InterruptedException {
String content = event.getReadableMessageContent(); String content = event.getReadableMessageContent();
var userId = event.getMessageAuthor().getId(); var userId = String.valueOf(event.getMessageAuthor().getId());
var systemMessage = this.userDBService.getConfig(String.valueOf(userId)).get().systemMessage(); var config = this.userDBService.getConfig(userId).get();
var request = new ChatGPTRequestBuilder().simpleRequest(content, systemMessage); var systemMessage = config.systemMessage();
var context = this.userDBService.getLastMessages(userId, config.contextLength())
.stream()
.map(userMessage -> new ChatGPTRequestBuilder.PreviousInteraction(userMessage.question(), userMessage.answer()))
.toList();
var request = new ChatGPTRequestBuilder().contextRequest(context, content, systemMessage);
var response = this.chatGPTService.submit(request); var response = this.chatGPTService.submit(request);
if (response.choices().size() < 1) { if (response.choices().size() < 1) {
event.getMessage().reply("No response available"); event.getMessage().reply("No response available");

View file

@ -1,6 +1,7 @@
package de.hhhammer.dchat.bot.openai; package de.hhhammer.dchat.bot.openai;
import de.hhhammer.dchat.bot.openai.models.ChatGPTRequest; import de.hhhammer.dchat.bot.openai.models.ChatGPTRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -18,7 +19,7 @@ public class ChatGPTRequestBuilder {
); );
} }
public ChatGPTRequest contextRequest(List<String> contextMessages, String message, String systemMessage) { public ChatGPTRequest replyRequest(List<String> contextMessages, String message, String systemMessage) {
List<ChatGPTRequest.Message> messages = new ArrayList<>(); List<ChatGPTRequest.Message> messages = new ArrayList<>();
messages.add(new ChatGPTRequest.Message("system", systemMessage)); messages.add(new ChatGPTRequest.Message("system", systemMessage));
var context = contextMessages.stream() var context = contextMessages.stream()
@ -32,4 +33,26 @@ public class ChatGPTRequestBuilder {
0.7f 0.7f
); );
} }
public ChatGPTRequest contextRequest(List<PreviousInteraction> contextMessages, String message, String systemMessage) {
List<ChatGPTRequest.Message> messages = new ArrayList<>();
messages.add(new ChatGPTRequest.Message("system", systemMessage));
var context = contextMessages.stream()
.map(m -> List.of(
new ChatGPTRequest.Message("user", m.question),
new ChatGPTRequest.Message("assistant", m.answer)
))
.flatMap(List::stream)
.toList();
messages.addAll(context);
messages.add(new ChatGPTRequest.Message("user", message));
return new ChatGPTRequest(
model,
messages,
0.7f
);
}
public record PreviousInteraction(String question, String answer) {
}
} }

View file

@ -29,7 +29,7 @@ public class ChatGPTService {
.POST(HttpRequest.BodyPublishers.ofByteArray(data)) .POST(HttpRequest.BodyPublishers.ofByteArray(data))
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer " + this.apiKey) .setHeader("Authorization", "Bearer " + this.apiKey)
.timeout(Duration.ofSeconds(90)) .timeout(Duration.ofMinutes(5))
.build(); .build();
var responseStream = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); var responseStream = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());

View file

@ -17,11 +17,11 @@
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-annotations</artifactId>
<version>2.15.0-rc2</version>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -5,6 +5,7 @@ import de.hhhammer.dchat.db.models.server.ServerMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.*; import java.sql.*;
import java.time.Instant; import java.time.Instant;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
@ -14,22 +15,17 @@ import java.util.stream.StreamSupport;
public class ServerDBService { public class ServerDBService {
private static final Logger logger = LoggerFactory.getLogger(ServerDBService.class); private static final Logger logger = LoggerFactory.getLogger(ServerDBService.class);
private final String jdbcConnectionString; private final DataSource dataSource;
private final String username;
private final String password;
public ServerDBService(String jdbcConnectionString, String username, String password) { public ServerDBService(DataSource dataSource) {
this.jdbcConnectionString = jdbcConnectionString; this.dataSource = dataSource;
this.username = username;
this.password = password;
} }
public Optional<ServerConfig> getConfig(String serverId) { public Optional<ServerConfig> getConfig(String serverId) {
var getServerConfig = """ var getServerConfig = """
SELECT * FROM server_configs WHERE server_id = ? SELECT * FROM server_configs WHERE server_id = ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, serverId); pstmt.setString(1, serverId);
@ -49,7 +45,7 @@ public class ServerDBService {
var getAllowedServerSql = """ var getAllowedServerSql = """
SELECT * FROM server_configs SELECT * FROM server_configs
"""; """;
try (Connection con = DriverManager.getConnection(this.jdbcConnectionString, this.username, this.password); try (Connection con = dataSource.getConnection();
PreparedStatement pstmt = con.prepareStatement(getAllowedServerSql) PreparedStatement pstmt = con.prepareStatement(getAllowedServerSql)
) { ) {
ResultSet resultSet = pstmt.executeQuery(); ResultSet resultSet = pstmt.executeQuery();
@ -66,8 +62,7 @@ public class ServerDBService {
var getServerConfig = """ var getServerConfig = """
SELECT * FROM server_configs WHERE id = ? SELECT * FROM server_configs WHERE id = ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setLong(1, id); pstmt.setLong(1, id);
@ -85,8 +80,7 @@ public class ServerDBService {
var getServerConfig = """ var getServerConfig = """
INSERT INTO server_configs (server_id, system_message, rate_limit) VALUES (?,?,?) INSERT INTO server_configs (server_id, system_message, rate_limit) VALUES (?,?,?)
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, newServerConfig.serverId()); pstmt.setString(1, newServerConfig.serverId());
@ -105,8 +99,7 @@ public class ServerDBService {
var getServerConfig = """ var getServerConfig = """
UPDATE server_configs SET system_message = ?, rate_limit = ?, server_id = ? WHERE id = ? UPDATE server_configs SET system_message = ?, rate_limit = ?, server_id = ? WHERE id = ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, newServerConfig.systemMessage()); pstmt.setString(1, newServerConfig.systemMessage());
@ -126,8 +119,7 @@ public class ServerDBService {
var getServerConfig = """ var getServerConfig = """
DELETE FROM server_configs WHERE id = ? DELETE FROM server_configs WHERE id = ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setLong(1, id); pstmt.setLong(1, id);
@ -144,8 +136,7 @@ public class ServerDBService {
var getServerConfig = """ var getServerConfig = """
SELECT count(*) FROM server_messages WHERE server_id = ? AND time <= ? and time >= ? SELECT count(*) FROM server_messages WHERE server_id = ? AND time <= ? and time >= ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, serverId); pstmt.setString(1, serverId);
@ -168,8 +159,7 @@ public class ServerDBService {
var getServerConfig = """ var getServerConfig = """
INSERT INTO server_messages (server_id, user_id, tokens) VALUES (?,?,?) INSERT INTO server_messages (server_id, user_id, tokens) VALUES (?,?,?)
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, serverMessage.serverId()); pstmt.setString(1, serverMessage.serverId());
@ -188,8 +178,7 @@ public class ServerDBService {
var countTokensOfLast30Days = """ var countTokensOfLast30Days = """
SELECT sum(tokens) FROM server_messages WHERE server_id = ? AND time < ? AND time >= ? SELECT sum(tokens) FROM server_messages WHERE server_id = ? AND time < ? AND time >= ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(countTokensOfLast30Days) PreparedStatement pstmt = con.prepareStatement(countTokensOfLast30Days)
) { ) {
pstmt.setString(1, serverId); pstmt.setString(1, serverId);

View file

@ -5,6 +5,7 @@ import de.hhhammer.dchat.db.models.user.UserMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.sql.*; import java.sql.*;
import java.time.Instant; import java.time.Instant;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
@ -14,22 +15,17 @@ import java.util.stream.StreamSupport;
public class UserDBService { public class UserDBService {
private static final Logger logger = LoggerFactory.getLogger(UserDBService.class); private static final Logger logger = LoggerFactory.getLogger(UserDBService.class);
private final String jdbcConnectionString; private final DataSource dataSource;
private final String username;
private final String password;
public UserDBService(String jdbcConnectionString, String username, String password) { public UserDBService(DataSource dataSource) {
this.jdbcConnectionString = jdbcConnectionString; this.dataSource = dataSource;
this.username = username;
this.password = password;
} }
public Optional<UserConfig> getConfig(String userId) { public Optional<UserConfig> getConfig(String userId) {
var getServerConfig = """ var getServerConfig = """
SELECT * FROM user_configs WHERE user_id = ? SELECT * FROM user_configs WHERE user_id = ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, userId); pstmt.setString(1, userId);
@ -49,8 +45,7 @@ public class UserDBService {
var getServerConfig = """ var getServerConfig = """
SELECT * FROM user_configs WHERE id = ? SELECT * FROM user_configs WHERE id = ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setLong(1, id); pstmt.setLong(1, id);
@ -68,8 +63,7 @@ public class UserDBService {
var getServerConfig = """ var getServerConfig = """
SELECT * FROM user_configs SELECT * FROM user_configs
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
ResultSet resultSet = pstmt.executeQuery(); ResultSet resultSet = pstmt.executeQuery();
@ -86,8 +80,7 @@ public class UserDBService {
var getServerConfig = """ var getServerConfig = """
INSERT INTO user_configs (user_id, system_message, context_length, rate_limit) VALUES (?,?,?,?) INSERT INTO user_configs (user_id, system_message, context_length, rate_limit) VALUES (?,?,?,?)
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, newUserConfig.userId()); pstmt.setString(1, newUserConfig.userId());
@ -107,8 +100,7 @@ public class UserDBService {
var getServerConfig = """ var getServerConfig = """
UPDATE user_configs SET system_message = ?, context_length = ?, rate_limit = ?, user_id = ? WHERE id = ? UPDATE user_configs SET system_message = ?, context_length = ?, rate_limit = ?, user_id = ? WHERE id = ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, newUserConfig.systemMessage()); pstmt.setString(1, newUserConfig.systemMessage());
@ -129,8 +121,7 @@ public class UserDBService {
var getServerConfig = """ var getServerConfig = """
DELETE FROM user_configs WHERE id = ? DELETE FROM user_configs WHERE id = ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setLong(1, id); pstmt.setLong(1, id);
@ -147,8 +138,7 @@ public class UserDBService {
var getServerConfig = """ var getServerConfig = """
SELECT count(*) FROM user_messages WHERE user_id = ? AND time <= ? and time >= ? SELECT count(*) FROM user_messages WHERE user_id = ? AND time <= ? and time >= ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, userId); pstmt.setString(1, userId);
@ -171,8 +161,7 @@ public class UserDBService {
var getServerConfig = """ var getServerConfig = """
INSERT INTO user_messages (user_id, question, answer, tokens) VALUES (?,?,?,?) INSERT INTO user_messages (user_id, question, answer, tokens) VALUES (?,?,?,?)
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(getServerConfig) PreparedStatement pstmt = con.prepareStatement(getServerConfig)
) { ) {
pstmt.setString(1, newUserMessage.userId()); pstmt.setString(1, newUserMessage.userId());
@ -188,12 +177,31 @@ public class UserDBService {
} }
} }
public List<UserMessage> getLastMessages(String userId, int limit) {
var getLastMessages = """
SELECT * FROM user_messages WHERE user_id = ? ORDER BY time DESC LIMIT ?
""";
try (Connection con = dataSource.getConnection();
PreparedStatement pstmt = con.prepareStatement(getLastMessages)
) {
pstmt.setString(1, userId);
pstmt.setInt(2, limit);
ResultSet resultSet = pstmt.executeQuery();
Iterable<UserMessage> iterable = () -> new ResultSetIterator<>(resultSet, new UserMessage.UserMessageResultSetTransformer());
return StreamSupport.stream(iterable.spliterator(), false).toList();
} catch (SQLException e) {
logger.error("Fetching last messages for user whit id: " + userId, e);
} catch (ResultSetIteratorException e) {
logger.error("Iterating over messages ResultSet from user with id: " + userId, e);
}
return List.of();
}
public long tokensOfLast30Days(String userId) { public long tokensOfLast30Days(String userId) {
var countTokensOfLast30Days = """ var countTokensOfLast30Days = """
SELECT sum(tokens) FROM user_messages WHERE user_id = ? AND time < ? AND time >= ? SELECT sum(tokens) FROM user_messages WHERE user_id = ? AND time < ? AND time >= ?
"""; """;
try (Connection con = DriverManager try (Connection con = dataSource.getConnection();
.getConnection(this.jdbcConnectionString, this.username, this.password);
PreparedStatement pstmt = con.prepareStatement(countTokensOfLast30Days) PreparedStatement pstmt = con.prepareStatement(countTokensOfLast30Days)
) { ) {
pstmt.setString(1, userId); pstmt.setString(1, userId);

View file

@ -16,6 +16,7 @@
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency> </dependency>
</dependencies> </dependencies>

38
pom.xml
View file

@ -30,6 +30,7 @@
<maven.compiler.source>19</maven.compiler.source> <maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target> <maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson.version>2.15.1</jackson.version>
</properties> </properties>
@ -75,6 +76,43 @@
<version>42.6.0</version> <version>42.6.0</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.javacord</groupId>
<artifactId>javacord</artifactId>
<version>3.8.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.20.0</version>
<scope>runtime</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<build> <build>

View file

@ -22,20 +22,21 @@
<artifactId>db</artifactId> <artifactId>db</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency> <dependency>
<groupId>io.javalin</groupId> <groupId>io.javalin</groupId>
<artifactId>javalin</artifactId> <artifactId>javalin</artifactId>
<version>5.5.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.15.0-rc2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.datatype</groupId> <groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId> <artifactId>jackson-datatype-jsr310</artifactId>
<version>2.14.2</version>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -1,5 +1,7 @@
package de.hhhammer.dchat.web; package de.hhhammer.dchat.web;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import de.hhhammer.dchat.db.ServerDBService; import de.hhhammer.dchat.db.ServerDBService;
import de.hhhammer.dchat.db.UserDBService; import de.hhhammer.dchat.db.UserDBService;
import io.javalin.Javalin; import io.javalin.Javalin;
@ -21,12 +23,23 @@ public class App {
System.exit(1); System.exit(1);
} }
var serverDBService = new ServerDBService(postgresUrl, postgresUser, postgresPassword);
var userDBService = new UserDBService(postgresUrl, postgresUser, postgresPassword);
String apiPortStr = System.getenv("API_PORT") != null ? System.getenv("API_PORT") : "8080"; String apiPortStr = System.getenv("API_PORT") != null ? System.getenv("API_PORT") : "8080";
int apiPort = Integer.parseInt(apiPortStr); int apiPort = Integer.parseInt(apiPortStr);
var webApi = new WebAPI(serverDBService, userDBService, apiPort);
webApi.run(); var config = new HikariConfig();
config.setJdbcUrl(postgresUrl);
config.setUsername(postgresUser);
config.setPassword(postgresPassword);
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
try (var ds = new HikariDataSource(config)) {
var serverDBService = new ServerDBService(ds);
var userDBService = new UserDBService(ds);
var webApi = new WebAPI(serverDBService, userDBService, apiPort);
webApi.run();
}
} }
} }