2016-12-03 10:54:41 +00:00
|
|
|
// Copyright 2013 bee authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"): you may
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
// a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
// under the License.
|
2014-10-14 13:30:32 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
path "path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdHproseapp = &Command{
|
|
|
|
// CustomFlags: true,
|
|
|
|
UsageLine: "hprose [appname]",
|
2016-12-03 10:54:41 +00:00
|
|
|
Short: "Creates an RPC application based on Hprose and Beego frameworks",
|
2016-07-31 21:30:35 +00:00
|
|
|
Long: `
|
2016-12-03 10:54:41 +00:00
|
|
|
The command 'hprose' creates an RPC application based on both Beego and Hprose (http://hprose.com/).
|
2014-10-14 13:30:32 +00:00
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
{{"To scaffold out your application, use:"|bold}}
|
2016-07-31 21:30:35 +00:00
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
$ bee hprose [appname] [-tables=""] [-driver=mysql] [-conn=root:@tcp(127.0.0.1:3306)/test]
|
2014-10-14 13:30:32 +00:00
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
If 'conn' is empty, the command will generate a sample application. Otherwise the command
|
|
|
|
will connect to your database and generate models based on the existing tables.
|
2014-10-14 13:30:32 +00:00
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
The command 'hprose' creates a folder named [appname] with the following structure:
|
2014-10-14 13:30:32 +00:00
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
├── main.go
|
2016-12-03 15:06:46 +00:00
|
|
|
├── {{"conf"|foldername}}
|
|
|
|
│ └── app.conf
|
|
|
|
└── {{"models"|foldername}}
|
2016-12-03 10:54:41 +00:00
|
|
|
└── object.go
|
|
|
|
└── user.go
|
2014-10-14 13:30:32 +00:00
|
|
|
`,
|
2016-12-03 10:54:41 +00:00
|
|
|
PreRun: func(cmd *Command, args []string) { ShowShortVersionBanner() },
|
|
|
|
Run: createhprose,
|
2014-10-14 13:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var hproseconf = `appname = {{.Appname}}
|
|
|
|
httpport = 8080
|
|
|
|
runmode = dev
|
|
|
|
autorender = false
|
|
|
|
copyrequestbody = true
|
|
|
|
EnableDocs = true
|
|
|
|
`
|
|
|
|
var hproseMaingo = `package main
|
|
|
|
|
|
|
|
import (
|
2016-11-01 10:59:32 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2016-11-13 14:14:48 +00:00
|
|
|
|
2014-10-14 13:30:32 +00:00
|
|
|
"{{.Appname}}/models"
|
2016-11-01 10:59:32 +00:00
|
|
|
"github.com/hprose/hprose-golang/rpc"
|
2014-10-14 13:30:32 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
)
|
|
|
|
|
2016-11-01 10:59:32 +00:00
|
|
|
func logInvokeHandler(
|
|
|
|
name string,
|
|
|
|
args []reflect.Value,
|
|
|
|
context rpc.Context,
|
|
|
|
next rpc.NextInvokeHandler) (results []reflect.Value, err error) {
|
|
|
|
fmt.Printf("%s(%v) = ", name, args)
|
|
|
|
results, err = next(name, args, context)
|
|
|
|
fmt.Printf("%v %v\r\n", results, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:30:32 +00:00
|
|
|
func main() {
|
2016-11-01 10:59:32 +00:00
|
|
|
// Create WebSocketServer
|
|
|
|
// service := rpc.NewWebSocketService()
|
|
|
|
|
|
|
|
// Create Http Server
|
|
|
|
service := rpc.NewHTTPService()
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// Use Logger Middleware
|
2016-11-01 10:59:32 +00:00
|
|
|
service.AddInvokeHandler(logInvokeHandler)
|
|
|
|
|
|
|
|
// Publish Functions
|
2014-10-14 13:30:32 +00:00
|
|
|
service.AddFunction("AddOne", models.AddOne)
|
|
|
|
service.AddFunction("GetOne", models.GetOne)
|
2016-11-01 10:59:32 +00:00
|
|
|
|
|
|
|
// Start Service
|
2014-10-16 13:10:22 +00:00
|
|
|
beego.Handler("/", service)
|
2014-10-14 13:30:32 +00:00
|
|
|
beego.Run()
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var hproseMainconngo = `package main
|
|
|
|
|
|
|
|
import (
|
2016-11-01 10:59:32 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2014-10-14 13:30:32 +00:00
|
|
|
"{{.Appname}}/models"
|
2016-11-01 10:59:32 +00:00
|
|
|
"github.com/hprose/hprose-golang/rpc"
|
2014-10-14 13:30:32 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
{{.DriverPkg}}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
orm.RegisterDataBase("default", "{{.DriverName}}", "{{.conn}}")
|
|
|
|
}
|
|
|
|
|
2016-11-01 10:59:32 +00:00
|
|
|
func logInvokeHandler(
|
|
|
|
name string,
|
|
|
|
args []reflect.Value,
|
|
|
|
context rpc.Context,
|
|
|
|
next rpc.NextInvokeHandler) (results []reflect.Value, err error) {
|
|
|
|
fmt.Printf("%s(%v) = ", name, args)
|
|
|
|
results, err = next(name, args, context)
|
|
|
|
fmt.Printf("%v %v\r\n", results, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-14 13:30:32 +00:00
|
|
|
func main() {
|
2016-11-01 10:59:32 +00:00
|
|
|
// Create WebSocketServer
|
|
|
|
// service := rpc.NewWebSocketService()
|
|
|
|
|
|
|
|
// Create Http Server
|
|
|
|
service := rpc.NewHTTPService()
|
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
// Use Logger Middleware
|
2016-11-01 10:59:32 +00:00
|
|
|
service.AddInvokeHandler(logInvokeHandler)
|
|
|
|
|
2014-10-14 13:30:32 +00:00
|
|
|
{{HproseFunctionList}}
|
2016-11-01 10:59:32 +00:00
|
|
|
|
|
|
|
// Start Service
|
2014-10-16 13:10:22 +00:00
|
|
|
beego.Handler("/", service)
|
2014-10-14 13:30:32 +00:00
|
|
|
beego.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var hproseModels = `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 hproseModels2 = `package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
UserList map[string]*User
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
UserList = make(map[string]*User)
|
|
|
|
u := User{"user_11111", "astaxie", "11111", 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 {
|
|
|
|
u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
|
|
UserList[u.Id] = &u
|
|
|
|
return u.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUser(uid string) (u *User, err error) {
|
|
|
|
if u, ok := UserList[uid]; ok {
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("User not exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllUsers() map[string]*User {
|
|
|
|
return UserList
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateUser(uid string, uu *User) (a *User, err error) {
|
|
|
|
if u, ok := UserList[uid]; ok {
|
|
|
|
if uu.Username != "" {
|
|
|
|
u.Username = uu.Username
|
|
|
|
}
|
|
|
|
if uu.Password != "" {
|
|
|
|
u.Password = uu.Password
|
|
|
|
}
|
|
|
|
if uu.Profile.Age != 0 {
|
|
|
|
u.Profile.Age = uu.Profile.Age
|
|
|
|
}
|
|
|
|
if uu.Profile.Address != "" {
|
|
|
|
u.Profile.Address = uu.Profile.Address
|
|
|
|
}
|
|
|
|
if uu.Profile.Gender != "" {
|
|
|
|
u.Profile.Gender = uu.Profile.Gender
|
|
|
|
}
|
|
|
|
if uu.Profile.Email != "" {
|
|
|
|
u.Profile.Email = uu.Profile.Email
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("User Not Exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Login(username, password string) bool {
|
|
|
|
for _, u := range UserList {
|
|
|
|
if u.Username == username && u.Password == password {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUser(uid string) {
|
|
|
|
delete(UserList, uid)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
var hproseAddFunctions = []string{}
|
|
|
|
|
|
|
|
func init() {
|
2016-12-03 10:54:41 +00:00
|
|
|
cmdHproseapp.Flag.Var(&tables, "tables", "List of table names separated by a comma.")
|
|
|
|
cmdHproseapp.Flag.Var(&driver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
|
|
|
cmdHproseapp.Flag.Var(&conn, "conn", "Connection string used by the driver to connect to a database instance.")
|
2014-10-14 13:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createhprose(cmd *Command, args []string) int {
|
2016-12-03 10:54:41 +00:00
|
|
|
output := cmd.Out()
|
2016-07-31 21:30:35 +00:00
|
|
|
|
2016-12-27 08:44:15 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
logger.Fatal("Argument [appname] is missing")
|
2016-12-27 01:01:08 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 13:30:32 +00:00
|
|
|
curpath, _ := os.Getwd()
|
|
|
|
if len(args) > 1 {
|
|
|
|
cmd.Flag.Parse(args[1:])
|
|
|
|
}
|
|
|
|
apppath, packpath, err := checkEnv(args[0])
|
|
|
|
if err != nil {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Fatalf("%s", err)
|
2014-10-14 13:30:32 +00:00
|
|
|
}
|
|
|
|
if driver == "" {
|
|
|
|
driver = "mysql"
|
|
|
|
}
|
|
|
|
if conn == "" {
|
|
|
|
}
|
2016-06-01 12:30:29 +00:00
|
|
|
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Info("Creating Hprose application...")
|
2016-06-01 12:30:29 +00:00
|
|
|
|
2014-10-14 13:30:32 +00:00
|
|
|
os.MkdirAll(apppath, 0755)
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m")
|
2014-10-14 13:30:32 +00:00
|
|
|
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m")
|
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf", "app.conf"), "\x1b[0m")
|
2016-05-26 16:17:24 +00:00
|
|
|
WriteToFile(path.Join(apppath, "conf", "app.conf"),
|
2014-10-14 13:30:32 +00:00
|
|
|
strings.Replace(hproseconf, "{{.Appname}}", args[0], -1))
|
|
|
|
|
|
|
|
if conn != "" {
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Infof("Using '%s' as 'driver'", driver)
|
|
|
|
logger.Infof("Using '%s' as 'conn'", conn)
|
|
|
|
logger.Infof("Using '%s' as 'tables'", tables)
|
2014-10-16 13:10:22 +00:00
|
|
|
generateHproseAppcode(string(driver), string(conn), "1", string(tables), path.Join(curpath, args[0]))
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
2014-10-14 13:30:32 +00:00
|
|
|
maingoContent := strings.Replace(hproseMainconngo, "{{.Appname}}", packpath, -1)
|
|
|
|
maingoContent = strings.Replace(maingoContent, "{{.DriverName}}", string(driver), -1)
|
|
|
|
maingoContent = strings.Replace(maingoContent, "{{HproseFunctionList}}", strings.Join(hproseAddFunctions, ""), -1)
|
|
|
|
if driver == "mysql" {
|
|
|
|
maingoContent = strings.Replace(maingoContent, "{{.DriverPkg}}", `_ "github.com/go-sql-driver/mysql"`, -1)
|
|
|
|
} else if driver == "postgres" {
|
|
|
|
maingoContent = strings.Replace(maingoContent, "{{.DriverPkg}}", `_ "github.com/lib/pq"`, -1)
|
|
|
|
}
|
2016-05-26 16:17:24 +00:00
|
|
|
WriteToFile(path.Join(apppath, "main.go"),
|
2014-10-14 13:30:32 +00:00
|
|
|
strings.Replace(
|
|
|
|
maingoContent,
|
|
|
|
"{{.conn}}",
|
|
|
|
conn.String(),
|
|
|
|
-1,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
os.Mkdir(path.Join(apppath, "models"), 0755)
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models"), "\x1b[0m")
|
2014-10-14 13:30:32 +00:00
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "object.go"), "\x1b[0m")
|
2016-05-26 16:17:24 +00:00
|
|
|
WriteToFile(path.Join(apppath, "models", "object.go"), apiModels)
|
2014-10-14 13:30:32 +00:00
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "models", "user.go"), "\x1b[0m")
|
2016-05-26 16:17:24 +00:00
|
|
|
WriteToFile(path.Join(apppath, "models", "user.go"), apiModels2)
|
2014-10-14 13:30:32 +00:00
|
|
|
|
2016-12-03 10:54:41 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "main.go"), "\x1b[0m")
|
2016-05-26 16:17:24 +00:00
|
|
|
WriteToFile(path.Join(apppath, "main.go"),
|
2014-10-14 13:30:32 +00:00
|
|
|
strings.Replace(hproseMaingo, "{{.Appname}}", packpath, -1))
|
|
|
|
}
|
2016-11-13 14:14:48 +00:00
|
|
|
logger.Success("New Hprose application successfully created!")
|
2014-10-14 13:30:32 +00:00
|
|
|
return 0
|
|
|
|
}
|