mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-06 16:26:31 +00:00
added templating support
changed ping command to use templates added userjoin/userleave event added guild to command context refactored post target service added logging output to initial loading added server_id to postTarget added leave log postTarget
This commit is contained in:
21
abstracto-application/templating/pom.xml
Normal file
21
abstracto-application/templating/pom.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>abstracto-application</artifactId>
|
||||
<groupId>dev.sheldan.abstracto</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>dev.sheldan.abstracto.templating</groupId>
|
||||
<artifactId>templating</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>templating-interface</module>
|
||||
<module>templating-impl</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
31
abstracto-application/templating/templating-impl/pom.xml
Normal file
31
abstracto-application/templating/templating-impl/pom.xml
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>templating</artifactId>
|
||||
<groupId>dev.sheldan.abstracto.templating</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>templating-impl</artifactId>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>dev.sheldan.abstracto.templating</groupId>
|
||||
<artifactId>templating-interface</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,24 @@
|
||||
package dev.sheldan.abstracto.templating.config;
|
||||
|
||||
import dev.sheldan.abstracto.templating.loading.DatabaseTemplateLoader;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@org.springframework.context.annotation.Configuration
|
||||
public class FreemarkerConfiguration {
|
||||
|
||||
@Autowired
|
||||
private DatabaseTemplateLoader templateLoader;
|
||||
|
||||
@Bean
|
||||
public Configuration freeMarkerConfiguration() throws IOException, TemplateException {
|
||||
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
|
||||
factory.setPreTemplateLoaders(templateLoader);
|
||||
return factory.createConfiguration();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package dev.sheldan.abstracto.templating.loading;
|
||||
|
||||
import dev.sheldan.abstracto.templating.TemplateDto;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import freemarker.cache.TemplateLoader;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
@Component
|
||||
public class DatabaseTemplateLoader implements TemplateLoader {
|
||||
|
||||
@Autowired
|
||||
private TemplateService templateService;
|
||||
|
||||
@Override
|
||||
public Object findTemplateSource(String s) throws IOException {
|
||||
TemplateDto templateDtoByKey = templateService.getTemplateByKey(s);
|
||||
if(templateDtoByKey != null){
|
||||
return templateDtoByKey;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastModified(Object o) {
|
||||
TemplateDto casted = (TemplateDto) o;
|
||||
TemplateDto templateDtoByKey = templateService.getTemplateByKey(casted.getKey());
|
||||
if(templateDtoByKey != null){
|
||||
return templateDtoByKey.getLastModified().getEpochSecond();
|
||||
} else {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader getReader(Object o, String s) throws IOException {
|
||||
return new StringReader(((TemplateDto) o).getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeTemplateSource(Object o) throws IOException {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package dev.sheldan.abstracto.templating.loading;
|
||||
|
||||
import dev.sheldan.abstracto.templating.TemplateDto;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface TemplateRepository extends JpaRepository<TemplateDto, String> {
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package dev.sheldan.abstracto.templating.loading;
|
||||
|
||||
import dev.sheldan.abstracto.templating.TemplateDto;
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Component
|
||||
public class TemplateServiceBean implements TemplateService {
|
||||
|
||||
@Autowired
|
||||
private TemplateRepository repository;
|
||||
|
||||
@Autowired
|
||||
private Configuration configuration;
|
||||
|
||||
@Override
|
||||
@Cacheable("template")
|
||||
public TemplateDto getTemplateByKey(String key) {
|
||||
return repository.getOne(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String renderTemplate(TemplateDto templateDto) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String renderTemplate(String key, HashMap<String, Object> parameters) {
|
||||
try {
|
||||
return FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(key), parameters);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (TemplateException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTemplate(String key, String content) {
|
||||
repository.save(TemplateDto.builder().key(key).content(content).lastModified(Instant.now()).build());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package dev.sheldan.abstracto.templating.seeddata;
|
||||
|
||||
import dev.sheldan.abstracto.templating.TemplateService;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class TemplateSeedDataLoader {
|
||||
|
||||
@Value("classpath*:**/templates/**/*.ftl")
|
||||
private Resource[] resources;
|
||||
|
||||
@Autowired
|
||||
private TemplateService service;
|
||||
|
||||
@EventListener
|
||||
public void handleContextRefreshEvent(ContextRefreshedEvent ctxStartEvt) {
|
||||
List<Resource> templatesToLoad = Arrays.asList(resources);
|
||||
templatesToLoad.forEach(resource -> {
|
||||
try {
|
||||
String templateKey = FilenameUtils.getBaseName(resource.getFilename());
|
||||
String templateContent = IOUtils.toString(resource.getURI(), Charset.defaultCharset());
|
||||
service.createTemplate(templateKey, templateContent);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>dev.sheldan.abstracto.templating</groupId>
|
||||
<artifactId>templating</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>templating-interface</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
package dev.sheldan.abstracto.templating;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.time.Instant;
|
||||
|
||||
@Builder
|
||||
@Entity
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "template")
|
||||
public class TemplateDto {
|
||||
|
||||
@Id
|
||||
@Getter
|
||||
private String key;
|
||||
|
||||
@Getter
|
||||
private String content;
|
||||
|
||||
@Getter
|
||||
private String section;
|
||||
|
||||
@Getter
|
||||
private Instant lastModified;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dev.sheldan.abstracto.templating;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public interface TemplateService {
|
||||
TemplateDto getTemplateByKey(String key);
|
||||
String renderTemplate(TemplateDto templateDto);
|
||||
String renderTemplate(String key, HashMap<String, Object> parameters);
|
||||
void createTemplate(String key, String content);
|
||||
}
|
||||
Reference in New Issue
Block a user