Skip to content

Commit

Permalink
Merge pull request #79 from sunmi-OS/feature/v2.1.0
Browse files Browse the repository at this point in the history
Feature/v2.1.0
  • Loading branch information
luduoxin authored Oct 10, 2022
2 parents 6bf676f + 79ff7ca commit 4b1d8fc
Show file tree
Hide file tree
Showing 38 changed files with 150 additions and 270 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ go.mod
go.sum
.vscode/
gocore
tools/gocore/main
18 changes: 2 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

gocore是一款高度集成的开发框架和脚手架,支持api、rpc、job、task等开发方式,并集成各类主流开源库和中间件融入最佳实践,简化研发流程、提高效率、统一规范。

官网文档站首页:https://sunmi-os.github.io/gocore/

![cli](https://file.cdn.sunmi.com/gocore_cli.svg)

## 特性
Expand Down Expand Up @@ -131,8 +129,8 @@ config:
hotUpdate: false #是否热更新
index:
db0: 0 #选择第几个db
rpcEnable: false #是否生成rpc服务层
httpApiEnable: true #是否生成接口程序
cronJobEnable: true #是否生成定时任务
jobEnable: true #是否生成常驻任务
httpApis:
host: 0.0.0.0 #api接口监听ip地址
Expand Down Expand Up @@ -168,19 +166,7 @@ httpApis:
type: string
comment: 用户名
validate: ""
cronJobs:
- spec: '@every 30m' #定时任务规则,参考:github.com/robfig/cron
job:
name: SyncUser #定时任务方法名称
comment: 同步用户 #定时任务备注
jobs:
- name: InitUser #一次性任务,常驻任务方法名称
comment: 初始化默认用户 #一次性任务,常驻任务备注
```
## 联系我们
欢迎加入`gocore`QQ群:1004023331 一起沟通讨论
![qq](https://file.cdn.sunmi.com/qq.png?x-oss-process=image/resize,h_200)
```
Binary file modified tools/.DS_Store
Binary file not shown.
53 changes: 31 additions & 22 deletions tools/gocore/cmd/create_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"
"os"
"os/exec"

"github.com/fatih/color"
"github.com/sunmi-OS/gocore/v2/tools/gocore/conf"
Expand Down Expand Up @@ -34,7 +36,11 @@ func creatService(c *cli.Context) error {
config := conf.GetGocoreConfig()
yamlPath := c.String("config")
root := "."

sourceCodeRoot := root + "/app"
err := os.Mkdir(sourceCodeRoot, os.ModePerm)
if err != nil {
panic("Failed to create sourceCodeRoot folder: " + err.Error())
}
if yamlPath == "" {
yamlPath = root + "/gocore.yaml"
}
Expand All @@ -44,51 +50,54 @@ func creatService(c *cli.Context) error {
}

// 创建配置&读取配置
config, err := InitYaml(yamlPath, config)
config, err = InitYaml(yamlPath, config)
if err != nil {
panic(err)
}

modPath := root + "/go.mod"
modPath := sourceCodeRoot + "/go.mod"
if file.CheckFileIsExist(modPath) {
resp, err := utils.Cmd("go", []string{"fmt", "./..."})
if err != nil {
fmt.Println(resp)
panic(err)
}
} else {
printHint("Run go mod init.")
resp, err := utils.Cmd("go", []string{"mod", "init", config.Service.ProjectName})
printHint("Run go mod init")
goModInitCmd := exec.Command("go", []string{"mod", "init", config.Service.ProjectName}...)
goModInitCmd.Dir = sourceCodeRoot
out, err := goModInitCmd.Output()
if err != nil {
fmt.Println(resp)
fmt.Println(out)
panic(err)
}
}

template.CreateCode(root, config.Service.ProjectName, config)
template.CreateCode(root, sourceCodeRoot, config.Service.ProjectName, config)

printHint("Run go mod tidy.")
printHint("Run go mod tidy")

resp, err := utils.Cmd("go", []string{"mod", "tidy"})
goModTidyCmd := exec.Command("go", []string{"mod", "tidy"}...)
goModTidyCmd.Dir = sourceCodeRoot
goModTidyCmd.Stderr = os.Stderr
err = goModTidyCmd.Start()
if err != nil {
fmt.Println(resp)
panic(err)
panic("go mod tidy error: " + err.Error())
}
_ = goModTidyCmd.Wait()

printHint("Run go fmt.")
resp, err = utils.Cmd("go", []string{"fmt", "./..."})
if err != nil {
fmt.Println(resp)
panic(err)
}
printHint("Run go fmt")

printHint("goimports -l -w .")
resp, err = utils.Cmd("goimports", []string{"-l", "-w", "."})
goFmtCmd := exec.Command("go", []string{"fmt", "./..."}...)
goFmtCmd.Dir = sourceCodeRoot
goFmtCmd.Stderr = os.Stderr
err = goFmtCmd.Start()
if err != nil {
fmt.Println(resp)
panic(err)
panic("go fmt error: " + err.Error())
}
printHint("Welcome to GoCore, the project has been initialized.")
_ = goFmtCmd.Wait()

printHint("Welcome to GoCore, the project has been initialized")

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion tools/gocore/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package conf
// 配置常量
const (
PROJECT_NAME = "gocore"
PROJECT_VERSION = "v1.0.1"
PROJECT_VERSION = "v1.1.0"
)
91 changes: 39 additions & 52 deletions tools/gocore/conf/gocore.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package conf

import (
"os"
"strings"

"github.com/sunmi-OS/gocore/v2/utils/file"
)

type GoCore struct {
Service Service `yaml:"service"`
Config Config `yaml:"config"`
HttpApiEnable bool `yaml:"httpApiEnable"` // 是否开启HttpApi
CronJobEnable bool `yaml:"cronJobEnable"` // 是否开启 CronJob 默认不开启
JobEnable bool `yaml:"jobEnable"` // 是否开启 Job 任务
HttpApis HttpApi `yaml:"httpApis"`
CronJobs []CronJob `yaml:"cronJobs"`
Jobs []Job `yaml:"jobs"`
Service Service `yaml:"service"`
Config Config `yaml:"config"`
HttpApiEnable bool `yaml:"httpApiEnable"` // 是否开启HttpApi,开启后生成接口表示层
RPCEnable bool `yaml:"rpcEnable"` // 是否开启 rpc server,开启后生成rpc服务器表示层
JobEnable bool `yaml:"jobEnable"` // 是否开启 Job 任务
HttpApis HttpApi `yaml:"httpApis"`
Jobs []Job `yaml:"jobs"`
}

type Service struct {
Expand Down Expand Up @@ -52,11 +50,6 @@ type Param struct {
Validate string `yaml:"validate"`
}

type CronJob struct {
Spec string `yaml:"spec"` // 定时规则
Job Job `yaml:"job"`
}

type Job struct {
Name string `yaml:"name"` // 任务名称
Comment string `yaml:"comment"`
Expand Down Expand Up @@ -89,10 +82,13 @@ type Redis struct {
}

func GetGocoreConfig() *GoCore {

projectName := "demo"
// 获取当前目录名称
path := file.GetPath()
pwd, err := os.Getwd()
if err != nil {
panic("failed to get the rooted path name corresponding to the current directory")
}
path := strings.Replace(pwd, "\\", "/", -1)
arr := strings.Split(path, "/")
if len(arr) > 1 {
projectName = arr[len(arr)-1]
Expand All @@ -107,7 +103,7 @@ func GetGocoreConfig() *GoCore {
CRocketMQConfig: true,
CMysql: []Mysql{
{
Name: "app",
Name: "demo",
Models: []Model{
{
Name: "user",
Expand All @@ -123,76 +119,67 @@ func GetGocoreConfig() *GoCore {
},
CRedis: []Redis{
{
Name: "default",
Name: "demo",
Index: map[string]int{
"db0": 0,
},
},
},
},
RPCEnable: false,
HttpApiEnable: true,
CronJobEnable: true,
JobEnable: true,
HttpApis: HttpApi{
Host: "0.0.0.0",
Port: "80",
Params: map[string][]Param{
"User": {
{
Name: "uid",
Type: "int",
Comment: "用户ID",
},
{
Name: "name",
Type: "string",
Comment: "用户名",
},
},
},
//Params: map[string][]Param{
// "User": {
// {
// Name: "id",
// Type: "int64",
// Comment: "用户ID",
// },
// {
// Name: "name",
// Type: "string",
// Comment: "用户名",
// },
// },
//},
Apis: []Api{
{
ModuleName: "user",
Prefix: "/app/user",
Handle: []Handle{
{
Name: "GetUserInfo",
Name: "getUserInfo",
Method: "POST",
Comment: "获取用户信息",
RequestParams: []Param{
{
Name: "uid",
Type: "int",
Name: "id",
Type: "int64",
Comment: "用户ID",
Validate: "required,min=1,max=100000",
},
},
ResponseParams: []Param{
{
Name: "detail",
Type: "*User",
Comment: "用户详情",
Name: "id",
Type: "int64",
Comment: "用户ID",
},
{
Name: "list",
Type: "[]*User",
Comment: "用户列表",
Name: "name",
Type: "string",
Comment: "用户名",
},
},
},
},
},
},
},
CronJobs: []CronJob{
{
Spec: "@every 30m",
Job: Job{
Name: "SyncUser",
Comment: "同步用户",
},
},
},
Jobs: []Job{
{
Name: "InitUser",
Expand Down
6 changes: 3 additions & 3 deletions tools/gocore/template/Dockerfile.docker
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ ENV GO111MODULE on

#step 1 build go cache
WORKDIR /go/cache
ADD go.mod .
ADD go.sum .
ADD app/go.mod .
ADD app/go.sum .
RUN go mod download

#step 2 build binary project
WORKDIR /project
ADD . .
ADD ./app/ .
RUN ls
RUN go build main.go

Expand Down
9 changes: 3 additions & 6 deletions tools/gocore/template/Dockerfile.docker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Code generated by hero.
// source: /Users/liuguoqiang/Desktop/go/mod/gocore/tools/gocore/template/Dockerfile.docker
// DO NOT EDIT!
package template

import "bytes"
Expand All @@ -14,13 +11,13 @@ ENV GO111MODULE on
#step 1 build go cache
WORKDIR /go/cache
ADD go.mod .
ADD go.sum .
ADD app/go.mod .
ADD app/go.sum .
RUN go mod download
#step 2 build binary project
WORKDIR /project
ADD . .
ADD ./app/ .
RUN ls
RUN go build main.go
Expand Down
3 changes: 0 additions & 3 deletions tools/gocore/template/README.md.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Code generated by hero.
// source: /Users/liuguoqiang/Desktop/go/mod/gocore/tools/gocore/template/README.md
// DO NOT EDIT!
package template

import "bytes"
Expand Down
6 changes: 3 additions & 3 deletions tools/gocore/template/api.got
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package api

import (
"<%== name %>/app/def"
"<%== name %>/param"

"github.com/gin-gonic/gin"
"github.com/sunmi-OS/gocore/v2/api"
Expand All @@ -16,12 +16,12 @@ import (
// <%== v1 %> <%== " "+comments[k1] %>
func <%== v1 %>(g *gin.Context) {
ctx := api.NewContext(g)
req := new(def.<%== req[k1] %>Request)
req := new(param.<%== req[k1] %>Request)
err := ctx.BindValidator(req)
if err != nil {
ctx.Error(err)
return
}
ctx.Success(def.<%== req[k1] %>Response{})
ctx.Success(param.<%== req[k1] %>Response{})
}
<% } %>
Loading

0 comments on commit 4b1d8fc

Please sign in to comment.