mirror of
https://github.com/beego/bee.git
synced 2024-11-22 05:00:54 +00:00
bee api create a new api from database
This commit is contained in:
parent
41dbb385e7
commit
50dfe40b94
72
apiapp.go
72
apiapp.go
@ -28,6 +28,13 @@ var cmdApiapp = &Command{
|
|||||||
Long: `
|
Long: `
|
||||||
create an api application base on beego framework
|
create an api application base on beego framework
|
||||||
|
|
||||||
|
bee api [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
|
||||||
|
-tables: a list of table names separated by ',', default is empty, indicating all tables
|
||||||
|
-driver: [mysql | postgresql | sqlite], the default is mysql
|
||||||
|
-conn: the connection string used by the driver, the default is ''
|
||||||
|
|
||||||
|
if conn is empty will create a example api application. otherwise generate api application based on an existing database.
|
||||||
|
|
||||||
In the current path, will create a folder named [appname]
|
In the current path, will create a folder named [appname]
|
||||||
|
|
||||||
In the appname folder has the follow struct:
|
In the appname folder has the follow struct:
|
||||||
@ -73,6 +80,32 @@ func main() {
|
|||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var apiMainconngo = `package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "{{.Appname}}/docs"
|
||||||
|
_ "{{.Appname}}/routers"
|
||||||
|
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"github.com/astaxie/beego/orm"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
orm.RegisterDataBase("default", "mysql", "{{.conn}}")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if beego.RunMode == "dev" {
|
||||||
|
beego.DirectoryIndex = true
|
||||||
|
beego.StaticDir["/swagger"] = "swagger"
|
||||||
|
}
|
||||||
|
beego.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
var apirouter = `// @APIVersion 1.0.0
|
var apirouter = `// @APIVersion 1.0.0
|
||||||
// @Title beego Test API
|
// @Title beego Test API
|
||||||
// @Description beego has a very cool tools to autogenerate documents for your API
|
// @Description beego has a very cool tools to autogenerate documents for your API
|
||||||
@ -504,18 +537,26 @@ func TestGet(t *testing.T) {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdApiapp.Run = createapi
|
cmdApiapp.Run = createapi
|
||||||
|
cmdApiapp.Flag.Var(&tables, "tables", "specify tables to generate model")
|
||||||
|
cmdApiapp.Flag.Var(&driver, "driver", "database driver: mysql, postgresql, etc.")
|
||||||
|
cmdApiapp.Flag.Var(&conn, "conn", "connection string used by the driver to connect to a database instance")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createapi(cmd *Command, args []string) {
|
func createapi(cmd *Command, args []string) {
|
||||||
if len(args) != 1 {
|
curpath, _ := os.Getwd()
|
||||||
fmt.Println("error args")
|
if len(args) > 1 {
|
||||||
os.Exit(2)
|
cmd.Flag.Parse(args[1:])
|
||||||
}
|
}
|
||||||
apppath, packpath, err := checkEnv(args[0])
|
apppath, packpath, err := checkEnv(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
if driver == "" {
|
||||||
|
driver = "mysql"
|
||||||
|
}
|
||||||
|
if conn == "" {
|
||||||
|
}
|
||||||
os.MkdirAll(apppath, 0755)
|
os.MkdirAll(apppath, 0755)
|
||||||
fmt.Println("create app folder:", apppath)
|
fmt.Println("create app folder:", apppath)
|
||||||
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
||||||
@ -524,10 +565,6 @@ func createapi(cmd *Command, args []string) {
|
|||||||
fmt.Println("create controllers:", path.Join(apppath, "controllers"))
|
fmt.Println("create controllers:", path.Join(apppath, "controllers"))
|
||||||
os.Mkdir(path.Join(apppath, "docs"), 0755)
|
os.Mkdir(path.Join(apppath, "docs"), 0755)
|
||||||
fmt.Println("create docs:", path.Join(apppath, "docs"))
|
fmt.Println("create docs:", path.Join(apppath, "docs"))
|
||||||
os.Mkdir(path.Join(apppath, "models"), 0755)
|
|
||||||
fmt.Println("create models:", path.Join(apppath, "models"))
|
|
||||||
os.Mkdir(path.Join(apppath, "routers"), 0755)
|
|
||||||
fmt.Println(path.Join(apppath, "routers") + string(path.Separator))
|
|
||||||
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
||||||
fmt.Println("create tests:", path.Join(apppath, "tests"))
|
fmt.Println("create tests:", path.Join(apppath, "tests"))
|
||||||
|
|
||||||
@ -535,6 +572,26 @@ func createapi(cmd *Command, args []string) {
|
|||||||
writetofile(path.Join(apppath, "conf", "app.conf"),
|
writetofile(path.Join(apppath, "conf", "app.conf"),
|
||||||
strings.Replace(apiconf, "{{.Appname}}", args[0], -1))
|
strings.Replace(apiconf, "{{.Appname}}", args[0], -1))
|
||||||
|
|
||||||
|
if conn != "" {
|
||||||
|
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
|
||||||
|
writetofile(path.Join(apppath, "main.go"),
|
||||||
|
strings.Replace(
|
||||||
|
strings.Replace(apiMainconngo, "{{.Appname}}", packpath, -1),
|
||||||
|
"{{.conn}}",
|
||||||
|
conn.String(),
|
||||||
|
-1,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
ColorLog("[INFO] Using '%s' as 'driver'\n", driver)
|
||||||
|
ColorLog("[INFO] Using '%s' as 'conn'\n", conn)
|
||||||
|
ColorLog("[INFO] Using '%s' as 'tables'", tables)
|
||||||
|
generateModel(string(driver), string(conn), "3", string(tables), path.Join(curpath, packpath))
|
||||||
|
} else {
|
||||||
|
os.Mkdir(path.Join(apppath, "models"), 0755)
|
||||||
|
fmt.Println("create models:", path.Join(apppath, "models"))
|
||||||
|
os.Mkdir(path.Join(apppath, "routers"), 0755)
|
||||||
|
fmt.Println(path.Join(apppath, "routers") + string(path.Separator))
|
||||||
|
|
||||||
fmt.Println("create controllers object.go:", path.Join(apppath, "controllers", "object.go"))
|
fmt.Println("create controllers object.go:", path.Join(apppath, "controllers", "object.go"))
|
||||||
writetofile(path.Join(apppath, "controllers", "object.go"),
|
writetofile(path.Join(apppath, "controllers", "object.go"),
|
||||||
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
||||||
@ -563,6 +620,7 @@ func createapi(cmd *Command, args []string) {
|
|||||||
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
|
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
|
||||||
writetofile(path.Join(apppath, "main.go"),
|
writetofile(path.Join(apppath, "main.go"),
|
||||||
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1))
|
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkEnv(appname string) (apppath, packpath string, err error) {
|
func checkEnv(appname string) (apppath, packpath string, err error) {
|
||||||
|
@ -640,7 +640,7 @@ func appendModels(cmpath, pkgpath, controllerName string, realTypes []string) {
|
|||||||
if _, ok := modelsList[pkgpath+controllerName][p+realType]; ok {
|
if _, ok := modelsList[pkgpath+controllerName][p+realType]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Printf(pkgpath + ":" + controllerName + ":" + cmpath + ":" + realType + "\n")
|
//fmt.Printf(pkgpath + ":" + controllerName + ":" + cmpath + ":" + realType + "\n")
|
||||||
_, _, mod, newRealTypes := getModel(p + realType)
|
_, _, mod, newRealTypes := getModel(p + realType)
|
||||||
modelsList[pkgpath+controllerName][p+realType] = mod
|
modelsList[pkgpath+controllerName][p+realType] = mod
|
||||||
appendModels(cmpath, pkgpath, controllerName, newRealTypes)
|
appendModels(cmpath, pkgpath, controllerName, newRealTypes)
|
||||||
|
8
run.go
8
run.go
@ -23,7 +23,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var cmdRun = &Command{
|
var cmdRun = &Command{
|
||||||
UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-docgen=true]",
|
UsageLine: "run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true]",
|
||||||
Short: "run the app which can hot compile",
|
Short: "run the app which can hot compile",
|
||||||
Long: `
|
Long: `
|
||||||
start the appname throw exec.Command
|
start the appname throw exec.Command
|
||||||
@ -47,12 +47,12 @@ when the file has changed bee will auto go build and restart the app
|
|||||||
var mainFiles ListOpts
|
var mainFiles ListOpts
|
||||||
|
|
||||||
var downdoc docValue
|
var downdoc docValue
|
||||||
var docgen docValue
|
var gendoc docValue
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdRun.Run = runApp
|
cmdRun.Run = runApp
|
||||||
cmdRun.Flag.Var(&mainFiles, "main", "specify main go files")
|
cmdRun.Flag.Var(&mainFiles, "main", "specify main go files")
|
||||||
cmdRun.Flag.Var(&docgen, "docgen", "auto generate the docs")
|
cmdRun.Flag.Var(&gendoc, "gendoc", "auto generate the docs")
|
||||||
cmdRun.Flag.Var(&downdoc, "downdoc", "auto download swagger file when not exist")
|
cmdRun.Flag.Var(&downdoc, "downdoc", "auto download swagger file when not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ func runApp(cmd *Command, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if docgen == "true" {
|
if gendoc == "true" {
|
||||||
NewWatcher(paths, files, true)
|
NewWatcher(paths, files, true)
|
||||||
Autobuild(files, true)
|
Autobuild(files, true)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user