Skip to content

Commit

Permalink
update todo
Browse files Browse the repository at this point in the history
  • Loading branch information
陈壁浩 committed Jun 30, 2024
1 parent 2eca2ff commit 7e8740b
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 238 deletions.
48 changes: 34 additions & 14 deletions docs/src/guide/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,12 @@ description:
---
# 待办

## 梳理
## 功能优化

- [ ] 梳理引入包
- [ ] 梳理源码引入
- [ ] 梳理三方库引入
- [ ] 梳理新版本 go 废弃 API,换成新的
- [ ] 统一 provider 注册方法 (`func (provider *GormProvider) Register` 里的调用 new 命名)
- [x] 补充 command 、contract 文件开头说明文档,方便查看(甚至改成支持 doc)
- [x] command:包括命令说明、可选配置项
- [ ] contract:包括对应命令、配置项说明
- [ ] 部分 linux 内容未测试
- [ ] 条件编译
- [ ] 守护进程模式 `app start --daemon=true`
- [ ] gspt 库(CGO_ENABLED=1)
- [ ] 全部代码过一遍,看看有什么问题

## 功能优化

- [ ] 脚手架优化
- [ ] 构建后读取配置问题(生产部署时如何读取配置问题)
Expand Down Expand Up @@ -69,10 +58,41 @@ description:
- [ ] 远程配置中心

- [ ] 蓝图模块功能
- [ ] 初始化蓝图流程(拉取蓝图模块、建表等)
- [ ] 后台管理基础
- [ ] 用户注册登录
- [ ] RBAC 权限
- [ ] 多租户模块
- [ ] 博客
- [ ] ...
- [ ] ...
- [ ] ...
- [ ] ...

## 已完成归档

- [x] 梳理使用框架
- [x] 梳理源码引入
- cobra
- gin v1.9.1 + middleware
- [x] 梳理三方库引入
- fsnotify、go-daemon、goconvey、swaggo、cast
- survey/v2、go-git/v5、go-github/v62、go-redis/v9、cron/v3、gorm + gen
- gotree、uuid、xid、ratelimit、file-rotatelogs、mapstructure
- jennifer/jen、jianfengye/collection、kr/pretty
- [x] 梳理三方框架使用
- vue、vuepress

- [x] 统一 provider 注册方法 (`func (provider *GormProvider) Register` 里的调用 new 命名)
- [x] 补充 command 、contract 文件开头说明文档,方便查看(甚至改成支持 doc)
- [x] command:包括命令说明、可选配置项
- [x] contract:包括对应命令、配置项说明

- [x] 梳理新版本 go 废弃 API,换成新的
- `io/ioutil` -> `os``io`
- `strings.Title` -> `cases.Title`
- `math/rand` -> `rand.Rand`






5 changes: 3 additions & 2 deletions docs/src/provider/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ cache 服务提供丰富的接口,可以通过接口来操作缓存,目前
通过配置文件 `config/[env]/cache.yaml` 可以配置缓存服务的驱动和参数,如下是一个配置示例:

```yaml
driver: memory # 连接驱动,可选 redis/memory
... # 如果 driver: redis,则可配置项与redis服务一致
# 连接驱动,可选 redis/memory
# 如果 driver=redis,则可配置项与redis服务一致
driver: memory
```
## 提供方法:
Expand Down
3 changes: 2 additions & 1 deletion docs/src/provider/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ description:

## 服务介绍:
提供基础的配置文件获取方法
## 支持命令:无
## 支持命令:
[app](../command/config)
## 支持配置:无

## 提供方法:
Expand Down
3 changes: 2 additions & 1 deletion docs/src/provider/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ description:

## 服务介绍:
提供环境变量相关方法
## 支持命令:无
## 支持命令:
[app](../command/env)
## 支持配置:无

## 提供方法:
Expand Down
3 changes: 2 additions & 1 deletion docs/src/provider/orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ description:

## 服务介绍:
提供ORM服务的服务,可以用于获取数据库连接,获取表结构等。
## 支持命令:无
## 支持命令:
[app](../command/model)
## 支持配置:

使用之前需要确保已经正确配置了redis服务。
Expand Down
74 changes: 62 additions & 12 deletions framework/command/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,47 @@ const AppCommandKey = "运行命令"
var appAddress = "" // app 启动地址
var appDaemon = false // app 守护模式

// appBaseArgs对应AppService中可以配置的参数
var appStartArgs = []string{
"base_folder",
"config_folder",
"log_folder",
"http_folder",
"console_folder",
"storage_folder",
"provider_folder",
"middleware_folder",
"command_folder",
"runtime_folder",
"test_folder",
"deploy_folder",
"app_folder",
}

var appOtherArgs = []string{
"runtime_folder",
"storage_folder",
"base_folder",
}

// initAppCommand 初始化app命令和其子命令
func initAppCommand() *cobra.Command {

appStartCommand.Flags().StringVar(&appAddress, "address", ":8080", "设置app启动的地址,默认为:8080端口")
appStartCommand.Flags().BoolVarP(&appDaemon, "daemon", "d", false, "以守护进程方式启动")

for _, arg := range appStartArgs {
tmp := ""
appStartCommand.Flags().StringVar(&tmp, arg, "", "base config for app service: "+arg)
}

for _, arg := range appOtherArgs {
tmp := ""
appRestartCommand.Flags().StringVar(&tmp, arg, "", "base config for app service: "+arg)
appStateCommand.Flags().StringVar(&tmp, arg, "", "base config for app service: "+arg)
appStopCommand.Flags().StringVar(&tmp, arg, "", "base config for app service: "+arg)
}

appCommand.AddCommand(appStartCommand)
appCommand.AddCommand(appRestartCommand)
appCommand.AddCommand(appStateCommand)
Expand Down Expand Up @@ -117,6 +152,7 @@ var appStartCommand = &cobra.Command{
// 从kernel服务实例中获取引擎
core := kernelService.HttpEngine()

// 先读取参数,然后读取Env,然后读取配置文件
if appAddress == "" {
envService := container.MustMake(contract.EnvKey).(contract.Env)
if envService.Get("ADDRESS") != "" {
Expand All @@ -136,30 +172,44 @@ var appStartCommand = &cobra.Command{
Addr: appAddress,
}

processName := "gob app"
if len(os.Args) > 0 {
processName = filepath.Base(os.Args[0]) + " app"
}

// 设置app的日志地址和进程id地址
appService := container.MustMake(contract.AppKey).(contract.App)

runtimeFolder := appService.RuntimeFolder()
serverPidFile := filepath.Join(runtimeFolder, "app.pid")

pidFolder := appService.RuntimeFolder()
serverPidFile := filepath.Join(pidFolder, "app.pid")
if err := util.CreateFolderIfNotExists(pidFolder); err != nil {
return err
}
logFolder := appService.LogFolder()
serverLogFile := filepath.Join(logFolder, "app.log")
if err := util.CreateFolderIfNotExists(logFolder); err != nil {
return err
}
currentFolder := util.GetExecDirectory()

processName := "gob app"
if len(os.Args) > 0 {
processName = filepath.Base(os.Args[0]) + " app"
}

// daemon 模式
if appDaemon {
// win不支持 daemon 模式
if util.IsWindows() {
return errors.New("daemon: Non-POSIX OS is not supported")
}

parentArgs := make([]string, 0, len(os.Args))
for _, arg := range os.Args {
if strings.HasPrefix(arg, "--") {
if strings.Contains(arg, "--daemon=") {
continue
}
parentArgs = append(parentArgs, arg)
}
}
subArgs := []string{filepath.Base(os.Args[0]), "app", "start", "--daemon=true"}
subArgs = append(subArgs, parentArgs...)
// 创建一个Context
cntxt := &daemon.Context{
// 设置pid文件
Expand All @@ -173,7 +223,9 @@ var appStartCommand = &cobra.Command{
// 设置所有设置文件的mask,默认为750
Umask: 027,
// 子进程的参数,按照这个参数设置,子进程的命令为 ./gob app start --daemon=true
Args: []string{"", "app", "start", "--daemon=true"},
Args: subArgs,
// 环境变量和父进程一样
Env: os.Environ(),
}
// 启动子进程,d不为空表示当前是父进程,d为空表示当前是子进程
// 这里可以把 Reborn 理解成 fork,当调用这个函数的时候,父进程会继续往下走,但是返回值 d 不为空,它的信息是子进程的进程号等信息。
Expand Down Expand Up @@ -210,14 +262,12 @@ var appStartCommand = &cobra.Command{

// 非 daemon 模式,直接执行
content := strconv.Itoa(os.Getpid())
fmt.Println("[PID]", content)
err := os.WriteFile(serverPidFile, []byte(content), 0644)
if err != nil {
return err
}

util.SetProcessTitle(processName)

fmt.Println("成功启动进程:", processName)
fmt.Println("进程pid:", content)
showAppAddress := appAddress
Expand Down Expand Up @@ -271,7 +321,7 @@ var appRestartCommand = &cobra.Command{
if configService.IsExist("app.close_wait") {
closeWait = configService.GetInt("app.close_wait")
}
// 确认进程已经关闭,每秒检测一次, 最多检测 closeWait * 2秒
// 确认进程已经关闭,每秒检测一次, 最多检测 closeWait * 2 秒
for i := 0; i < closeWait*2; i++ {
if !util.CheckProcessExist(pid) {
break
Expand Down
4 changes: 1 addition & 3 deletions framework/command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/chenbihao/gob/framework/util"
"github.com/disiqueira/gotree"
"github.com/pkg/errors"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"os"
"path/filepath"
"text/template"
Expand Down Expand Up @@ -125,7 +123,7 @@ var cmdCreateCommand = &cobra.Command{
}

// 创建title这个模版方法
funcs := template.FuncMap{"title": cases.Title(language.Und, cases.NoLower).String}
funcs := template.FuncMap{"title": util.ToTitle}
{
// 创建name.go
file := filepath.Join(pFolder, folder, name+".go")
Expand Down
9 changes: 4 additions & 5 deletions framework/command/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/chenbihao/gob/framework/contract"
"github.com/chenbihao/gob/framework/util"
"github.com/sevlyar/go-daemon"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -160,7 +159,7 @@ var cronRestartCommand = &cobra.Command{
// GetPid
serverPidFile := filepath.Join(appService.RuntimeFolder(), "cron.pid")

content, err := ioutil.ReadFile(serverPidFile)
content, err := os.ReadFile(serverPidFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -199,7 +198,7 @@ var cronStopCommand = &cobra.Command{
// GetPid
serverPidFile := filepath.Join(appService.RuntimeFolder(), "cron.pid")

content, err := ioutil.ReadFile(serverPidFile)
content, err := os.ReadFile(serverPidFile)
if err != nil {
return err
}
Expand All @@ -212,7 +211,7 @@ var cronStopCommand = &cobra.Command{
if err := util.KillProcess(pid); err != nil {
return err
}
if err := ioutil.WriteFile(serverPidFile, []byte{}, 0644); err != nil {
if err := os.WriteFile(serverPidFile, []byte{}, 0644); err != nil {
return err
}
fmt.Println("stop pid:", pid)
Expand All @@ -231,7 +230,7 @@ var cronStateCommand = &cobra.Command{
// GetPid
serverPidFile := filepath.Join(appService.RuntimeFolder(), "cron.pid")

content, err := ioutil.ReadFile(serverPidFile)
content, err := os.ReadFile(serverPidFile)
if err != nil {
return err
}
Expand Down
11 changes: 2 additions & 9 deletions framework/command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ var deployAllCommand = &cobra.Command{
return err
}
// 上传前端+后端,并执行对应的shell
if err := deployUploadAction(deployFolder, container, "all"); err != nil {
return err
}
return nil
return deployUploadAction(deployFolder, container, "all")
},
}

Expand Down Expand Up @@ -191,6 +188,7 @@ func deployBuildBackend(c *cobra.Command, deployFolder string) error {
log.Fatalln("gob go: 请在Path路径中先安装go")
}

// 组装命令
deployBinFile := filepath.Join(deployFolder, binFile)
cmd := exec.Command(path, "build", "-o", deployBinFile, "./")
cmd.Env = os.Environ()
Expand Down Expand Up @@ -320,11 +318,6 @@ func deployUploadAction(deployFolder string, container framework.Container, end
})
bts, err := session.CombinedOutput(action)
if err != nil {
logger.Info(context.Background(), "execute post action err", map[string]interface{}{
"cmd": action,
"connection": node,
"out": strings.ReplaceAll(string(bts), "\n", ""),
})
session.Close()
return err
}
Expand Down
7 changes: 5 additions & 2 deletions framework/command/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ func (p *Proxy) startProxy(startFrontend, startBackend bool) (err error) {
// rebuildBackend 重新编译后端
func (p *Proxy) rebuildBackend() error {
// 重新编译
cmdBuild := exec.Command("./gob", "build", "backend")
bin := os.Args[0]
cmdBuild := exec.Command(bin, "build", "backend")
cmdBuild.Stdout = os.Stdout
cmdBuild.Stderr = os.Stderr
if err := cmdBuild.Start(); err == nil {
Expand Down Expand Up @@ -308,8 +309,9 @@ func (p *Proxy) restartBackend() error {
// 设置随机端口,真实后端的端口
port := p.devConfig.Backend.Port
gobAddress := fmt.Sprintf(":" + port)
bin := os.Args[0]
// 使用命令行启动后端进程
cmd := exec.Command("./gob", "app", "start", "--address="+gobAddress)
cmd := exec.Command(bin, "app", "start", "--address="+gobAddress)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand Down Expand Up @@ -408,6 +410,7 @@ func (p *Proxy) monitorBackend() (err error) {
if !ok {
continue
}
// 如果有文件监听错误,则停止计时器
fmt.Println("监听文件夹错误:", err.Error())
t.Reset(time.Duration(refreshTime) * time.Second)
}
Expand Down
Loading

0 comments on commit 7e8740b

Please sign in to comment.