mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-13 19:41:38 +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:
@@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user