diff --git a/.gitignore b/.gitignore index ffab5d0..409a39f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ _testmain.go bee *.exe~ .goxc.local.json + +#IDEA +.idea diff --git a/apiapp.go b/apiapp.go index 8ae376e..66dc7db 100644 --- a/apiapp.go +++ b/apiapp.go @@ -42,19 +42,23 @@ on the existing database. The command 'api' creates a folder named [appname] and inside the folder deploy the following files/directories structure: + . ├── conf - │ └── app.conf + │   └── app.conf ├── controllers - │ └── object.go - │ └── user.go - ├── routers - │ └── router.go - ├── tests - │ └── default_test.go + │   ├── object_controller.go + │   └── user_controller.go + ├── docs + │   └── doc.go ├── main.go - └── models - └── object.go - └── user.go + ├── models + │   ├── object.go + │   └── user.go + ├── routers + │   └── router.go + ├── structure + └── tests + └── default_test.go `, } @@ -199,53 +203,40 @@ func Delete(ObjectId string) { var apiModels2 = `package models import ( + "{{.Appname}}/structures" "errors" "strconv" "time" ) var ( - UserList map[string]*User + UserList map[string]*structures.User ) func init() { - UserList = make(map[string]*User) - u := User{"user_11111", "astaxie", "11111", Profile{"male", 20, "Singapore", "astaxie@gmail.com"}} + UserList = make(map[string]*structures.User) + u := structures.User{"user_11111", "astaxie", "11111", structures.Profile{"male", 20, "Singapore", "astaxie@gmail.com"}} UserList["user_11111"] = &u } -type User struct { - Id string - Username string - Password string - Profile Profile -} - -type Profile struct { - Gender string - Age int - Address string - Email string -} - -func AddUser(u User) string { +func AddUser(u structures.User) string { u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10) UserList[u.Id] = &u return u.Id } -func GetUser(uid string) (u *User, err error) { +func GetUser(uid string) (u *structures.User, err error) { if u, ok := UserList[uid]; ok { return u, nil } return nil, errors.New("User not exists") } -func GetAllUsers() map[string]*User { +func GetAllUsers() map[string]*structures.User { return UserList } -func UpdateUser(uid string, uu *User) (a *User, err error) { +func UpdateUser(uid string, uu *structures.User) (a *structures.User, err error) { if u, ok := UserList[uid]; ok { if uu.Username != "" { u.Username = uu.Username @@ -538,6 +529,23 @@ func TestGet(t *testing.T) { ` +var apistructures = `package structures + +type User struct { + Id string + Username string + Password string + Profile Profile +} + +type Profile struct { + Gender string + Age int + Address string + Email string +} + +` func init() { cmdApiapp.Run = createapi cmdApiapp.Flag.Var(&tables, "tables", "specify tables to generate model") @@ -574,6 +582,8 @@ func createapi(cmd *Command, args []string) int { fmt.Println("create docs:", path.Join(apppath, "docs")) os.Mkdir(path.Join(apppath, "tests"), 0755) fmt.Println("create tests:", path.Join(apppath, "tests")) + os.Mkdir(path.Join(apppath, "helpers"), 0755) + fmt.Println("create structure:",path.Join(apppath, "helpers")) fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf")) writetofile(path.Join(apppath, "conf", "app.conf"), @@ -604,14 +614,16 @@ func createapi(cmd *Command, args []string) int { 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 routers:",path.Join(apppath, "routers")) + os.Mkdir(path.Join(apppath, "structures"), 0755) + fmt.Println("create structure:",path.Join(apppath, "structures")) - fmt.Println("create controllers object.go:", path.Join(apppath, "controllers", "object.go")) - writetofile(path.Join(apppath, "controllers", "object.go"), + fmt.Println("create controllers object_controller.go:", path.Join(apppath, "controllers", "object_controller.go")) + writetofile(path.Join(apppath, "controllers", "object_controller.go"), strings.Replace(apiControllers, "{{.Appname}}", packpath, -1)) - fmt.Println("create controllers user.go:", path.Join(apppath, "controllers", "user.go")) - writetofile(path.Join(apppath, "controllers", "user.go"), + fmt.Println("create controllers user_controller.go:", path.Join(apppath, "controllers", "user_controller.go")) + writetofile(path.Join(apppath, "controllers", "user_controller.go"), strings.Replace(apiControllers2, "{{.Appname}}", packpath, -1)) fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go")) @@ -626,7 +638,11 @@ func createapi(cmd *Command, args []string) int { writetofile(path.Join(apppath, "models", "object.go"), apiModels) fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go")) - writetofile(path.Join(apppath, "models", "user.go"), apiModels2) + writetofile(path.Join(apppath, "models", "user.go"), + strings.Replace(apiModels2, "{{.Appname}}", packpath, -1)) + + fmt.Println("create structures user_structure.go:", path.Join(apppath, "structures", "user_structure.go")) + writetofile(path.Join(apppath, "structures", "user_structure.go"), apistructures) fmt.Println("create docs doc.go:", path.Join(apppath, "docs", "doc.go")) writetofile(path.Join(apppath, "docs", "doc.go"), "package docs") diff --git a/g_controllers.go b/g_controllers.go index 49b5fd9..87b11dc 100644 --- a/g_controllers.go +++ b/g_controllers.go @@ -25,13 +25,14 @@ import ( // func generateController(cname, crupath string) { p, f := path.Split(cname) + fileName := strings.Title(f) + "_controllers" controllerName := strings.Title(f) packageName := "controllers" if p != "" { i := strings.LastIndex(p[:len(p)-1], "/") packageName = p[i+1 : len(p)-1] } - ColorLog("[INFO] Using '%s' as controller name\n", controllerName) + ColorLog("[INFO] Using '%s' as controller name\n", fileName) ColorLog("[INFO] Using '%s' as package name\n", packageName) fp := path.Join(crupath, "controllers", p) if _, err := os.Stat(fp); os.IsNotExist(err) { @@ -41,7 +42,7 @@ func generateController(cname, crupath string) { os.Exit(2) } } - fpath := path.Join(fp, strings.ToLower(controllerName)+".go") + fpath := path.Join(fp, strings.ToLower(fileName)+".go") if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil { defer f.Close() modelPath := path.Join(crupath, "models", strings.ToLower(controllerName)+".go") diff --git a/new.go b/new.go index bba644d..ceda69e 100644 --- a/new.go +++ b/new.go @@ -30,22 +30,25 @@ Creates a Beego application for the given app name in the current directory. The command 'new' creates a folder named [appname] and inside the folder deploy the following files/directories structure: - |- main.go - |- conf - |- app.conf - |- controllers - |- default.go - |- models - |- routers - |- router.go - |- tests - |- default_test.go - |- static - |- js - |- css - |- img - |- views - index.tpl + . + ├── conf + │   └── app.conf + ├── controllers + │   └── index_controller.go + ├── helpers + ├── main.go + ├── models + ├── routers + │   └── router.go + ├── static + │   ├── css + │   ├── img + │   └── js + ├── structure + ├── tests + │   └── default_test.go + └── views + └── index.tpl `, } @@ -116,12 +119,16 @@ func createApp(cmd *Command, args []string) int { fmt.Println(path.Join(apppath, "conf") + string(path.Separator)) os.Mkdir(path.Join(apppath, "controllers"), 0755) fmt.Println(path.Join(apppath, "controllers") + string(path.Separator)) + os.Mkdir(path.Join(apppath, "helpers"), 0755) + fmt.Println(path.Join(apppath, "helpers") + string(path.Separator)) os.Mkdir(path.Join(apppath, "models"), 0755) fmt.Println(path.Join(apppath, "models") + string(path.Separator)) 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(path.Join(apppath, "tests") + string(path.Separator)) + os.Mkdir(path.Join(apppath, "structures"), 0755) + fmt.Println(path.Join(apppath, "structures") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static"), 0755) fmt.Println(path.Join(apppath, "static") + string(path.Separator)) os.Mkdir(path.Join(apppath, "static", "js"), 0755) @@ -135,8 +142,8 @@ func createApp(cmd *Command, args []string) int { fmt.Println(path.Join(apppath, "conf", "app.conf")) writetofile(path.Join(apppath, "conf", "app.conf"), strings.Replace(appconf, "{{.Appname}}", args[0], -1)) - fmt.Println(path.Join(apppath, "controllers", "default.go")) - writetofile(path.Join(apppath, "controllers", "default.go"), controllers) + fmt.Println(path.Join(apppath, "controllers", "index_controller.go")) + writetofile(path.Join(apppath, "controllers", "index_controller.go"), controllers) fmt.Println(path.Join(apppath, "views", "index.tpl")) writetofile(path.Join(apppath, "views", "index.tpl"), indextpl)