办学质量监测教学评价系统
ageer
2024-05-17 7fe89a931b291196b0b571c668bd3758309019d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package spring.config;
 
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ReflectUtil;
import com.xmzs.midjourney.ProxyProperties;
import com.xmzs.midjourney.loadbalancer.rule.IRule;
import com.xmzs.midjourney.service.NotifyService;
import com.xmzs.midjourney.service.TaskStoreService;
import com.xmzs.midjourney.service.TranslateService;
import com.xmzs.midjourney.service.store.InMemoryTaskStoreServiceImpl;
import com.xmzs.midjourney.service.store.RedisTaskStoreServiceImpl;
import com.xmzs.midjourney.service.translate.BaiduTranslateServiceImpl;
import com.xmzs.midjourney.service.translate.GPTTranslateServiceImpl;
import com.xmzs.midjourney.service.translate.NoTranslateServiceImpl;
import com.xmzs.midjourney.support.DiscordAccountHelper;
import com.xmzs.midjourney.support.DiscordHelper;
import com.xmzs.midjourney.support.Task;
import com.xmzs.midjourney.wss.handle.MessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.client.RestTemplate;
 
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Configuration
public class BeanConfig {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ProxyProperties properties;
 
    @Bean
    TranslateService translateService() {
        return switch (this.properties.getTranslateWay()) {
            case BAIDU -> new BaiduTranslateServiceImpl(this.properties.getBaiduTranslate());
            case GPT -> new GPTTranslateServiceImpl(this.properties);
            default -> new NoTranslateServiceImpl();
        };
    }
 
    @Bean
    TaskStoreService taskStoreService(RedisConnectionFactory redisConnectionFactory) {
        ProxyProperties.TaskStore.Type type = this.properties.getTaskStore().getType();
        Duration timeout = this.properties.getTaskStore().getTimeout();
        return switch (type) {
            case IN_MEMORY -> new InMemoryTaskStoreServiceImpl(timeout);
            case REDIS -> new RedisTaskStoreServiceImpl(timeout, taskRedisTemplate(redisConnectionFactory));
        };
    }
 
    @Bean
    RedisTemplate<String, Task> taskRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Task> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Task.class));
        return redisTemplate;
    }
 
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
 
    @Bean
    public IRule loadBalancerRule() {
        String ruleClassName = IRule.class.getPackageName() + "." + this.properties.getAccountChooseRule();
        return ReflectUtil.newInstance(ruleClassName);
    }
 
    @Bean
    List<MessageHandler> messageHandlers() {
        return this.applicationContext.getBeansOfType(MessageHandler.class).values().stream().toList();
    }
 
    @Bean
    DiscordAccountHelper discordAccountHelper(DiscordHelper discordHelper, TaskStoreService taskStoreService, NotifyService notifyService) throws IOException {
        var resources = this.applicationContext.getResources("classpath:api-params/*.json");
        Map<String, String> paramsMap = new HashMap<>();
        for (var resource : resources) {
            String filename = resource.getFilename();
            String params = IoUtil.readUtf8(resource.getInputStream());
            paramsMap.put(filename.substring(0, filename.length() - 5), params);
        }
        return new DiscordAccountHelper(discordHelper, this.properties, restTemplate(), taskStoreService, notifyService, messageHandlers(), paramsMap);
    }
 
 
}