Added backend and fixed openapi generation
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
plugins {
|
||||
java
|
||||
id("org.springframework.boot") version "3.4.4"
|
||||
id("io.spring.dependency-management") version "1.1.7"
|
||||
}
|
||||
|
||||
group = "com.alttd.altitudeweb"
|
||||
version = "0.0.1-SNAPSHOT"
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
compileOnly {
|
||||
extendsFrom(configurations.annotationProcessor.get())
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":open_api"))
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
compileOnly("org.projectlombok:lombok")
|
||||
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
|
||||
annotationProcessor("org.projectlombok:lombok")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
implementation("org.mybatis:mybatis:3.5.13")
|
||||
implementation("org.springframework.boot:spring-boot-configuration-processor")
|
||||
}
|
||||
|
||||
tasks.withType<Test> {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.alttd.altitudeweb;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AltitudeWebApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AltitudeWebApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.alttd.altitudeweb.controllers;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class CorsConfig implements WebMvcConfigurer {
|
||||
|
||||
@Value("${cors.allowed-origins}")
|
||||
private String[] allowedOrigins;
|
||||
|
||||
public CorsConfig() {
|
||||
log.info("test");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins(allowedOrigins)
|
||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("*")
|
||||
.allowCredentials(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.alttd.altitudeweb.controllers;
|
||||
|
||||
import com.alttd.altitudeweb.api.TeamApi;
|
||||
import com.alttd.altitudeweb.model.TeamMemberDto;
|
||||
import com.alttd.altitudeweb.model.TeamMembersDto;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class TeamApiController implements TeamApi {
|
||||
|
||||
@Override
|
||||
public ResponseEntity<TeamMembersDto> getTeamMembers(String group) {
|
||||
TeamMembersDto teamMemberDtos = new TeamMembersDto();
|
||||
teamMemberDtos.add(new TeamMemberDto("test", "good"));
|
||||
return ResponseEntity.ok().body(teamMemberDtos);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.alttd.altitudeweb.database;
|
||||
|
||||
import com.alttd.altitudeweb.database.web_db.DatabaseSettings;
|
||||
import com.alttd.altitudeweb.database.web_db.SettingsMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.datasource.pooled.PooledDataSource;
|
||||
import org.apache.ibatis.mapping.Environment;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Slf4j
|
||||
public class Connection {
|
||||
|
||||
private static final HashMap<Databases, Connection> connections = new HashMap<>();
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
private final DatabaseSettings settings;
|
||||
private final AddMappers addMappers;
|
||||
|
||||
private Connection(DatabaseSettings settings, AddMappers addMappers) {
|
||||
this.settings = settings;
|
||||
this.addMappers = addMappers;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AddMappers {
|
||||
void apply(Configuration configuration);
|
||||
}
|
||||
|
||||
public static CompletableFuture<Connection> getConnection(Databases database, AddMappers addMappers) {
|
||||
if (connections.containsKey(database)) {
|
||||
return CompletableFuture.completedFuture(connections.get(database));
|
||||
}
|
||||
if (database == Databases.DEFAULT) {
|
||||
return loadDefaultDatabase(addMappers);
|
||||
}
|
||||
CompletableFuture<DatabaseSettings> settingsFuture = new CompletableFuture<>();
|
||||
getConnection(Databases.DEFAULT, (mapper -> mapper.addMapper(DatabaseSettings.class))).thenApply(connection -> {
|
||||
connection.runQuery(session -> {
|
||||
DatabaseSettings loadedSettings = session.getMapper(SettingsMapper.class).getSettings(database.getInternalName());
|
||||
settingsFuture.complete(loadedSettings);
|
||||
});
|
||||
return null;
|
||||
});
|
||||
return settingsFuture.thenApply(loadedSettings -> {
|
||||
Connection connection = new Connection(loadedSettings, addMappers);
|
||||
connections.put(database, connection);
|
||||
return connection;
|
||||
});
|
||||
}
|
||||
|
||||
private static CompletableFuture<Connection> loadDefaultDatabase(AddMappers addMappers) {
|
||||
DatabaseSettings databaseSettings = new DatabaseSettings(
|
||||
System.getenv("DB_HOST"),
|
||||
Integer.parseInt(System.getenv("DB_PORT")),
|
||||
System.getenv("DB_NAME"),
|
||||
System.getenv("DB_USER"),
|
||||
System.getenv("DB_PASS")
|
||||
);
|
||||
Connection connection = new Connection(databaseSettings, addMappers);
|
||||
return CompletableFuture.completedFuture(connection);
|
||||
}
|
||||
|
||||
public void runQuery(Consumer<SqlSession> consumer) {
|
||||
new Thread(() -> {
|
||||
if (sqlSessionFactory == null) {
|
||||
sqlSessionFactory = createSqlSessionFactory(settings, addMappers);
|
||||
}
|
||||
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
consumer.accept(session);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to run discord query", e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private SqlSessionFactory createSqlSessionFactory(DatabaseSettings settings, AddMappers addMappers) {
|
||||
PooledDataSource dataSource = new PooledDataSource();
|
||||
dataSource.setDriver("com.mysql.cj.jdbc.Driver");
|
||||
dataSource.setUrl(String.format("jdbc:mysql://%s:%d/%s", settings.host(),
|
||||
settings.port(), settings.name()));
|
||||
dataSource.setUsername(settings.username());
|
||||
dataSource.setPassword(settings.password());
|
||||
Environment environment = new Environment("production", new JdbcTransactionFactory(), dataSource);
|
||||
Configuration configuration = new Configuration(environment);
|
||||
addMappers.apply(configuration);
|
||||
|
||||
return new SqlSessionFactoryBuilder().build(configuration);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.alttd.altitudeweb.database;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum Databases {
|
||||
DEFAULT("web_db"),
|
||||
LUCK_PERMS("luckperms");
|
||||
|
||||
private final String internalName;
|
||||
|
||||
Databases(String internalName) {
|
||||
this.internalName = internalName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.alttd.altitudeweb.database.luckperms;
|
||||
|
||||
public record PlayerGroup(String username, String groupName) {}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.alttd.altitudeweb.database.luckperms;
|
||||
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TeamMemberMapper {
|
||||
@Select("""
|
||||
SELECT players.username, #{groupName} AS group_name
|
||||
FROM luckperms_user_permissions AS permissions
|
||||
INNER JOIN luckperms_players AS players ON players.uuid = permissions.uuid
|
||||
WHERE permission = 'group.'#{groupName}""")
|
||||
List<PlayerGroup> getTeamMembers(String groupName);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.altitudeweb.database.web_db;
|
||||
|
||||
public record DatabaseSettings(String host, int port, String name, String username, String password) {
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.alttd.altitudeweb.database.web_db;
|
||||
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
public interface SettingsMapper {
|
||||
@Select("SELECT * FROM database_settings WHERE name = #{database}")
|
||||
DatabaseSettings getSettings(String database);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
spring.application.name=AltitudeWeb
|
||||
database.name=${DB_NAME:web_db}
|
||||
database.port=${DB_PORT:3306}
|
||||
database.host=${DB_HOST:localhost}
|
||||
database.user=${DB_USER:root}
|
||||
database.password=${DB_PASSWORD:root}
|
||||
cors.allowed-origins=${CORS:http://localhost:4200}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.alttd.altitudeweb;
|
||||
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.springframework.boot.test.context.SpringBootTest;
|
||||
//
|
||||
//@SpringBootTest
|
||||
//class AltitudeWebApplicationTests {
|
||||
//
|
||||
// @Test
|
||||
// void contextLoads() {
|
||||
// }
|
||||
//
|
||||
//}
|
||||
Reference in New Issue
Block a user