mirror of
https://github.com/beego/bee.git
synced 2024-11-22 05:00:54 +00:00
修改bee hprose 参数,只发布hprose服务,不包含RESTful Api。
This commit is contained in:
parent
eaae7694b8
commit
adf84eb060
77
README.md
77
README.md
@ -5,15 +5,34 @@ bee
|
|||||||
|
|
||||||
Bee is a command line tool facilitating development with beego framework.
|
Bee is a command line tool facilitating development with beego framework.
|
||||||
|
|
||||||
|
|
||||||
|
## 新增采用 Hprose 发布RPC服务
|
||||||
|
|
||||||
|
go get github.com/Lao-liu/bee
|
||||||
|
|
||||||
|
bee hprose DbApi -conn=root:@tcp\(127.0.0.1:3306\)/test
|
||||||
|
OR
|
||||||
|
bee hprose DbApi -conn=root:@tcp\(127.0.0.1:3306\)/test -tables=tablename
|
||||||
|
|
||||||
|
cd DbApi
|
||||||
|
|
||||||
|
bee run
|
||||||
|
|
||||||
|
#### 浏览器打开 http://127.0.0.1:8080
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Go version >= 1.1.
|
- Go version >= 1.1.
|
||||||
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Begin by installing `bee` using `go get` command.
|
Begin by installing `bee` using `go get` command.
|
||||||
|
|
||||||
go get github.com/beego/bee
|
go get github.com/beego/bee
|
||||||
|
|
||||||
Then you can add `bee` binary to PATH environment variable in your `~/.bashrc` or `~/.bash_profile` file:
|
Then you can add `bee` binary to PATH environment variable in your `~/.bashrc` or `~/.bash_profile` file:
|
||||||
|
|
||||||
@ -23,20 +42,20 @@ export PATH=$PATH:<your_main_gopath>/bin/bee
|
|||||||
|
|
||||||
> If you already have `bee` installed, updating `bee` is simple:
|
> If you already have `bee` installed, updating `bee` is simple:
|
||||||
|
|
||||||
go get -u github.com/beego/bee
|
go get -u github.com/beego/bee
|
||||||
|
|
||||||
## Basic commands
|
## Basic commands
|
||||||
|
|
||||||
Bee provides a variety of commands which can be helpful at various stage of development. The top level commands include:
|
Bee provides a variety of commands which can be helpful at various stage of development. The top level commands include:
|
||||||
|
|
||||||
new create an application base on beego framework
|
new create an application base on beego framework
|
||||||
run run the app which can hot compile
|
run run the app which can hot compile
|
||||||
pack compress an beego project
|
pack compress an beego project
|
||||||
api create an api application base on beego framework
|
api create an api application base on beego framework
|
||||||
bale packs non-Go files to Go source files
|
bale packs non-Go files to Go source files
|
||||||
version show the bee & beego version
|
version show the bee & beego version
|
||||||
generate source code generator
|
generate source code generator
|
||||||
migrate run database migrations
|
migrate run database migrations
|
||||||
|
|
||||||
## bee version
|
## bee version
|
||||||
|
|
||||||
@ -121,26 +140,26 @@ bee api [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/
|
|||||||
-conn: the connection string used by the driver, the default is ''
|
-conn: the connection string used by the driver, the default is ''
|
||||||
e.g. for mysql: root:@tcp(127.0.0.1:3306)/test
|
e.g. for mysql: root:@tcp(127.0.0.1:3306)/test
|
||||||
e.g. for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
|
e.g. for postgres: postgres://postgres:postgres@127.0.0.1:5432/postgres
|
||||||
|
|
||||||
if conn is empty will create a example api application. otherwise generate api application based on an existing database.
|
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:
|
||||||
|
|
||||||
├── conf
|
├── conf
|
||||||
│ └── app.conf
|
│ └── app.conf
|
||||||
├── controllers
|
├── controllers
|
||||||
│ └── object.go
|
│ └── object.go
|
||||||
│ └── user.go
|
│ └── user.go
|
||||||
├── routers
|
├── routers
|
||||||
│ └── router.go
|
│ └── router.go
|
||||||
├── tests
|
├── tests
|
||||||
│ └── default_test.go
|
│ └── default_test.go
|
||||||
├── main.go
|
├── main.go
|
||||||
└── models
|
└── models
|
||||||
└── object.go
|
└── object.go
|
||||||
└── user.go
|
└── user.go
|
||||||
```
|
```
|
||||||
|
|
||||||
## bee bale
|
## bee bale
|
||||||
@ -211,7 +230,7 @@ bee generate view [viewpath]
|
|||||||
bee generate migration [migrationfile] [-fields=""]
|
bee generate migration [migrationfile] [-fields=""]
|
||||||
generate migration file for making database schema update
|
generate migration file for making database schema update
|
||||||
-fields: a list of table fields. Format: field:type, ...
|
-fields: a list of table fields. Format: field:type, ...
|
||||||
|
|
||||||
bee generate docs
|
bee generate docs
|
||||||
generate swagger doc file
|
generate swagger doc file
|
||||||
|
|
||||||
@ -258,13 +277,13 @@ usage: bee run [appname] [watchall] [-main=*.go] [-downdoc=true] [-gendoc=true]
|
|||||||
start the appname throw exec.Command
|
start the appname throw exec.Command
|
||||||
|
|
||||||
then start a inotify watch for current dir
|
then start a inotify watch for current dir
|
||||||
|
|
||||||
when the file has changed bee will auto go build and restart the app
|
when the file has changed bee will auto go build and restart the app
|
||||||
|
|
||||||
file changed
|
file changed
|
||||||
|
|
|
|
||||||
check if it's go file
|
check if it's go file
|
||||||
|
|
|
|
||||||
yes no
|
yes no
|
||||||
| |
|
| |
|
||||||
go build do nothing
|
go build do nothing
|
||||||
|
@ -160,8 +160,6 @@ func genHprose(dbms, connStr string, mode byte, selectedTableNames map[string]bo
|
|||||||
tables := getTableObjects(tableNames, db, trans)
|
tables := getTableObjects(tableNames, db, trans)
|
||||||
mvcPath := new(MvcPath)
|
mvcPath := new(MvcPath)
|
||||||
mvcPath.ModelPath = path.Join(currpath, "models")
|
mvcPath.ModelPath = path.Join(currpath, "models")
|
||||||
mvcPath.ControllerPath = path.Join(currpath, "controllers")
|
|
||||||
mvcPath.RouterPath = path.Join(currpath, "routers")
|
|
||||||
createPaths(mode, mvcPath)
|
createPaths(mode, mvcPath)
|
||||||
pkgPath := getPackagePath(currpath)
|
pkgPath := getPackagePath(currpath)
|
||||||
writeHproseSourceFiles(pkgPath, tables, mode, mvcPath, selectedTableNames)
|
writeHproseSourceFiles(pkgPath, tables, mode, mvcPath, selectedTableNames)
|
||||||
@ -179,14 +177,6 @@ func writeHproseSourceFiles(pkgPath string, tables []*Table, mode byte, paths *M
|
|||||||
ColorLog("[INFO] Creating model files...\n")
|
ColorLog("[INFO] Creating model files...\n")
|
||||||
writeHproseModelFiles(tables, paths.ModelPath, selectedTables)
|
writeHproseModelFiles(tables, paths.ModelPath, selectedTables)
|
||||||
}
|
}
|
||||||
if (O_CONTROLLER & mode) == O_CONTROLLER {
|
|
||||||
ColorLog("[INFO] Creating controller files...\n")
|
|
||||||
writeHproseControllerFiles(tables, paths.ControllerPath, selectedTables, pkgPath)
|
|
||||||
}
|
|
||||||
if (O_ROUTER & mode) == O_ROUTER {
|
|
||||||
ColorLog("[INFO] Creating router files...\n")
|
|
||||||
writeHproseRouterFile(tables, paths.RouterPath, selectedTables, pkgPath)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeHproseModelFiles generates model files
|
// writeHproseModelFiles generates model files
|
||||||
@ -248,105 +238,6 @@ func writeHproseModelFiles(tables []*Table, mPath string, selectedTables map[str
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeHproseControllerFiles generates controller files
|
|
||||||
func writeHproseControllerFiles(tables []*Table, cPath string, selectedTables map[string]bool, pkgPath string) {
|
|
||||||
for _, tb := range tables {
|
|
||||||
// if selectedTables map is not nil and this table is not selected, ignore it
|
|
||||||
if selectedTables != nil {
|
|
||||||
if _, selected := selectedTables[tb.Name]; !selected {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if tb.Pk == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
filename := getFileName(tb.Name)
|
|
||||||
fpath := path.Join(cPath, filename+".go")
|
|
||||||
var f *os.File
|
|
||||||
var err error
|
|
||||||
if isExist(fpath) {
|
|
||||||
ColorLog("[WARN] %v is exist, do you want to overwrite it? Yes or No?\n", fpath)
|
|
||||||
if askForConfirmation() {
|
|
||||||
f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666)
|
|
||||||
if err != nil {
|
|
||||||
ColorLog("[WARN] %v\n", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ColorLog("[WARN] skip create file\n")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
f, err = os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0666)
|
|
||||||
if err != nil {
|
|
||||||
ColorLog("[WARN] %v\n", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fileStr := strings.Replace(HPROSE_CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1)
|
|
||||||
fileStr = strings.Replace(fileStr, "{{pkgPath}}", pkgPath, -1)
|
|
||||||
if _, err := f.WriteString(fileStr); err != nil {
|
|
||||||
ColorLog("[ERRO] Could not write controller file to %s\n", fpath)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
f.Close()
|
|
||||||
ColorLog("[INFO] controller => %s\n", fpath)
|
|
||||||
formatSourceCode(fpath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeHproseRouterFile generates router file
|
|
||||||
func writeHproseRouterFile(tables []*Table, rPath string, selectedTables map[string]bool, pkgPath string) {
|
|
||||||
var nameSpaces []string
|
|
||||||
for _, tb := range tables {
|
|
||||||
// if selectedTables map is not nil and this table is not selected, ignore it
|
|
||||||
if selectedTables != nil {
|
|
||||||
if _, selected := selectedTables[tb.Name]; !selected {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if tb.Pk == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// add name spaces
|
|
||||||
nameSpace := strings.Replace(HPROSE_NAMESPACE_TPL, "{{nameSpace}}", tb.Name, -1)
|
|
||||||
nameSpace = strings.Replace(nameSpace, "{{ctrlName}}", camelCase(tb.Name), -1)
|
|
||||||
nameSpaces = append(nameSpaces, nameSpace)
|
|
||||||
}
|
|
||||||
// add export controller
|
|
||||||
fpath := path.Join(rPath, "router.go")
|
|
||||||
routerStr := strings.Replace(HPROSE_ROUTER_TPL, "{{nameSpaces}}", strings.Join(nameSpaces, ""), 1)
|
|
||||||
routerStr = strings.Replace(routerStr, "{{pkgPath}}", pkgPath, 1)
|
|
||||||
var f *os.File
|
|
||||||
var err error
|
|
||||||
if isExist(fpath) {
|
|
||||||
ColorLog("[WARN] %v is exist, do you want to overwrite it? Yes or No?\n", fpath)
|
|
||||||
if askForConfirmation() {
|
|
||||||
f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666)
|
|
||||||
if err != nil {
|
|
||||||
ColorLog("[WARN] %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ColorLog("[WARN] skip create file\n")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
f, err = os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0666)
|
|
||||||
if err != nil {
|
|
||||||
ColorLog("[WARN] %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, err := f.WriteString(routerStr); err != nil {
|
|
||||||
ColorLog("[ERRO] Could not write router file to %s\n", fpath)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
f.Close()
|
|
||||||
ColorLog("[INFO] router => %s\n", fpath)
|
|
||||||
formatSourceCode(fpath)
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HPROSE_ADDFUNCTION = `
|
HPROSE_ADDFUNCTION = `
|
||||||
// publish about {{modelName}} function
|
// publish about {{modelName}} function
|
||||||
@ -501,193 +392,5 @@ func Delete{{modelName}}(id int) (err error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
`
|
|
||||||
HPROSE_CTRL_TPL = `package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"{{pkgPath}}/models"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
|
||||||
)
|
|
||||||
|
|
||||||
// oprations for {{ctrlName}}
|
|
||||||
type {{ctrlName}}Controller struct {
|
|
||||||
beego.Controller
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *{{ctrlName}}Controller) URLMapping() {
|
|
||||||
this.Mapping("Post", this.Post)
|
|
||||||
this.Mapping("GetOne", this.GetOne)
|
|
||||||
this.Mapping("GetAll", this.GetAll)
|
|
||||||
this.Mapping("Put", this.Put)
|
|
||||||
this.Mapping("Delete", this.Delete)
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title Post
|
|
||||||
// @Description create {{ctrlName}}
|
|
||||||
// @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content"
|
|
||||||
// @Success 200 {int} models.{{ctrlName}}.Id
|
|
||||||
// @Failure 403 body is empty
|
|
||||||
// @router / [post]
|
|
||||||
func (this *{{ctrlName}}Controller) Post() {
|
|
||||||
var v models.{{ctrlName}}
|
|
||||||
json.Unmarshal(this.Ctx.Input.RequestBody, &v)
|
|
||||||
if id, err := models.Add{{ctrlName}}(&v); err == nil {
|
|
||||||
this.Data["json"] = map[string]int64{"id": id}
|
|
||||||
} else {
|
|
||||||
this.Data["json"] = err.Error()
|
|
||||||
}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title Get
|
|
||||||
// @Description get {{ctrlName}} by id
|
|
||||||
// @Param id path string true "The key for staticblock"
|
|
||||||
// @Success 200 {object} models.{{ctrlName}}
|
|
||||||
// @Failure 403 :id is empty
|
|
||||||
// @router /:id [get]
|
|
||||||
func (this *{{ctrlName}}Controller) GetOne() {
|
|
||||||
idStr := this.Ctx.Input.Params[":id"]
|
|
||||||
id, _ := strconv.Atoi(idStr)
|
|
||||||
v, err := models.Get{{ctrlName}}ById(id)
|
|
||||||
if err != nil {
|
|
||||||
this.Data["json"] = err.Error()
|
|
||||||
} else {
|
|
||||||
this.Data["json"] = v
|
|
||||||
}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title Get All
|
|
||||||
// @Description get {{ctrlName}}
|
|
||||||
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
|
|
||||||
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
|
|
||||||
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
|
|
||||||
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
|
|
||||||
// @Param limit query string false "Limit the size of result set. Must be an integer"
|
|
||||||
// @Param offset query string false "Start position of result set. Must be an integer"
|
|
||||||
// @Success 200 {object} models.{{ctrlName}}
|
|
||||||
// @Failure 403
|
|
||||||
// @router / [get]
|
|
||||||
func (this *{{ctrlName}}Controller) GetAll() {
|
|
||||||
var fields []string
|
|
||||||
var sortby []string
|
|
||||||
var order []string
|
|
||||||
var query map[string]string = make(map[string]string)
|
|
||||||
var limit int64 = 10
|
|
||||||
var offset int64 = 0
|
|
||||||
|
|
||||||
// fields: col1,col2,entity.col3
|
|
||||||
if v := this.GetString("fields"); v != "" {
|
|
||||||
fields = strings.Split(v, ",")
|
|
||||||
}
|
|
||||||
// limit: 10 (default is 10)
|
|
||||||
if v, err := this.GetInt("limit"); err == nil {
|
|
||||||
limit = v
|
|
||||||
}
|
|
||||||
// offset: 0 (default is 0)
|
|
||||||
if v, err := this.GetInt("offset"); err == nil {
|
|
||||||
offset = v
|
|
||||||
}
|
|
||||||
// sortby: col1,col2
|
|
||||||
if v := this.GetString("sortby"); v != "" {
|
|
||||||
sortby = strings.Split(v, ",")
|
|
||||||
}
|
|
||||||
// order: desc,asc
|
|
||||||
if v := this.GetString("order"); v != "" {
|
|
||||||
order = strings.Split(v, ",")
|
|
||||||
}
|
|
||||||
// query: k:v,k:v
|
|
||||||
if v := this.GetString("query"); v != "" {
|
|
||||||
for _, cond := range strings.Split(v, ",") {
|
|
||||||
kv := strings.Split(cond, ":")
|
|
||||||
if len(kv) != 2 {
|
|
||||||
this.Data["json"] = errors.New("Error: invalid query key/value pair")
|
|
||||||
this.ServeJson()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
k, v := kv[0], kv[1]
|
|
||||||
query[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
l, err := models.GetAll{{ctrlName}}(query, fields, sortby, order, offset, limit)
|
|
||||||
if err != nil {
|
|
||||||
this.Data["json"] = err.Error()
|
|
||||||
} else {
|
|
||||||
this.Data["json"] = l
|
|
||||||
}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title Update
|
|
||||||
// @Description update the {{ctrlName}}
|
|
||||||
// @Param id path string true "The id you want to update"
|
|
||||||
// @Param body body models.{{ctrlName}} true "body for {{ctrlName}} content"
|
|
||||||
// @Success 200 {object} models.{{ctrlName}}
|
|
||||||
// @Failure 403 :id is not int
|
|
||||||
// @router /:id [put]
|
|
||||||
func (this *{{ctrlName}}Controller) Put() {
|
|
||||||
idStr := this.Ctx.Input.Params[":id"]
|
|
||||||
id, _ := strconv.Atoi(idStr)
|
|
||||||
v := models.{{ctrlName}}{Id: id}
|
|
||||||
json.Unmarshal(this.Ctx.Input.RequestBody, &v)
|
|
||||||
if err := models.Update{{ctrlName}}ById(&v); err == nil {
|
|
||||||
this.Data["json"] = "OK"
|
|
||||||
} else {
|
|
||||||
this.Data["json"] = err.Error()
|
|
||||||
}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title Delete
|
|
||||||
// @Description delete the {{ctrlName}}
|
|
||||||
// @Param id path string true "The id you want to delete"
|
|
||||||
// @Success 200 {string} delete success!
|
|
||||||
// @Failure 403 id is empty
|
|
||||||
// @router /:id [delete]
|
|
||||||
func (this *{{ctrlName}}Controller) Delete() {
|
|
||||||
idStr := this.Ctx.Input.Params[":id"]
|
|
||||||
id, _ := strconv.Atoi(idStr)
|
|
||||||
if err := models.Delete{{ctrlName}}(id); err == nil {
|
|
||||||
this.Data["json"] = "OK"
|
|
||||||
} else {
|
|
||||||
this.Data["json"] = err.Error()
|
|
||||||
}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
`
|
|
||||||
HPROSE_ROUTER_TPL = `// @APIVersion 1.0.0
|
|
||||||
// @Title beego Test API
|
|
||||||
// @Description beego has a very cool tools to autogenerate documents for your API
|
|
||||||
// @Contact astaxie@gmail.com
|
|
||||||
// @TermsOfServiceUrl http://beego.me/
|
|
||||||
// @License Apache 2.0
|
|
||||||
// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
|
|
||||||
package routers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"{{pkgPath}}/controllers"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
ns := beego.NewNamespace("/v1",
|
|
||||||
{{nameSpaces}}
|
|
||||||
)
|
|
||||||
beego.AddNamespace(ns)
|
|
||||||
}
|
|
||||||
`
|
|
||||||
HPROSE_NAMESPACE_TPL = `
|
|
||||||
beego.NSNamespace("/{{nameSpace}}",
|
|
||||||
beego.NSInclude(
|
|
||||||
&controllers.{{ctrlName}}Controller{},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
339
hproseapp.go
339
hproseapp.go
@ -48,13 +48,6 @@ In the appname folder has the follow struct:
|
|||||||
|
|
||||||
├── conf
|
├── conf
|
||||||
│ └── app.conf
|
│ └── app.conf
|
||||||
├── controllers
|
|
||||||
│ └── object.go
|
|
||||||
│ └── user.go
|
|
||||||
├── routers
|
|
||||||
│ └── router.go
|
|
||||||
├── tests
|
|
||||||
│ └── default_test.go
|
|
||||||
├── main.go
|
├── main.go
|
||||||
└── models
|
└── models
|
||||||
└── object.go
|
└── object.go
|
||||||
@ -74,23 +67,17 @@ EnableDocs = true
|
|||||||
var hproseMaingo = `package main
|
var hproseMaingo = `package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "{{.Appname}}/docs"
|
|
||||||
"{{.Appname}}/models"
|
"{{.Appname}}/models"
|
||||||
_ "{{.Appname}}/routers"
|
|
||||||
"github.com/hprose/hprose-go/hprose"
|
"github.com/hprose/hprose-go/hprose"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if beego.RunMode == "dev" {
|
|
||||||
beego.DirectoryIndex = true
|
|
||||||
beego.StaticDir["/swagger"] = "swagger"
|
|
||||||
}
|
|
||||||
service := hprose.NewHttpService()
|
service := hprose.NewHttpService()
|
||||||
service.AddFunction("AddOne", models.AddOne)
|
service.AddFunction("AddOne", models.AddOne)
|
||||||
service.AddFunction("GetOne", models.GetOne)
|
service.AddFunction("GetOne", models.GetOne)
|
||||||
beego.Handler("/hprose", service)
|
beego.Handler("/", service)
|
||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
@ -98,9 +85,7 @@ func main() {
|
|||||||
var hproseMainconngo = `package main
|
var hproseMainconngo = `package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "{{.Appname}}/docs"
|
|
||||||
"{{.Appname}}/models"
|
"{{.Appname}}/models"
|
||||||
_ "{{.Appname}}/routers"
|
|
||||||
"github.com/hprose/hprose-go/hprose"
|
"github.com/hprose/hprose-go/hprose"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
@ -113,50 +98,14 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if beego.RunMode == "dev" {
|
|
||||||
beego.DirectoryIndex = true
|
|
||||||
beego.StaticDir["/swagger"] = "swagger"
|
|
||||||
}
|
|
||||||
service := hprose.NewHttpService()
|
service := hprose.NewHttpService()
|
||||||
{{HproseFunctionList}}
|
{{HproseFunctionList}}
|
||||||
beego.Handler("/hprose", service)
|
beego.Handler("/", service)
|
||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
var hproserouter = `// @APIVersion 1.0.0
|
|
||||||
// @Title beego Test API
|
|
||||||
// @Description beego has a very cool tools to autogenerate documents for your API
|
|
||||||
// @Contact astaxie@gmail.com
|
|
||||||
// @TermsOfServiceUrl http://beego.me/
|
|
||||||
// @License Apache 2.0
|
|
||||||
// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
|
|
||||||
package routers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"{{.Appname}}/controllers"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
ns := beego.NewNamespace("/v1",
|
|
||||||
beego.NSNamespace("/object",
|
|
||||||
beego.NSInclude(
|
|
||||||
&controllers.ObjectController{},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
beego.NSNamespace("/user",
|
|
||||||
beego.NSInclude(
|
|
||||||
&controllers.UserController{},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
beego.AddNamespace(ns)
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
var hproseModels = `package models
|
var hproseModels = `package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -300,260 +249,6 @@ func DeleteUser(uid string) {
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
var hproseControllers = `package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"{{.Appname}}/models"
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Operations about object
|
|
||||||
type ObjectController struct {
|
|
||||||
beego.Controller
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title create
|
|
||||||
// @Description create object
|
|
||||||
// @Param body body models.Object true "The object content"
|
|
||||||
// @Success 200 {string} models.Object.Id
|
|
||||||
// @Failure 403 body is empty
|
|
||||||
// @router / [post]
|
|
||||||
func (this *ObjectController) 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()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title Get
|
|
||||||
// @Description find object by objectid
|
|
||||||
// @Param objectId path string true "the objectid you want to get"
|
|
||||||
// @Success 200 {object} models.Object
|
|
||||||
// @Failure 403 :objectId is empty
|
|
||||||
// @router /:objectId [get]
|
|
||||||
func (this *ObjectController) Get() {
|
|
||||||
objectId := this.Ctx.Input.Params[":objectId"]
|
|
||||||
if objectId != "" {
|
|
||||||
ob, err := models.GetOne(objectId)
|
|
||||||
if err != nil {
|
|
||||||
this.Data["json"] = err
|
|
||||||
} else {
|
|
||||||
this.Data["json"] = ob
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title GetAll
|
|
||||||
// @Description get all objects
|
|
||||||
// @Success 200 {object} models.Object
|
|
||||||
// @Failure 403 :objectId is empty
|
|
||||||
// @router / [get]
|
|
||||||
func (this *ObjectController) GetAll() {
|
|
||||||
obs := models.GetAll()
|
|
||||||
this.Data["json"] = obs
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title update
|
|
||||||
// @Description update the object
|
|
||||||
// @Param objectId path string true "The objectid you want to update"
|
|
||||||
// @Param body body models.Object true "The body"
|
|
||||||
// @Success 200 {object} models.Object
|
|
||||||
// @Failure 403 :objectId is empty
|
|
||||||
// @router /:objectId [put]
|
|
||||||
func (this *ObjectController) Put() {
|
|
||||||
objectId := this.Ctx.Input.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()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title delete
|
|
||||||
// @Description delete the object
|
|
||||||
// @Param objectId path string true "The objectId you want to delete"
|
|
||||||
// @Success 200 {string} delete success!
|
|
||||||
// @Failure 403 objectId is empty
|
|
||||||
// @router /:objectId [delete]
|
|
||||||
func (this *ObjectController) Delete() {
|
|
||||||
objectId := this.Ctx.Input.Params[":objectId"]
|
|
||||||
models.Delete(objectId)
|
|
||||||
this.Data["json"] = "delete success!"
|
|
||||||
this.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
`
|
|
||||||
var hproseControllers2 = `package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"{{.Appname}}/models"
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Operations about Users
|
|
||||||
type UserController struct {
|
|
||||||
beego.Controller
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title createUser
|
|
||||||
// @Description create users
|
|
||||||
// @Param body body models.User true "body for user content"
|
|
||||||
// @Success 200 {int} models.User.Id
|
|
||||||
// @Failure 403 body is empty
|
|
||||||
// @router / [post]
|
|
||||||
func (u *UserController) Post() {
|
|
||||||
var user models.User
|
|
||||||
json.Unmarshal(u.Ctx.Input.RequestBody, &user)
|
|
||||||
uid := models.AddUser(user)
|
|
||||||
u.Data["json"] = map[string]string{"uid": uid}
|
|
||||||
u.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title Get
|
|
||||||
// @Description get all Users
|
|
||||||
// @Success 200 {object} models.User
|
|
||||||
// @router / [get]
|
|
||||||
func (u *UserController) GetAll() {
|
|
||||||
users := models.GetAllUsers()
|
|
||||||
u.Data["json"] = users
|
|
||||||
u.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title Get
|
|
||||||
// @Description get user by uid
|
|
||||||
// @Param uid path string true "The key for staticblock"
|
|
||||||
// @Success 200 {object} models.User
|
|
||||||
// @Failure 403 :uid is empty
|
|
||||||
// @router /:uid [get]
|
|
||||||
func (u *UserController) Get() {
|
|
||||||
uid := u.GetString(":uid")
|
|
||||||
if uid != "" {
|
|
||||||
user, err := models.GetUser(uid)
|
|
||||||
if err != nil {
|
|
||||||
u.Data["json"] = err
|
|
||||||
} else {
|
|
||||||
u.Data["json"] = user
|
|
||||||
}
|
|
||||||
}
|
|
||||||
u.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title update
|
|
||||||
// @Description update the user
|
|
||||||
// @Param uid path string true "The uid you want to update"
|
|
||||||
// @Param body body models.User true "body for user content"
|
|
||||||
// @Success 200 {object} models.User
|
|
||||||
// @Failure 403 :uid is not int
|
|
||||||
// @router /:uid [put]
|
|
||||||
func (u *UserController) Put() {
|
|
||||||
uid := u.GetString(":uid")
|
|
||||||
if uid != "" {
|
|
||||||
var user models.User
|
|
||||||
json.Unmarshal(u.Ctx.Input.RequestBody, &user)
|
|
||||||
uu, err := models.UpdateUser(uid, &user)
|
|
||||||
if err != nil {
|
|
||||||
u.Data["json"] = err
|
|
||||||
} else {
|
|
||||||
u.Data["json"] = uu
|
|
||||||
}
|
|
||||||
}
|
|
||||||
u.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title delete
|
|
||||||
// @Description delete the user
|
|
||||||
// @Param uid path string true "The uid you want to delete"
|
|
||||||
// @Success 200 {string} delete success!
|
|
||||||
// @Failure 403 uid is empty
|
|
||||||
// @router /:uid [delete]
|
|
||||||
func (u *UserController) Delete() {
|
|
||||||
uid := u.GetString(":uid")
|
|
||||||
models.DeleteUser(uid)
|
|
||||||
u.Data["json"] = "delete success!"
|
|
||||||
u.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title login
|
|
||||||
// @Description Logs user into the system
|
|
||||||
// @Param username query string true "The username for login"
|
|
||||||
// @Param password query string true "The password for login"
|
|
||||||
// @Success 200 {string} lonin success
|
|
||||||
// @Failure 403 user not exist
|
|
||||||
// @router /login [get]
|
|
||||||
func (u *UserController) Login() {
|
|
||||||
username := u.GetString("username")
|
|
||||||
password := u.GetString("password")
|
|
||||||
if models.Login(username, password) {
|
|
||||||
u.Data["json"] = "login success"
|
|
||||||
} else {
|
|
||||||
u.Data["json"] = "user not exist"
|
|
||||||
}
|
|
||||||
u.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Title logout
|
|
||||||
// @Description Logs out current logged in user session
|
|
||||||
// @Success 200 {string} logout success
|
|
||||||
// @router /logout [get]
|
|
||||||
func (u *UserController) Logout() {
|
|
||||||
u.Data["json"] = "logout success"
|
|
||||||
u.ServeJson()
|
|
||||||
}
|
|
||||||
|
|
||||||
`
|
|
||||||
|
|
||||||
var hproseTests = `package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
"runtime"
|
|
||||||
"path/filepath"
|
|
||||||
_ "{{.Appname}}/routers"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
_, file, _, _ := runtime.Caller(1)
|
|
||||||
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
|
|
||||||
beego.TestBeegoInit(apppath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestGet is a sample to run an endpoint test
|
|
||||||
func TestGet(t *testing.T) {
|
|
||||||
r, _ := http.NewRequest("GET", "/v1/object", nil)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
beego.BeeApp.Handlers.ServeHTTP(w, r)
|
|
||||||
|
|
||||||
beego.Trace("testing", "TestGet", "Code[%d]\n%s", w.Code, w.Body.String())
|
|
||||||
|
|
||||||
Convey("Subject: Test Station Endpoint\n", t, func() {
|
|
||||||
Convey("Status Code Should Be 200", func() {
|
|
||||||
So(w.Code, ShouldEqual, 200)
|
|
||||||
})
|
|
||||||
Convey("The Result Should Not Be Empty", func() {
|
|
||||||
So(w.Body.Len(), ShouldBeGreaterThan, 0)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
`
|
|
||||||
|
|
||||||
var hproseAddFunctions = []string{}
|
var hproseAddFunctions = []string{}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -583,13 +278,6 @@ func createhprose(cmd *Command, args []string) int {
|
|||||||
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)
|
|
||||||
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, "tests"), 0755)
|
|
||||||
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(hproseconf, "{{.Appname}}", args[0], -1))
|
strings.Replace(hproseconf, "{{.Appname}}", args[0], -1))
|
||||||
@ -598,7 +286,7 @@ func createhprose(cmd *Command, args []string) int {
|
|||||||
ColorLog("[INFO] Using '%s' as 'driver'\n", driver)
|
ColorLog("[INFO] Using '%s' as 'driver'\n", driver)
|
||||||
ColorLog("[INFO] Using '%s' as 'conn'\n", conn)
|
ColorLog("[INFO] Using '%s' as 'conn'\n", conn)
|
||||||
ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
|
ColorLog("[INFO] Using '%s' as 'tables'\n", tables)
|
||||||
generateHproseAppcode(string(driver), string(conn), "3", string(tables), path.Join(curpath, args[0]))
|
generateHproseAppcode(string(driver), string(conn), "1", string(tables), path.Join(curpath, args[0]))
|
||||||
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
|
fmt.Println("create main.go:", path.Join(apppath, "main.go"))
|
||||||
maingoContent := strings.Replace(hproseMainconngo, "{{.Appname}}", packpath, -1)
|
maingoContent := strings.Replace(hproseMainconngo, "{{.Appname}}", packpath, -1)
|
||||||
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
|
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
|
||||||
@ -619,24 +307,6 @@ func createhprose(cmd *Command, args []string) int {
|
|||||||
} else {
|
} else {
|
||||||
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, "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(hproseControllers, "{{.Appname}}", packpath, -1))
|
|
||||||
|
|
||||||
fmt.Println("create controllers user.go:", path.Join(apppath, "controllers", "user.go"))
|
|
||||||
writetofile(path.Join(apppath, "controllers", "user.go"),
|
|
||||||
strings.Replace(hproseControllers2, "{{.Appname}}", packpath, -1))
|
|
||||||
|
|
||||||
fmt.Println("create tests default.go:", path.Join(apppath, "tests", "default_test.go"))
|
|
||||||
writetofile(path.Join(apppath, "tests", "default_test.go"),
|
|
||||||
strings.Replace(hproseTests, "{{.Appname}}", packpath, -1))
|
|
||||||
|
|
||||||
fmt.Println("create routers router.go:", path.Join(apppath, "routers", "router.go"))
|
|
||||||
writetofile(path.Join(apppath, "routers", "router.go"),
|
|
||||||
strings.Replace(hproserouter, "{{.Appname}}", packpath, -1))
|
|
||||||
|
|
||||||
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)
|
||||||
@ -644,9 +314,6 @@ func createhprose(cmd *Command, args []string) int {
|
|||||||
fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go"))
|
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"), apiModels2)
|
||||||
|
|
||||||
fmt.Println("create docs doc.go:", path.Join(apppath, "docs", "doc.go"))
|
|
||||||
writetofile(path.Join(apppath, "docs", "doc.go"), "package docs")
|
|
||||||
|
|
||||||
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(hproseMaingo, "{{.Appname}}", packpath, -1))
|
strings.Replace(hproseMaingo, "{{.Appname}}", packpath, -1))
|
||||||
|
Loading…
Reference in New Issue
Block a user