-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.go
85 lines (75 loc) · 2.33 KB
/
app.go
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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/pwh-pwh/ai-gui/service"
"github.com/pwh-pwh/ai-gui/types"
"github.com/pwh-pwh/ai-gui/utils"
"os"
"path/filepath"
)
const CONFIG_FILE_NAME = "app.json"
// App struct
type App struct {
ctx context.Context
chat service.Chat
status string
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
InitWithApp()
utils.InitConfigPath()
fullFilePath := filepath.Join(utils.GetConfigPath(), CONFIG_FILE_NAME)
exists := utils.FileExists(fullFilePath)
conf := types.Config{}
if !exists {
jsonData, _ := json.Marshal(conf)
err := utils.WriteFile(fullFilePath, jsonData)
if err != nil {
a.status = fmt.Sprintf("无法在目录:%s%c创建%s 配置文件,请手动创建", utils.GetConfigPath(), os.PathSeparator, CONFIG_FILE_NAME)
} else {
a.status = fmt.Sprintf("应用初始化,请配置:%s%c%s 配置文件", utils.GetConfigPath(), os.PathSeparator, CONFIG_FILE_NAME)
}
utils.OpenFolder(utils.GetConfigPath())
} else {
a.status = fmt.Sprintf("应用启动,成功读取路径:%s%c%s 配置文件", utils.GetConfigPath(), os.PathSeparator, CONFIG_FILE_NAME)
bytesData, _ := utils.ReadFile(filepath.Join(utils.GetConfigPath(), CONFIG_FILE_NAME))
err := json.Unmarshal(bytesData, &conf)
if err != nil {
a.status = fmt.Sprintf("应用启动,读取配置文件失败:%s", err.Error())
}
}
a.chat = service.GetChatByConfig(conf)
}
// 对接 /loadconf
func (a *App) ReloadConf() string {
conf := types.Config{}
bytesData, _ := utils.ReadFile(filepath.Join(utils.GetConfigPath(), CONFIG_FILE_NAME))
err := json.Unmarshal(bytesData, &conf)
if err != nil {
a.status = fmt.Sprintf("重载配置文件失败:%s", err.Error())
}
a.chat = service.GetChatByConfig(conf)
return "配置重载完成"
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) GetStatus() string {
return a.status
}
func (a *App) DoChat(msg []types.Message) string {
return a.chat.Dochat(msg)
}
// 对接 /opconf
func (a *App) OpenConfigFolder() {
utils.OpenFolder(utils.GetConfigPath())
}