mirror of
https://github.com/beego/bee.git
synced 2024-11-21 23:50:54 +00:00
update apiapp
This commit is contained in:
parent
f839552b8d
commit
550cf6cb71
514
apiapp.go
514
apiapp.go
@ -1,183 +1,183 @@
|
|||||||
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.Router("/ping", &controllers.ObejctController{},"get:Ping")
|
beego.Router("/ping", &controllers.ObejctController{},"get:Ping")
|
||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
var apiModels = `package models
|
var apiModels = `package models
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
Objects map[string]*Object
|
|
||||||
)
|
|
||||||
|
|
||||||
type Object struct {
|
|
||||||
ObjectId string
|
|
||||||
Score int64
|
|
||||||
PlayerName string
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
Objects = make(map[string]*Object)
|
|
||||||
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
|
|
||||||
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddOne(object Object) (ObjectId string) {
|
|
||||||
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
||||||
Objects[object.ObjectId] = &object
|
|
||||||
return object.ObjectId
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetOne(ObjectId string) (object *Object, err error) {
|
|
||||||
if v, ok := Objects[ObjectId]; ok {
|
|
||||||
return v, nil
|
|
||||||
}
|
|
||||||
return nil, errors.New("ObjectId Not Exist")
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAll() map[string]*Object {
|
|
||||||
return Objects
|
|
||||||
}
|
|
||||||
|
|
||||||
func Update(ObjectId string, Score int64) (err error) {
|
|
||||||
if v, ok := Objects[ObjectId]; ok {
|
|
||||||
v.Score = Score
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return errors.New("ObjectId Not Exist")
|
|
||||||
}
|
|
||||||
|
|
||||||
func Delete(ObjectId string) {
|
|
||||||
delete(Objects, ObjectId)
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
var apiControllers = `package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/astaxie/beego"
|
|
||||||
"{{.Appname}}/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ResponseInfo struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
type ObejctController struct {
|
|
||||||
beego.Controller
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *ObejctController) Post() {
|
|
||||||
var ob models.Object
|
|
||||||
json.Unmarshal(this.Ctx.RequestBody, &ob)
|
|
||||||
objectid := models.AddOne(ob)
|
|
||||||
this.Data["json"] = map[string]string{"ObjectId": objectid}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *ObejctController) Get() {
|
|
||||||
objectId := this.Ctx.Params[":objectId"]
|
|
||||||
if objectId != "" {
|
|
||||||
ob, err := models.GetOne(objectId)
|
|
||||||
if err != nil {
|
|
||||||
this.Data["json"] = err
|
|
||||||
} else {
|
|
||||||
this.Data["json"] = ob
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
obs := models.GetAll()
|
|
||||||
this.Data["json"] = obs
|
|
||||||
}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *ObejctController) Put() {
|
|
||||||
objectId := this.Ctx.Params[":objectId"]
|
|
||||||
var ob models.Object
|
|
||||||
json.Unmarshal(this.Ctx.RequestBody, &ob)
|
|
||||||
|
|
||||||
err := models.Update(objectId, ob.Score)
|
|
||||||
if err != nil {
|
|
||||||
this.Data["json"] = err
|
|
||||||
} else {
|
|
||||||
this.Data["json"] = "update success!"
|
|
||||||
}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *ObejctController) Delete() {
|
|
||||||
objectId := this.Ctx.Params[":objectId"]
|
|
||||||
models.Delete(objectId)
|
|
||||||
this.Data["json"] = "delete success!"
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *ObejctController) Ping() {
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Objects map[string]*Object
|
||||||
|
)
|
||||||
|
|
||||||
|
type Object struct {
|
||||||
|
ObjectId string
|
||||||
|
Score int64
|
||||||
|
PlayerName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Objects = make(map[string]*Object)
|
||||||
|
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
|
||||||
|
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddOne(object Object) (ObjectId string) {
|
||||||
|
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||||
|
Objects[object.ObjectId] = &object
|
||||||
|
return object.ObjectId
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOne(ObjectId string) (object *Object, err error) {
|
||||||
|
if v, ok := Objects[ObjectId]; ok {
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New("ObjectId Not Exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAll() map[string]*Object {
|
||||||
|
return Objects
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(ObjectId string, Score int64) (err error) {
|
||||||
|
if v, ok := Objects[ObjectId]; ok {
|
||||||
|
v.Score = Score
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New("ObjectId Not Exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(ObjectId string) {
|
||||||
|
delete(Objects, ObjectId)
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
var apiControllers = `package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"{{.Appname}}/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ResponseInfo struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type ObejctController struct {
|
||||||
|
beego.Controller
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ObejctController) Post() {
|
||||||
|
var ob models.Object
|
||||||
|
json.Unmarshal(this.Ctx.Input.RequestBody, &ob)
|
||||||
|
objectid := models.AddOne(ob)
|
||||||
|
this.Data["json"] = map[string]string{"ObjectId": objectid}
|
||||||
|
this.ServeJson()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ObejctController) Get() {
|
||||||
|
objectId := this.Ctx.Params[":objectId"]
|
||||||
|
if objectId != "" {
|
||||||
|
ob, err := models.GetOne(objectId)
|
||||||
|
if err != nil {
|
||||||
|
this.Data["json"] = err
|
||||||
|
} else {
|
||||||
|
this.Data["json"] = ob
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
obs := models.GetAll()
|
||||||
|
this.Data["json"] = obs
|
||||||
|
}
|
||||||
|
this.ServeJson()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ObejctController) Put() {
|
||||||
|
objectId := this.Ctx.Params[":objectId"]
|
||||||
|
var ob models.Object
|
||||||
|
json.Unmarshal(this.Ctx.Input.RequestBody, &ob)
|
||||||
|
|
||||||
|
err := models.Update(objectId, ob.Score)
|
||||||
|
if err != nil {
|
||||||
|
this.Data["json"] = err
|
||||||
|
} else {
|
||||||
|
this.Data["json"] = "update success!"
|
||||||
|
}
|
||||||
|
this.ServeJson()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ObejctController) Delete() {
|
||||||
|
objectId := this.Ctx.Input.Params[":objectId"]
|
||||||
|
models.Delete(objectId)
|
||||||
|
this.Data["json"] = "delete success!"
|
||||||
|
this.ServeJson()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ObejctController) Ping() {
|
||||||
this.Ctx.WriteString("pong")
|
this.Ctx.WriteString("pong")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
`
|
||||||
|
|
||||||
`
|
|
||||||
|
|
||||||
var apiTests = `package tests
|
var apiTests = `package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -198,88 +198,88 @@ func TestHelloWorld(t *testing.T) {
|
|||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
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)
|
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
||||||
fmt.Println("create tests:", path.Join(apppath, "tests"))
|
fmt.Println("create tests:", path.Join(apppath, "tests"))
|
||||||
|
|
||||||
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
|
fmt.Println("create conf app.conf:", path.Join(apppath, "conf", "app.conf"))
|
||||||
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))
|
||||||
|
|
||||||
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
|
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
|
||||||
writetofile(path.Join(apppath, "controllers", "default.go"),
|
writetofile(path.Join(apppath, "controllers", "default.go"),
|
||||||
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
||||||
|
|
||||||
fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go"))
|
fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go"))
|
||||||
writetofile(path.Join(apppath, "tests", "default_test.go"),
|
writetofile(path.Join(apppath, "tests", "default_test.go"),
|
||||||
apiTests)
|
apiTests)
|
||||||
|
|
||||||
fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go"))
|
fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go"))
|
||||||
writetofile(path.Join(apppath, "models", "object.go"), apiModels)
|
writetofile(path.Join(apppath, "models", "object.go"), apiModels)
|
||||||
|
|
||||||
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) {
|
||||||
curpath, err := os.Getwd()
|
curpath, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gopath := os.Getenv("GOPATH")
|
gopath := os.Getenv("GOPATH")
|
||||||
Debugf("gopath:%s", gopath)
|
Debugf("gopath:%s", gopath)
|
||||||
if gopath == "" {
|
if gopath == "" {
|
||||||
err = fmt.Errorf("you should set GOPATH in the env")
|
err = fmt.Errorf("you should set GOPATH in the env")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
appsrcpath := ""
|
appsrcpath := ""
|
||||||
haspath := false
|
haspath := false
|
||||||
wgopath := path.SplitList(gopath)
|
wgopath := path.SplitList(gopath)
|
||||||
for _, wg := range wgopath {
|
for _, wg := range wgopath {
|
||||||
wg = path.Join(wg, "src")
|
wg = path.Join(wg, "src")
|
||||||
|
|
||||||
if path.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
|
if path.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
|
||||||
haspath = true
|
haspath = true
|
||||||
appsrcpath = wg
|
appsrcpath = wg
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !haspath {
|
if !haspath {
|
||||||
err = fmt.Errorf("can't create application outside of GOPATH `%s`\n"+
|
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))
|
"you first should `cd $GOPATH%ssrc` then use create\n", gopath, string(path.Separator))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
apppath = path.Join(curpath, appname)
|
apppath = path.Join(curpath, appname)
|
||||||
|
|
||||||
if _, e := os.Stat(apppath); os.IsNotExist(e) == false {
|
if _, e := os.Stat(apppath); os.IsNotExist(e) == false {
|
||||||
err = fmt.Errorf("path `%s` exists, can not create app without remove it\n", apppath)
|
err = fmt.Errorf("path `%s` exists, can not create app without remove it\n", apppath)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
packpath = strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/")
|
packpath = strings.Join(strings.Split(apppath[len(appsrcpath)+1:], string(path.Separator)), "/")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user