bee api create a new api from database

This commit is contained in:
astaxie 2014-08-09 00:55:55 +08:00
parent 41dbb385e7
commit 50dfe40b94
3 changed files with 92 additions and 34 deletions

View File

@ -28,6 +28,13 @@ var cmdApiapp = &Command{
Long: `
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 appname folder has the follow struct:
@ -73,6 +80,32 @@ func main() {
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
// @Title beego Test 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() {
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) {
if len(args) != 1 {
fmt.Println("error args")
os.Exit(2)
curpath, _ := os.Getwd()
if len(args) > 1 {
cmd.Flag.Parse(args[1:])
}
apppath, packpath, err := checkEnv(args[0])
if err != nil {
fmt.Println(err)
os.Exit(2)
}
if driver == "" {
driver = "mysql"
}
if conn == "" {
}
os.MkdirAll(apppath, 0755)
fmt.Println("create app folder:", apppath)
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"))
os.Mkdir(path.Join(apppath, "docs"), 0755)
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)
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"),
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"))
writetofile(path.Join(apppath, "controllers", "object.go"),
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"))
writetofile(path.Join(apppath, "main.go"),
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1))
}
}
func checkEnv(appname string) (apppath, packpath string, err error) {

View File

@ -640,7 +640,7 @@ func appendModels(cmpath, pkgpath, controllerName string, realTypes []string) {
if _, ok := modelsList[pkgpath+controllerName][p+realType]; ok {
continue
}
fmt.Printf(pkgpath + ":" + controllerName + ":" + cmpath + ":" + realType + "\n")
//fmt.Printf(pkgpath + ":" + controllerName + ":" + cmpath + ":" + realType + "\n")
_, _, mod, newRealTypes := getModel(p + realType)
modelsList[pkgpath+controllerName][p+realType] = mod
appendModels(cmpath, pkgpath, controllerName, newRealTypes)

8
run.go
View File

@ -23,7 +23,7 @@ import (
)
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",
Long: `
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 downdoc docValue
var docgen docValue
var gendoc docValue
func init() {
cmdRun.Run = runApp
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")
}
@ -98,7 +98,7 @@ func runApp(cmd *Command, args []string) {
}
}
if docgen == "true" {
if gendoc == "true" {
NewWatcher(paths, files, true)
Autobuild(files, true)
} else {