mirror of
https://github.com/beego/bee.git
synced 2024-11-21 23:50:54 +00:00
Merge branch 'master' of github.com:astaxie/bee
This commit is contained in:
commit
acc8c4ddd8
508
apiapp.go
508
apiapp.go
@ -1,253 +1,255 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
path "path/filepath"
|
path "path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmdApiapp = &Command{
|
var cmdApiapp = &Command{
|
||||||
// CustomFlags: true,
|
// CustomFlags: true,
|
||||||
UsageLine: "api [appname]",
|
UsageLine: "api [appname]",
|
||||||
Short: "create an api application base on beego framework",
|
Short: "create an api application base on beego framework",
|
||||||
Long: `
|
Long: `
|
||||||
create an api application base on beego framework
|
create an api application base on beego framework
|
||||||
|
|
||||||
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:
|
||||||
|
|
||||||
├── conf
|
├── conf
|
||||||
│ └── app.conf
|
│ └── app.conf
|
||||||
├── controllers
|
├── controllers
|
||||||
│ └── default.go
|
│ └── default.go
|
||||||
├── main.go
|
├── main.go
|
||||||
└── models
|
└── models
|
||||||
└── object.go
|
└── object.go
|
||||||
|
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
var apiconf = `
|
var apiconf = `
|
||||||
appname = {{.Appname}}
|
appname = {{.Appname}}
|
||||||
httpport = 8080
|
httpport = 8080
|
||||||
runmode = dev
|
runmode = dev
|
||||||
autorender = false
|
autorender = false
|
||||||
copyrequestbody = true
|
copyrequestbody = true
|
||||||
`
|
`
|
||||||
var apiMaingo = `package main
|
var apiMaingo = `package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"{{.Appname}}/controllers"
|
"{{.Appname}}/controllers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Objects
|
// Objects
|
||||||
|
|
||||||
// URL HTTP Verb Functionality
|
// URL HTTP Verb Functionality
|
||||||
// /object POST Creating Objects
|
// /object POST Creating Objects
|
||||||
// /object/<objectId> GET Retrieving Objects
|
// /object/<objectId> GET Retrieving Objects
|
||||||
// /object/<objectId> PUT Updating Objects
|
// /object/<objectId> PUT Updating Objects
|
||||||
// /object GET Queries
|
// /object GET Queries
|
||||||
// /object/<objectId> DELETE Deleting Objects
|
// /object/<objectId> DELETE Deleting Objects
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
beego.RESTRouter("/object", &controllers.ObejctController{})
|
beego.RESTRouter("/object", &controllers.ObejctController{})
|
||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
var apiModels = `package models
|
var apiModels = `package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Objects map[string]*Object
|
Objects map[string]*Object
|
||||||
)
|
)
|
||||||
|
|
||||||
type Object struct {
|
type Object struct {
|
||||||
ObjectId string
|
ObjectId string
|
||||||
Score int64
|
Score int64
|
||||||
PlayerName string
|
PlayerName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Objects = make(map[string]*Object)
|
Objects = make(map[string]*Object)
|
||||||
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
|
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
|
||||||
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
|
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddOne(object Object) (ObjectId string) {
|
func AddOne(object Object) (ObjectId string) {
|
||||||
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||||
Objects[object.ObjectId] = &object
|
Objects[object.ObjectId] = &object
|
||||||
return object.ObjectId
|
return object.ObjectId
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOne(ObjectId string) (object *Object, err error) {
|
func GetOne(ObjectId string) (object *Object, err error) {
|
||||||
if v, ok := Objects[ObjectId]; ok {
|
if v, ok := Objects[ObjectId]; ok {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("ObjectId Not Exist")
|
return nil, errors.New("ObjectId Not Exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAll() map[string]*Object {
|
func GetAll() map[string]*Object {
|
||||||
return Objects
|
return Objects
|
||||||
}
|
}
|
||||||
|
|
||||||
func Update(ObjectId string, Score int64) (err error) {
|
func Update(ObjectId string, Score int64) (err error) {
|
||||||
if v, ok := Objects[ObjectId]; ok {
|
if v, ok := Objects[ObjectId]; ok {
|
||||||
v.Score = Score
|
v.Score = Score
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("ObjectId Not Exist")
|
return errors.New("ObjectId Not Exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Delete(ObjectId string) {
|
func Delete(ObjectId string) {
|
||||||
delete(Objects, ObjectId)
|
delete(Objects, ObjectId)
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
var apiControllers = `package controllers
|
var apiControllers = `package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"{{.Appname}}/models"
|
"{{.Appname}}/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ResponseInfo struct {
|
type ResponseInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ObejctController struct {
|
type ObejctController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ObejctController) Post() {
|
func (this *ObejctController) Post() {
|
||||||
var ob models.Object
|
var ob models.Object
|
||||||
json.Unmarshal(this.Ctx.RequestBody, &ob)
|
json.Unmarshal(this.Ctx.RequestBody, &ob)
|
||||||
objectid := models.AddOne(ob)
|
objectid := models.AddOne(ob)
|
||||||
this.Data["json"] = "{\"ObjectId\":\"" + objectid + "\"}"
|
this.Data["json"] = map[string]string{"ObjectId": objectid}
|
||||||
this.ServeJson()
|
this.ServeJson()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ObejctController) Get() {
|
func (this *ObejctController) Get() {
|
||||||
objectId := this.Ctx.Params[":objectId"]
|
objectId := this.Ctx.Params[":objectId"]
|
||||||
if objectId != "" {
|
if objectId != "" {
|
||||||
ob, err := models.GetOne(objectId)
|
ob, err := models.GetOne(objectId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.Data["json"] = err
|
this.Data["json"] = err
|
||||||
} else {
|
} else {
|
||||||
this.Data["json"] = ob
|
this.Data["json"] = ob
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
obs := models.GetAll()
|
obs := models.GetAll()
|
||||||
this.Data["json"] = obs
|
this.Data["json"] = obs
|
||||||
}
|
}
|
||||||
this.ServeJson()
|
this.ServeJson()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ObejctController) Put() {
|
func (this *ObejctController) Put() {
|
||||||
objectId := this.Ctx.Params[":objectId"]
|
objectId := this.Ctx.Params[":objectId"]
|
||||||
var ob models.Object
|
var ob models.Object
|
||||||
json.Unmarshal(this.Ctx.RequestBody, &ob)
|
json.Unmarshal(this.Ctx.RequestBody, &ob)
|
||||||
|
|
||||||
err := models.Update(objectId, ob.Score)
|
err := models.Update(objectId, ob.Score)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.Data["json"] = err
|
this.Data["json"] = err
|
||||||
} else {
|
} else {
|
||||||
this.Data["json"] = "update success!"
|
this.Data["json"] = "update success!"
|
||||||
}
|
}
|
||||||
this.ServeJson()
|
this.ServeJson()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *ObejctController) Delete() {
|
func (this *ObejctController) Delete() {
|
||||||
objectId := this.Ctx.Params[":objectId"]
|
objectId := this.Ctx.Params[":objectId"]
|
||||||
models.Delete(objectId)
|
models.Delete(objectId)
|
||||||
this.Data["json"] = "delete success!"
|
this.Data["json"] = "delete success!"
|
||||||
this.ServeJson()
|
this.ServeJson()
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdApiapp.Run = createapi
|
cmdApiapp.Run = createapi
|
||||||
}
|
}
|
||||||
|
|
||||||
func createapi(cmd *Command, args []string) {
|
func createapi(cmd *Command, args []string) {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
fmt.Println("error args")
|
fmt.Println("error args")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
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)
|
||||||
}
|
}
|
||||||
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)
|
||||||
fmt.Println("create conf:", path.Join(apppath, "conf"))
|
fmt.Println("create conf:", path.Join(apppath, "conf"))
|
||||||
os.Mkdir(path.Join(apppath, "controllers"), 0755)
|
os.Mkdir(path.Join(apppath, "controllers"), 0755)
|
||||||
fmt.Println("create controllers:", path.Join(apppath, "controllers"))
|
fmt.Println("create controllers:", path.Join(apppath, "controllers"))
|
||||||
os.Mkdir(path.Join(apppath, "models"), 0755)
|
os.Mkdir(path.Join(apppath, "models"), 0755)
|
||||||
fmt.Println("create models:", path.Join(apppath, "models"))
|
fmt.Println("create models:", path.Join(apppath, "models"))
|
||||||
|
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
||||||
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
|
fmt.Println("create tests:", path.Join(apppath, "tests"))
|
||||||
writetofile(path.Join(apppath, "conf", "app.conf"),
|
|
||||||
strings.Replace(apiconf, "{{.Appname}}", args[0], -1))
|
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
|
||||||
|
writetofile(path.Join(apppath, "conf", "app.conf"),
|
||||||
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
|
strings.Replace(apiconf, "{{.Appname}}", args[0], -1))
|
||||||
writetofile(path.Join(apppath, "controllers", "default.go"),
|
|
||||||
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
|
||||||
|
writetofile(path.Join(apppath, "controllers", "default.go"),
|
||||||
fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go"))
|
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
||||||
writetofile(path.Join(apppath, "models", "object.go"), apiModels)
|
|
||||||
|
fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go"))
|
||||||
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
|
writetofile(path.Join(apppath, "models", "object.go"), apiModels)
|
||||||
writetofile(path.Join(apppath, "main.go"),
|
|
||||||
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1))
|
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) {
|
}
|
||||||
curpath, err := os.Getwd()
|
|
||||||
if err != nil {
|
func checkEnv(appname string) (apppath, packpath string, err error) {
|
||||||
return
|
curpath, err := os.Getwd()
|
||||||
}
|
if err != nil {
|
||||||
|
return
|
||||||
gopath := os.Getenv("GOPATH")
|
}
|
||||||
Debugf("gopath:%s", gopath)
|
|
||||||
if gopath == "" {
|
gopath := os.Getenv("GOPATH")
|
||||||
err = fmt.Errorf("you should set GOPATH in the env")
|
Debugf("gopath:%s", gopath)
|
||||||
return
|
if gopath == "" {
|
||||||
}
|
err = fmt.Errorf("you should set GOPATH in the env")
|
||||||
|
return
|
||||||
appsrcpath := ""
|
}
|
||||||
haspath := false
|
|
||||||
wgopath := path.SplitList(gopath)
|
appsrcpath := ""
|
||||||
for _, wg := range wgopath {
|
haspath := false
|
||||||
wg = path.Join(wg, "src")
|
wgopath := path.SplitList(gopath)
|
||||||
|
for _, wg := range wgopath {
|
||||||
if path.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
|
wg = path.Join(wg, "src")
|
||||||
haspath = true
|
|
||||||
appsrcpath = wg
|
if path.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
|
||||||
break
|
haspath = true
|
||||||
}
|
appsrcpath = wg
|
||||||
}
|
break
|
||||||
|
}
|
||||||
if !haspath {
|
}
|
||||||
err = fmt.Errorf("can't create application outside of GOPATH `%s`\n"+
|
|
||||||
"you first should `cd $GOPATH%ssrc` then use create\n", gopath, string(path.Separator))
|
if !haspath {
|
||||||
return
|
err = fmt.Errorf("can't create application outside of GOPATH `%s`\n"+
|
||||||
}
|
"you first should `cd $GOPATH%ssrc` then use create\n", gopath, string(path.Separator))
|
||||||
apppath = path.Join(curpath, appname)
|
return
|
||||||
|
}
|
||||||
if _, e := os.Stat(apppath); os.IsNotExist(e) == false {
|
apppath = path.Join(curpath, appname)
|
||||||
err = fmt.Errorf("path `%s` exists, can not create app without remove it\n", apppath)
|
|
||||||
return
|
if _, e := os.Stat(apppath); os.IsNotExist(e) == false {
|
||||||
}
|
err = fmt.Errorf("path `%s` exists, can not create app without remove it\n", apppath)
|
||||||
packpath = strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/")
|
return
|
||||||
return
|
}
|
||||||
}
|
packpath = strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
1
bee.go
1
bee.go
@ -76,6 +76,7 @@ var commands = []*Command{
|
|||||||
cmdPack,
|
cmdPack,
|
||||||
cmdApiapp,
|
cmdApiapp,
|
||||||
cmdRouter,
|
cmdRouter,
|
||||||
|
cmdTest,
|
||||||
//cmdReStart,
|
//cmdReStart,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
82
test.go
Normal file
82
test.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
path "path/filepath"
|
||||||
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cmdTest = &Command{
|
||||||
|
UsageLine: "test [appname]",
|
||||||
|
Short: "test the app",
|
||||||
|
Long: ``,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
cmdTest.Run = testApp
|
||||||
|
}
|
||||||
|
|
||||||
|
var started= make(chan bool)
|
||||||
|
|
||||||
|
func testApp(cmd *Command, args []string) {
|
||||||
|
if len(args) != 1 {
|
||||||
|
colorLog("[ERRO] Cannot start running[ %s ]\n",
|
||||||
|
"argument 'appname' is missing")
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
crupath, _ := os.Getwd()
|
||||||
|
Debugf("current path:%s\n", crupath)
|
||||||
|
|
||||||
|
err := loadConfig()
|
||||||
|
if err != nil {
|
||||||
|
colorLog("[ERRO] Fail to parse bee.json[ %s ]", err)
|
||||||
|
}
|
||||||
|
var paths []string
|
||||||
|
paths = append(paths,
|
||||||
|
path.Join(crupath, conf.DirStruct.Controllers),
|
||||||
|
path.Join(crupath, conf.DirStruct.Models),
|
||||||
|
path.Join(crupath, "./")) // Current path.
|
||||||
|
// Because monitor files has some issues, we watch current directory
|
||||||
|
// and ignore non-go files.
|
||||||
|
paths = append(paths, conf.DirStruct.Others...)
|
||||||
|
paths = append(paths, conf.MainFiles.Others...)
|
||||||
|
|
||||||
|
NewWatcher(paths)
|
||||||
|
appname = args[0]
|
||||||
|
Autobuild()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-started:
|
||||||
|
runTest()
|
||||||
|
Kill()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func runTest(){
|
||||||
|
colorLog("[INFO] Start testing...\n")
|
||||||
|
time.Sleep(time.Second*5)
|
||||||
|
path, _ := os.Getwd()
|
||||||
|
os.Chdir(path+"/tests")
|
||||||
|
|
||||||
|
var err error
|
||||||
|
icmd := exec.Command("go", "test")
|
||||||
|
var out,errbuffer bytes.Buffer
|
||||||
|
icmd.Stdout = &out
|
||||||
|
icmd.Stderr = &errbuffer
|
||||||
|
colorLog("[INFO] ============== Test Begin ===================\n")
|
||||||
|
err = icmd.Run()
|
||||||
|
colorLog(out.String())
|
||||||
|
colorLog(errbuffer.String())
|
||||||
|
colorLog("[INFO] ============== Test End ===================\n")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
colorLog("[ERRO] ============== Test failed ===================\n")
|
||||||
|
colorLog("[ERRO] " ,err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
colorLog("[SUCC] Test finish\n")
|
||||||
|
}
|
4
util.go
4
util.go
@ -50,6 +50,10 @@ const (
|
|||||||
// Errors have to surrounded by "[ " and " ]"(space).
|
// Errors have to surrounded by "[ " and " ]"(space).
|
||||||
func colorLog(format string, a ...interface{}) {
|
func colorLog(format string, a ...interface{}) {
|
||||||
log := fmt.Sprintf(format, a...)
|
log := fmt.Sprintf(format, a...)
|
||||||
|
if len(log) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
var clog string
|
var clog string
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user