|
1 | 1 | package run.halo.comment.widget; |
2 | 2 |
|
3 | | -import static run.halo.app.extension.index.query.QueryFactory.equal; |
4 | | - |
5 | | -import java.util.function.Function; |
6 | | -import com.google.common.cache.Cache; |
7 | | -import com.google.common.cache.CacheBuilder; |
8 | 3 | import lombok.RequiredArgsConstructor; |
9 | 4 | import org.springframework.stereotype.Component; |
10 | 5 | import reactor.core.publisher.Mono; |
11 | | -import run.halo.app.extension.ConfigMap; |
12 | | -import run.halo.app.extension.DefaultExtensionMatcher; |
13 | | -import run.halo.app.extension.ExtensionClient; |
14 | | -import run.halo.app.extension.controller.Controller; |
15 | | -import run.halo.app.extension.controller.ControllerBuilder; |
16 | | -import run.halo.app.extension.controller.Reconciler; |
17 | | -import run.halo.app.extension.router.selector.FieldSelector; |
18 | 6 | import run.halo.app.plugin.ReactiveSettingFetcher; |
19 | 7 |
|
20 | 8 | @Component |
21 | 9 | @RequiredArgsConstructor |
22 | 10 | public class SettingConfigGetterImpl implements SettingConfigGetter { |
23 | 11 | private final ReactiveSettingFetcher settingFetcher; |
24 | | - private final SettingConfigCache settingConfigCache; |
25 | 12 |
|
26 | 13 | @Override |
27 | | - public Mono<SecurityConfig> getSecurityConfig() { |
28 | | - return settingConfigCache.get("security", |
29 | | - key -> settingFetcher.fetch("security", SecurityConfig.class) |
30 | | - .defaultIfEmpty(SecurityConfig.empty()) |
31 | | - ); |
| 14 | + public Mono<BasicConfig> getBasicConfig() { |
| 15 | + return settingFetcher.fetch(BasicConfig.GROUP, BasicConfig.class) |
| 16 | + .defaultIfEmpty(new BasicConfig()); |
32 | 17 | } |
33 | 18 |
|
34 | | - interface SettingConfigCache { |
35 | | - <T> Mono<T> get(String key, Function<String, Mono<T>> loader); |
| 19 | + @Override |
| 20 | + public Mono<AvatarConfig> getAvatarConfig() { |
| 21 | + return settingFetcher.fetch(AvatarConfig.GROUP, AvatarConfig.class) |
| 22 | + .defaultIfEmpty(new AvatarConfig()); |
36 | 23 | } |
37 | 24 |
|
38 | | - @Component |
39 | | - @RequiredArgsConstructor |
40 | | - static class SettingConfigCacheImpl implements Reconciler<Reconciler.Request>, SettingConfigCache { |
41 | | - private static final String CONFIG_NAME = "plugin-comment-widget-configmap"; |
42 | | - |
43 | | - private final Cache<String, Object> cache = CacheBuilder.newBuilder() |
44 | | - .maximumSize(10) |
45 | | - .build(); |
46 | | - |
47 | | - private final ExtensionClient client; |
48 | | - |
49 | | - @SuppressWarnings("unchecked") |
50 | | - public <T> Mono<T> get(String key, Function<String, Mono<T>> loader) { |
51 | | - return Mono.justOrEmpty(cache.getIfPresent(key)) |
52 | | - .switchIfEmpty(loader.apply(key).doOnNext(value -> cache.put(key, value))) |
53 | | - .map(object -> (T) object); |
54 | | - } |
55 | | - |
56 | | - @Override |
57 | | - public Result reconcile(Request request) { |
58 | | - cache.invalidateAll(); |
59 | | - return Result.doNotRetry(); |
60 | | - } |
61 | | - |
62 | | - @Override |
63 | | - public Controller setupWith(ControllerBuilder builder) { |
64 | | - var extension = new ConfigMap(); |
65 | | - var extensionMatcher = DefaultExtensionMatcher.builder(client, extension.groupVersionKind()) |
66 | | - .fieldSelector(FieldSelector.of(equal("metadata.name", CONFIG_NAME))) |
67 | | - .build(); |
68 | | - return builder |
69 | | - .extension(extension) |
70 | | - .syncAllOnStart(false) |
71 | | - .onAddMatcher(extensionMatcher) |
72 | | - .onUpdateMatcher(extensionMatcher) |
73 | | - .build(); |
74 | | - } |
| 25 | + @Override |
| 26 | + public Mono<SecurityConfig> getSecurityConfig() { |
| 27 | + return settingFetcher.fetch(SecurityConfig.GROUP, SecurityConfig.class) |
| 28 | + .defaultIfEmpty(SecurityConfig.empty()); |
75 | 29 | } |
76 | 30 | } |
0 commit comments