init
This commit is contained in:
3
cmd/ctl/README.md
Normal file
3
cmd/ctl/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
## Init
|
||||
|
||||
go build ./cmd/ctl
|
62
cmd/ctl/command/model/file.go
Normal file
62
cmd/ctl/command/model/file.go
Normal file
@ -0,0 +1,62 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"ArmedPolice/utils"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
const ModelTemplate = `package {{.Name}}
|
||||
|
||||
type {{.StrutName}} struct {
|
||||
Model
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *{{.StrutName}}) TableName() string {
|
||||
return "{{.TableName}}"
|
||||
}
|
||||
|
||||
func New{{.StrutName}}() *{{.StrutName}} {
|
||||
return &{{.StrutName}}{}
|
||||
}`
|
||||
|
||||
type ModelFile struct {
|
||||
// Name is the plugin name. Snack style.
|
||||
Name string
|
||||
// StrutName is the struct name.
|
||||
StrutName string
|
||||
// TableName is the model table name.
|
||||
TableName string
|
||||
}
|
||||
|
||||
func (v *ModelFile) 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
|
||||
}
|
73
cmd/ctl/command/model/model.go
Normal file
73
cmd/ctl/command/model/model.go
Normal file
@ -0,0 +1,73 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"Edu/config"
|
||||
"Edu/serve/logger"
|
||||
"Edu/utils"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ModelCommand
|
||||
var ModelCommand = &cobra.Command{
|
||||
Use: "make:model",
|
||||
Short: "quick make model",
|
||||
Example: "ctl make:model -f sys_user -t sys_user",
|
||||
Version: *config.Version,
|
||||
}
|
||||
|
||||
var (
|
||||
file string
|
||||
tableName string
|
||||
)
|
||||
|
||||
func init() {
|
||||
ModelCommand.Flags().StringVarP(&file, "file", "f", "", "The file name.")
|
||||
ModelCommand.Flags().StringVarP(&tableName, "tableName", "t", "", "The file model tableName")
|
||||
ModelCommand.Run = func(cmd *cobra.Command, args []string) {
|
||||
utils.TryCatch(func() {
|
||||
if file == "" {
|
||||
logger.ErrorF("Filename Not Nil")
|
||||
return
|
||||
}
|
||||
if tableName == "" {
|
||||
logger.ErrorF("TableName Not Nil")
|
||||
return
|
||||
}
|
||||
file := strings.ToLower(file)
|
||||
|
||||
dir, err := os.Getwd()
|
||||
|
||||
output := "/app/common/model"
|
||||
|
||||
output = path.Join(dir, output)
|
||||
|
||||
if err != nil {
|
||||
logger.ErrorF("Make Model Error:%v", err)
|
||||
return
|
||||
}
|
||||
if err = utils.PrepareOutput(output); err != nil {
|
||||
logger.ErrorF("Make Model Error:%v", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(output)
|
||||
|
||||
modelFile := &ModelFile{
|
||||
Name: "model",
|
||||
StrutName: utils.ToSnake(file, "_"),
|
||||
TableName: tableName,
|
||||
}
|
||||
err = modelFile.Execute(path.Join(output, file+".go"), "model", ModelTemplate)
|
||||
|
||||
if err != nil {
|
||||
logger.ErrorF("Make Model Error:%v", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
})
|
||||
}
|
||||
}
|
22
cmd/ctl/main.go
Normal file
22
cmd/ctl/main.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"Edu/cmd/ctl/command/model"
|
||||
"Edu/config"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "ctl",
|
||||
Long: "ctl is a command line tool for serve",
|
||||
Version: *config.Version,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(model.ModelCommand)
|
||||
}
|
||||
|
||||
func main() {
|
||||
_ = rootCmd.Execute()
|
||||
}
|
56
cmd/serve/serve.go
Normal file
56
cmd/serve/serve.go
Normal file
@ -0,0 +1,56 @@
|
||||
package serve
|
||||
|
||||
import (
|
||||
"ArmedPolice/app"
|
||||
"ArmedPolice/config"
|
||||
"ArmedPolice/lib"
|
||||
"ArmedPolice/router"
|
||||
"ArmedPolice/serve/cache"
|
||||
"ArmedPolice/serve/logger"
|
||||
"ArmedPolice/serve/orm"
|
||||
"ArmedPolice/serve/web"
|
||||
"ArmedPolice/task"
|
||||
"ArmedPolice/tools"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Serve struct {
|
||||
*Option
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
Config string `json:"config"`
|
||||
RpcConfig string `json:"rpc_config"`
|
||||
}
|
||||
|
||||
func (this *Serve) Run() {
|
||||
// 载入配置
|
||||
lib.LoadConfig(this.Option.Config, config.SettingInfo, func(i interface{}) {
|
||||
obj := i.(*config.Setting)
|
||||
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()
|
||||
task.Init()
|
||||
app.Init()
|
||||
tools.Init()
|
||||
// 开启web
|
||||
web.NewWeb()(&web.WebConfig{
|
||||
Port: config.SettingInfo.Server.Port, ReadTimeout: config.SettingInfo.Server.ReadTimeout,
|
||||
WriteTimeout: config.SettingInfo.Server.WriteTimeout, IdleTimeout: config.SettingInfo.Server.IdleTimeout,
|
||||
}).Run(router.NewRouter(&router.Option{
|
||||
Mode: gin.DebugMode, IsCors: true,
|
||||
RateLimitConfig: &router.RateLimitConfig{
|
||||
IsRate: true, Limit: config.SettingInfo.Rate.Limit, Capacity: config.SettingInfo.Rate.Capacity,
|
||||
},
|
||||
}).Init())
|
||||
}
|
||||
|
||||
func NewServe() func(option *Option) *Serve {
|
||||
return func(option *Option) *Serve {
|
||||
return &Serve{option}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user