Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multi theme support #449

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "server/src/main/resources/templates/theme/simple"]
path = server/src/main/resources/templates/theme/simple
url = https://github.com/ikaros-dev/ikaros-theme-simple.git
17 changes: 14 additions & 3 deletions BUILD.MD
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# 文档状态
WIP
# 编译环境

- JDK: 17
- Gradle: 8.1
- SpringBoot: 3.0.1
- CheckStyle: 9.3
- Vue: 3
- IDE: IntelliJ IDEA

执行gradle任务前,默认会走一遍单元测试, 如需跳过,加上`-x test` 即可。

Expand Down Expand Up @@ -61,4 +60,16 @@ commit之前,用checkstyle检查下代码,确认没有问题后再commit。
最后OK保存

## 本地开发
在`IkarosApplication`的运行配置里,将`Active profiles` 配置成:`dev,win`
在`IkarosApplication`的运行配置里,将`Active profiles` 配置成:`dev,win`

## IDEA格式化配置

1. 打开 `Setting` => `Editor` => `Code Style` => `Java`
2. 选择 `Scheme` => 选择 `Project`
3. 点击右边小齿轮 => `Import Scheme` => `Checkstyle configuration`
4. 选择项目目录 `config/checkstyle` 下的 配置文件`checkstyle.xml` 保存导入
5. 保存设置

# 更多
WIP
<https://docs.ikaros.run>
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

# 0.7.5

## 特功能

- web端多主题支持 #450

## 国际化

- git仓库README国际化 #436
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public enum SubjectType {
MUSIC,
NOVEL,
REAL,
OTHER;
OTHER

}
17 changes: 16 additions & 1 deletion server/src/main/java/run/ikaros/server/config/WebFluxConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.codec.CodecConfigurer;
import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.lang.NonNull;
import org.springframework.util.ResourceUtils;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.function.BodyInserters;
Expand Down Expand Up @@ -107,7 +111,6 @@ private Mono<ServerResponse> serveConsoleIndex(ServerRequest request) {
}
}


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
var importRoot = ikarosProperties.getWorkDir().resolve(FileConst.DEFAULT_DIR_NAME);
Expand All @@ -132,5 +135,17 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/templates/static/");

// Register theme static files path
try {
File themeRootDir = ResourceUtils.getFile("classpath:templates/theme");
for (File themeDir : Objects.requireNonNull(themeRootDir.listFiles())) {
String theme = themeDir.getName();
registry.addResourceHandler("/static/" + theme + "/**")
.addResourceLocations("classpath:/templates/theme/" + theme + "/static/");
}
} catch (FileNotFoundException e) {
throw new RuntimeException("Not exists theme dir in classpath.", e);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package run.ikaros.server.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import reactor.core.publisher.Mono;
import run.ikaros.server.theme.ThemeService;

@Controller
public class IndexController {
private final ThemeService themeService;

public IndexController(ThemeService themeService) {
this.themeService = themeService;
}

@RequestMapping("/")
public Mono<String> index(Model model) {
return themeService.getCurrentTheme()
.map(theme -> "/theme/" + theme + "/index");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
import reactor.core.publisher.Mono;
import run.ikaros.api.wrap.PagingWrap;
import run.ikaros.server.core.subject.service.SubjectService;
import run.ikaros.server.theme.ThemeService;

@Controller
@RequestMapping("/subject")
public class SubjectController {

private final SubjectService subjectService;
private final ThemeService themeService;

public SubjectController(SubjectService subjectService) {
public SubjectController(SubjectService subjectService, ThemeService themeService) {
this.subjectService = subjectService;
this.themeService = themeService;
}


/**
* Subject list page.
*/
Expand All @@ -27,7 +31,8 @@ public Mono<String> index(Model model) {
return subjectService.findAllByPageable(new PagingWrap<>(1, 100, 0, null))
.map(PagingWrap::getItems)
.map(subs -> model.addAttribute("subjects", subs))
.thenReturn("subjects");
.then(themeService.getCurrentTheme())
.map(theme -> "/theme/" + theme + "/" + "subjects");
}

/**
Expand All @@ -37,6 +42,7 @@ public Mono<String> index(Model model) {
public Mono<String> findById(@PathVariable("id") Long id, Model model) {
return subjectService.findById(id)
.map(subject -> model.addAttribute("subject", subject))
.thenReturn("subject-details");
.then(themeService.getCurrentTheme())
.map(theme -> "/theme/" + theme + "/" + "subject-details");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run.ikaros.server.core.setting;

import java.util.Map;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
Expand All @@ -10,21 +11,20 @@
import run.ikaros.api.custom.ReactiveCustomClient;
import run.ikaros.api.infra.exception.NotFoundException;
import run.ikaros.server.custom.scheme.SchemeInitializedEvent;
import run.ikaros.server.infra.constants.SettingKeyConst;
import run.ikaros.server.infra.constants.ThemeConst;

@Slf4j
@Component
public class SystemSettingInitListener {
private final ReactiveCustomClient reactiveCustomClient;
@Getter
private static final String configMapName = "setting.server.ikaros.run";

public SystemSettingInitListener(ReactiveCustomClient reactiveCustomClient) {
this.reactiveCustomClient = reactiveCustomClient;
}

public static String getConfigMapName() {
return configMapName;
}

/**
* Init add system default config items.
*/
Expand Down Expand Up @@ -60,6 +60,9 @@ public Mono<Void> handle(SchemeInitializedEvent event) {
// System remote settings
settingConfigMap.putDataItem("REMOTE_ENABLE", "false");

// System web theme settings
settingConfigMap.putDataItem(SettingKeyConst.THEME_SELECT, ThemeConst.DEFAULT);

return reactiveCustomClient.findOne(ConfigMap.class, configMapName)
.onErrorResume(NotFoundException.class, e ->
reactiveCustomClient.create(settingConfigMap)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package run.ikaros.server.infra.constants;

public interface SettingKeyConst {
String THEME_SELECT = "THEME_SELECT";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package run.ikaros.server.infra.constants;

public interface ThemeConst {
String SIMPLE = "simple";
String DEFAULT = SIMPLE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package run.ikaros.server.theme;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import run.ikaros.api.core.setting.ConfigMap;
import run.ikaros.api.custom.ReactiveCustomClient;
import run.ikaros.server.core.setting.SystemSettingInitListener;
import run.ikaros.server.infra.constants.SettingKeyConst;
import run.ikaros.server.infra.constants.ThemeConst;

@Slf4j
@Service
public class DefaultThemeService implements ThemeService {
private final ReactiveCustomClient reactiveCustomClient;

public DefaultThemeService(ReactiveCustomClient reactiveCustomClient) {
this.reactiveCustomClient = reactiveCustomClient;
}

@Override
public Mono<String> getCurrentTheme() {
return reactiveCustomClient.findOne(ConfigMap.class,
SystemSettingInitListener.getConfigMapName())
.map(ConfigMap::getData)
.map(map -> map.getOrDefault(SettingKeyConst.THEME_SELECT, ThemeConst.DEFAULT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package run.ikaros.server.theme;

import reactor.core.publisher.Mono;

public interface ThemeService {
Mono<String> getCurrentTheme();
}
15 changes: 0 additions & 15 deletions server/src/main/resources/templates/error/4xx.html

This file was deleted.

15 changes: 0 additions & 15 deletions server/src/main/resources/templates/error/5xx.html

This file was deleted.

20 changes: 0 additions & 20 deletions server/src/main/resources/templates/index.html

This file was deleted.

Binary file not shown.