feat:完善项目

This commit is contained in:
henry
2021-11-02 16:22:07 +08:00
parent 690cd96bed
commit 20d81825e1
81 changed files with 5394 additions and 3700 deletions

View File

@ -0,0 +1,73 @@
package controller
import (
"ArmedPolice/utils"
"bytes"
"fmt"
"go/format"
"text/template"
)
const ControllerTemplate = `package {{.Name}}
import (
"ArmedPolice/app/controller/basic"
"ArmedPolice/app/service"
)
type {{.StrutName}} struct{ *service.Session }
type {{.StrutName}}Handle func(session *service.Session) *{{.StrutName}}
func (c *{{.StrutName}}) List() (*basic.ReturnPages, error) {
return &basic.ReturnPages{Data: nil, Count: 0}, nil
}
func (c *{{.StrutName}}) Form() error {
return nil
}
func (c *{{.StrutName}}) Delete() error {
return nil
}
func New{{.StrutName}}() {{.StrutName}}Handle {
return func(session *service.Session) *{{.StrutName}} {
return &{{.StrutName}}{session}
}
}`
type ControllerFile struct {
// Name is the plugin name. Snack style.
Name string
// StrutName is the struct name.
StrutName string
}
func (v *ControllerFile) Execute(file, tmplName, tmpl string) error {
fmt.Println(file)
f, err := utils.Open(file)
if err != nil {
return err
}
defer f.Close()
temp := new(template.Template)
if temp, err = template.New(tmplName).Parse(tmpl); err != nil {
return err
}
buf := new(bytes.Buffer)
if err = temp.Execute(buf, v); err != nil {
return err
}
out := make([]byte, 0)
if out, err = format.Source(buf.Bytes()); err != nil {
return err
}
_, err = f.Write(out)
return err
}

View File

@ -0,0 +1,75 @@
package controller
import (
"ArmedPolice/config"
"ArmedPolice/serve/logger"
"ArmedPolice/utils"
"fmt"
"os"
"path"
"strings"
"github.com/spf13/cobra"
)
var ControllerCommand = &cobra.Command{
Use: "make:controller",
Short: "quick make controller",
Example: "ctl make:controller -f sys_user -t sys_user",
Version: *config.Version,
}
var (
file string
address string
)
func init() {
ControllerCommand.Flags().StringVarP(&file, "file", "f", "", "The file name.")
ControllerCommand.Flags().StringVarP(&address, "address", "a", "", "The file controller address")
ControllerCommand.Run = func(cmd *cobra.Command, args []string) {
utils.TryCatch(func() {
if file == "" {
logger.ErrorF("Filename Not Nil")
return
}
output := "../../app/controller"
if address == "" {
logger.ErrorF("Address Not Nil")
return
}
// 解析文件地址
address = strings.Replace(address, "/", "", -1)
addresss := strings.Split(address, "/")
file := strings.ToLower(file)
dir, err := os.Getwd()
output = path.Join(dir, output+"/"+address)
if err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
if err = utils.PrepareOutput(output); err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
fmt.Println(output)
modelFile := &ControllerFile{
Name: addresss[len(addresss)-1],
StrutName: utils.ToSnake(file, "_"),
}
err = modelFile.Execute(path.Join(output, file+".go"), "controller", ControllerTemplate)
if err != nil {
logger.ErrorF("Make Controller Error%v", err)
return
}
return
})
}
}

View File

@ -1,9 +1,9 @@
package model
import (
"Edu/config"
"Edu/serve/logger"
"Edu/utils"
"ArmedPolice/config"
"ArmedPolice/serve/logger"
"ArmedPolice/utils"
"fmt"
"os"
"path"
@ -42,7 +42,7 @@ func init() {
dir, err := os.Getwd()
output := "/app/common/model"
output := "../..//app/common/model"
output = path.Join(dir, output)

View File

@ -1,8 +1,9 @@
package main
import (
"Edu/cmd/ctl/command/model"
"Edu/config"
"ArmedPolice/cmd/ctl/command/controller"
"ArmedPolice/cmd/ctl/command/model"
"ArmedPolice/config"
"github.com/spf13/cobra"
)
@ -14,6 +15,7 @@ var rootCmd = &cobra.Command{
}
func init() {
rootCmd.AddCommand(controller.ControllerCommand)
rootCmd.AddCommand(model.ModelCommand)
}

View File

@ -2,6 +2,7 @@ package serve
import (
"ArmedPolice/app"
"ArmedPolice/app/common"
"ArmedPolice/config"
"ArmedPolice/lib"
"ArmedPolice/router"
@ -10,7 +11,6 @@ import (
"ArmedPolice/serve/orm"
"ArmedPolice/serve/web"
"ArmedPolice/task"
"ArmedPolice/tools"
"strings"
"github.com/gin-gonic/gin"
@ -21,8 +21,7 @@ type Serve struct {
}
type Option struct {
Config string `json:"config"`
RpcConfig string `json:"rpc_config"`
Config string `json:"config"`
}
func (this *Serve) Run() {
@ -32,11 +31,14 @@ func (this *Serve) Run() {
obj.Upload.Exts = strings.Split(obj.Upload.Ext, ",")
logger.NewLogger().Init(&logger.Option{File: obj.Log.File, LeastDay: obj.Log.LeastDay, Level: obj.Log.Level, IsStdout: false}).Load()
})
cache.Init()
orm.Init()
if *config.Init {
common.Init()
}
cache.Init()
task.Init()
app.Init()
tools.Init()
// 开启web
web.NewWeb()(&web.WebConfig{
Port: config.SettingInfo.Server.Port, ReadTimeout: config.SettingInfo.Server.ReadTimeout,