2013-10-30 23:54:53 +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.
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
package apiapp
|
2013-09-11 10:18:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
path "path/filepath"
|
|
|
|
"strings"
|
2017-03-06 23:58:53 +00:00
|
|
|
|
2020-12-16 05:20:41 +00:00
|
|
|
"github.com/beego/bee/v2/logger/colors"
|
2020-10-09 15:33:58 +00:00
|
|
|
|
2020-12-16 05:20:41 +00:00
|
|
|
"github.com/beego/bee/v2/cmd/commands"
|
|
|
|
"github.com/beego/bee/v2/cmd/commands/version"
|
|
|
|
"github.com/beego/bee/v2/generate"
|
|
|
|
beeLogger "github.com/beego/bee/v2/logger"
|
|
|
|
"github.com/beego/bee/v2/utils"
|
2013-09-11 10:18:02 +00:00
|
|
|
)
|
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
var CmdApiapp = &commands.Command{
|
2013-09-11 10:18:02 +00:00
|
|
|
// CustomFlags: true,
|
|
|
|
UsageLine: "api [appname]",
|
2016-12-03 10:54:41 +00:00
|
|
|
Short: "Creates a Beego API application",
|
2014-12-18 16:19:34 +00:00
|
|
|
Long: `
|
2016-12-03 10:54:41 +00:00
|
|
|
The command 'api' creates a Beego API application.
|
2024-03-28 21:25:47 +00:00
|
|
|
now default support generate a go modules project.
|
2016-12-03 10:54:41 +00:00
|
|
|
|
|
|
|
{{"Example:"|bold}}
|
2020-11-25 13:01:30 +00:00
|
|
|
$ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-gopath=false] [-beego=v1.12.3]
|
2016-12-03 10:54:41 +00:00
|
|
|
|
|
|
|
If 'conn' argument is empty, the command will generate an example API application. Otherwise the command
|
|
|
|
will connect to your database and generate models based on the existing tables.
|
|
|
|
|
|
|
|
The command 'api' creates a folder named [appname] with the following structure:
|
|
|
|
|
|
|
|
├── main.go
|
2020-07-24 07:54:52 +00:00
|
|
|
├── go.mod
|
2016-12-03 15:06:46 +00:00
|
|
|
├── {{"conf"|foldername}}
|
|
|
|
│ └── app.conf
|
|
|
|
├── {{"controllers"|foldername}}
|
|
|
|
│ └── object.go
|
|
|
|
│ └── user.go
|
|
|
|
├── {{"routers"|foldername}}
|
|
|
|
│ └── router.go
|
|
|
|
├── {{"tests"|foldername}}
|
|
|
|
│ └── default_test.go
|
|
|
|
└── {{"models"|foldername}}
|
|
|
|
└── object.go
|
|
|
|
└── user.go
|
2013-09-11 10:18:02 +00:00
|
|
|
`,
|
2017-03-06 23:58:53 +00:00
|
|
|
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
|
2017-03-11 08:57:06 +00:00
|
|
|
Run: createAPI,
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
2014-06-24 06:32:17 +00:00
|
|
|
var apiconf = `appname = {{.Appname}}
|
2013-09-11 10:18:02 +00:00
|
|
|
httpport = 8080
|
|
|
|
runmode = dev
|
|
|
|
autorender = false
|
|
|
|
copyrequestbody = true
|
2014-06-24 06:32:17 +00:00
|
|
|
EnableDocs = true
|
2018-09-13 08:09:45 +00:00
|
|
|
sqlconn = {{.SQLConnStr}}
|
2013-09-11 10:18:02 +00:00
|
|
|
`
|
|
|
|
var apiMaingo = `package main
|
|
|
|
|
|
|
|
import (
|
2013-12-23 16:00:52 +00:00
|
|
|
_ "{{.Appname}}/routers"
|
2013-09-11 10:18:02 +00:00
|
|
|
|
2020-12-14 05:08:47 +00:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
2014-06-24 06:32:17 +00:00
|
|
|
)
|
2013-09-11 10:18:02 +00:00
|
|
|
|
|
|
|
func main() {
|
2016-01-06 03:55:56 +00:00
|
|
|
if beego.BConfig.RunMode == "dev" {
|
|
|
|
beego.BConfig.WebConfig.DirectoryIndex = true
|
|
|
|
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
2013-09-11 10:18:02 +00:00
|
|
|
beego.Run()
|
|
|
|
}
|
|
|
|
`
|
2014-08-08 16:55:55 +00:00
|
|
|
|
|
|
|
var apiMainconngo = `package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "{{.Appname}}/routers"
|
|
|
|
|
2020-12-14 05:08:47 +00:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
2023-03-19 22:27:34 +00:00
|
|
|
beeLogger "github.com/beego/bee/v2/logger"
|
2020-12-14 05:08:47 +00:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2014-08-22 07:46:36 +00:00
|
|
|
{{.DriverPkg}}
|
2014-08-08 16:55:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-08-12 09:57:59 +00:00
|
|
|
sqlConn,err := beego.AppConfig.String("sqlconn")
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatal("%s", err)
|
|
|
|
}
|
|
|
|
orm.RegisterDataBase("default", "{{.DriverName}}", sqlConn)
|
2016-01-06 03:55:56 +00:00
|
|
|
if beego.BConfig.RunMode == "dev" {
|
|
|
|
beego.BConfig.WebConfig.DirectoryIndex = true
|
|
|
|
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
|
2014-08-08 16:55:55 +00:00
|
|
|
}
|
|
|
|
beego.Run()
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:29:27 +00:00
|
|
|
`
|
|
|
|
var goMod = `
|
|
|
|
module %s
|
|
|
|
|
|
|
|
go %s
|
|
|
|
|
2020-12-14 05:08:47 +00:00
|
|
|
require github.com/beego/beego/v2 %s
|
2020-06-25 14:29:27 +00:00
|
|
|
require github.com/smartystreets/goconvey v1.6.4
|
2014-08-08 16:55:55 +00:00
|
|
|
`
|
|
|
|
|
2014-06-24 06:32:17 +00:00
|
|
|
var apirouter = `// @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
|
2013-12-23 15:28:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"{{.Appname}}/controllers"
|
2014-06-24 06:32:17 +00:00
|
|
|
|
2020-12-14 05:08:47 +00:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
2013-12-23 15:28:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-06-24 06:32:17 +00:00
|
|
|
ns := beego.NewNamespace("/v1",
|
|
|
|
beego.NSNamespace("/object",
|
|
|
|
beego.NSInclude(
|
|
|
|
&controllers.ObjectController{},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
beego.NSNamespace("/user",
|
|
|
|
beego.NSInclude(
|
|
|
|
&controllers.UserController{},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
beego.AddNamespace(ns)
|
2013-12-23 15:28:31 +00:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
var APIModels = `package models
|
2013-09-11 10:18:02 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2014-06-24 06:32:17 +00:00
|
|
|
|
|
|
|
`
|
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
var APIModels2 = `package models
|
2014-06-24 06:32:17 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2013-09-11 10:18:02 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
var apiControllers = `package controllers
|
|
|
|
|
|
|
|
import (
|
2014-06-24 06:32:17 +00:00
|
|
|
"{{.Appname}}/models"
|
2013-09-11 10:18:02 +00:00
|
|
|
"encoding/json"
|
2014-06-24 06:32:17 +00:00
|
|
|
|
2020-12-14 05:08:47 +00:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
2013-09-11 10:18:02 +00:00
|
|
|
)
|
|
|
|
|
2014-06-24 06:32:17 +00:00
|
|
|
// Operations about object
|
2013-09-13 07:32:58 +00:00
|
|
|
type ObjectController struct {
|
2013-09-11 10:18:02 +00:00
|
|
|
beego.Controller
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title Create
|
2014-06-24 06:32:17 +00:00
|
|
|
// @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]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (o *ObjectController) Post() {
|
2013-09-11 10:18:02 +00:00
|
|
|
var ob models.Object
|
2014-11-05 14:48:09 +00:00
|
|
|
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
|
2013-09-11 10:18:02 +00:00
|
|
|
objectid := models.AddOne(ob)
|
2014-11-05 14:48:09 +00:00
|
|
|
o.Data["json"] = map[string]string{"ObjectId": objectid}
|
2015-10-22 03:32:21 +00:00
|
|
|
o.ServeJSON()
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 06:32:17 +00:00
|
|
|
// @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]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (o *ObjectController) Get() {
|
2016-01-06 03:55:56 +00:00
|
|
|
objectId := o.Ctx.Input.Param(":objectId")
|
2013-09-11 10:18:02 +00:00
|
|
|
if objectId != "" {
|
|
|
|
ob, err := models.GetOne(objectId)
|
|
|
|
if err != nil {
|
2015-09-27 23:42:11 +00:00
|
|
|
o.Data["json"] = err.Error()
|
2013-09-11 10:18:02 +00:00
|
|
|
} else {
|
2014-11-05 14:48:09 +00:00
|
|
|
o.Data["json"] = ob
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
o.ServeJSON()
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 06:32:17 +00:00
|
|
|
// @Title GetAll
|
|
|
|
// @Description get all objects
|
|
|
|
// @Success 200 {object} models.Object
|
|
|
|
// @Failure 403 :objectId is empty
|
|
|
|
// @router / [get]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (o *ObjectController) GetAll() {
|
2014-06-24 06:32:17 +00:00
|
|
|
obs := models.GetAll()
|
2014-11-05 14:48:09 +00:00
|
|
|
o.Data["json"] = obs
|
2015-10-22 03:32:21 +00:00
|
|
|
o.ServeJSON()
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title Update
|
2014-06-24 06:32:17 +00:00
|
|
|
// @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]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (o *ObjectController) Put() {
|
2016-01-06 03:55:56 +00:00
|
|
|
objectId := o.Ctx.Input.Param(":objectId")
|
2013-09-11 10:18:02 +00:00
|
|
|
var ob models.Object
|
2014-11-05 14:48:09 +00:00
|
|
|
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
|
2013-09-11 10:18:02 +00:00
|
|
|
|
|
|
|
err := models.Update(objectId, ob.Score)
|
|
|
|
if err != nil {
|
2015-09-27 23:42:11 +00:00
|
|
|
o.Data["json"] = err.Error()
|
2013-09-11 10:18:02 +00:00
|
|
|
} else {
|
2014-11-05 14:48:09 +00:00
|
|
|
o.Data["json"] = "update success!"
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
o.ServeJSON()
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title Delete
|
2014-06-24 06:32:17 +00:00
|
|
|
// @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]
|
2014-11-05 14:48:09 +00:00
|
|
|
func (o *ObjectController) Delete() {
|
2016-01-06 03:55:56 +00:00
|
|
|
objectId := o.Ctx.Input.Param(":objectId")
|
2013-09-11 10:18:02 +00:00
|
|
|
models.Delete(objectId)
|
2014-11-05 14:48:09 +00:00
|
|
|
o.Data["json"] = "delete success!"
|
2015-10-22 03:32:21 +00:00
|
|
|
o.ServeJSON()
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 06:32:17 +00:00
|
|
|
`
|
|
|
|
var apiControllers2 = `package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"{{.Appname}}/models"
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-12-14 05:08:47 +00:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
2014-06-24 06:32:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Operations about Users
|
|
|
|
type UserController struct {
|
|
|
|
beego.Controller
|
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title CreateUser
|
2014-06-24 06:32:17 +00:00
|
|
|
// @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}
|
2015-10-22 03:32:21 +00:00
|
|
|
u.ServeJSON()
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title GetAll
|
2014-06-24 06:32:17 +00:00
|
|
|
// @Description get all Users
|
|
|
|
// @Success 200 {object} models.User
|
|
|
|
// @router / [get]
|
|
|
|
func (u *UserController) GetAll() {
|
|
|
|
users := models.GetAllUsers()
|
|
|
|
u.Data["json"] = users
|
2015-10-22 03:32:21 +00:00
|
|
|
u.ServeJSON()
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @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 {
|
2015-09-27 23:42:11 +00:00
|
|
|
u.Data["json"] = err.Error()
|
2014-06-24 06:32:17 +00:00
|
|
|
} else {
|
|
|
|
u.Data["json"] = user
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
u.ServeJSON()
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title Update
|
2014-06-24 06:32:17 +00:00
|
|
|
// @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 {
|
2015-09-27 23:42:11 +00:00
|
|
|
u.Data["json"] = err.Error()
|
2014-06-24 06:32:17 +00:00
|
|
|
} else {
|
|
|
|
u.Data["json"] = uu
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
u.ServeJSON()
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title Delete
|
2014-06-24 06:32:17 +00:00
|
|
|
// @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!"
|
2015-10-22 03:32:21 +00:00
|
|
|
u.ServeJSON()
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 15:32:13 +00:00
|
|
|
// @Title Login
|
2014-06-24 06:32:17 +00:00
|
|
|
// @Description Logs user into the system
|
|
|
|
// @Param username query string true "The username for login"
|
|
|
|
// @Param password query string true "The password for login"
|
2014-11-27 01:02:58 +00:00
|
|
|
// @Success 200 {string} login success
|
2014-06-24 06:32:17 +00:00
|
|
|
// @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"
|
|
|
|
}
|
2015-10-22 03:32:21 +00:00
|
|
|
u.ServeJSON()
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @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"
|
2015-10-22 03:32:21 +00:00
|
|
|
u.ServeJSON()
|
2014-06-24 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 10:18:02 +00:00
|
|
|
`
|
2013-08-27 14:35:48 +00:00
|
|
|
|
2013-12-23 15:28:31 +00:00
|
|
|
var apiTests = `package test
|
2013-08-27 14:35:48 +00:00
|
|
|
|
|
|
|
import (
|
2013-12-23 15:28:31 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2014-04-03 08:46:47 +00:00
|
|
|
"runtime"
|
|
|
|
"path/filepath"
|
2013-12-23 16:00:52 +00:00
|
|
|
_ "{{.Appname}}/routers"
|
2014-06-24 09:01:31 +00:00
|
|
|
|
2020-12-14 05:08:47 +00:00
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2013-12-23 15:28:31 +00:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2013-08-27 14:35:48 +00:00
|
|
|
)
|
|
|
|
|
2014-04-03 08:41:44 +00:00
|
|
|
func init() {
|
2019-01-07 17:34:51 +00:00
|
|
|
_, file, _, _ := runtime.Caller(0)
|
2014-06-24 09:01:31 +00:00
|
|
|
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
|
2014-04-03 08:41:44 +00:00
|
|
|
beego.TestBeegoInit(apppath)
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:28:31 +00:00
|
|
|
// TestGet is a sample to run an endpoint test
|
|
|
|
func TestGet(t *testing.T) {
|
2014-10-01 18:45:16 +00:00
|
|
|
r, _ := http.NewRequest("GET", "/v1/object", nil)
|
2013-12-23 15:28:31 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
beego.BeeApp.Handlers.ServeHTTP(w, r)
|
2014-06-24 09:01:31 +00:00
|
|
|
|
2020-11-05 15:52:08 +00:00
|
|
|
logs.Info("testing", "TestGet", "Code[%d]\n%s", w.Code, w.Body.String())
|
2014-06-24 09:01:31 +00:00
|
|
|
|
2013-12-23 15:28:31 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
2013-08-27 14:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
`
|
2020-07-24 07:54:52 +00:00
|
|
|
var gopath utils.DocValue
|
2020-06-25 14:29:27 +00:00
|
|
|
var beegoVersion utils.DocValue
|
2013-08-27 14:35:48 +00:00
|
|
|
|
2013-09-11 10:18:02 +00:00
|
|
|
func init() {
|
2017-03-06 23:58:53 +00:00
|
|
|
CmdApiapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.")
|
|
|
|
CmdApiapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
|
|
|
CmdApiapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.")
|
2020-07-24 07:54:52 +00:00
|
|
|
CmdApiapp.Flag.Var(&gopath, "gopath", "Support go path,default false")
|
|
|
|
CmdApiapp.Flag.Var(&beegoVersion, "beego", "set beego version,only take effect by go mod")
|
2017-03-06 23:58:53 +00:00
|
|
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdApiapp)
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
func createAPI(cmd *commands.Command, args []string) int {
|
2016-12-03 10:54:41 +00:00
|
|
|
output := cmd.Out()
|
2016-07-31 21:30:35 +00:00
|
|
|
|
2015-03-06 08:07:52 +00:00
|
|
|
if len(args) < 1 {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatal("Argument [appname] is missing")
|
2015-03-06 08:07:52 +00:00
|
|
|
}
|
2016-05-26 16:17:24 +00:00
|
|
|
|
2020-06-25 14:29:27 +00:00
|
|
|
if len(args) >= 2 {
|
2020-07-24 07:54:52 +00:00
|
|
|
err := cmd.Flag.Parse(args[1:])
|
|
|
|
if err != nil {
|
|
|
|
beeLogger.Log.Fatal("Parse args err " + err.Error())
|
|
|
|
}
|
2020-06-25 14:29:27 +00:00
|
|
|
}
|
|
|
|
var appPath string
|
|
|
|
var packPath string
|
|
|
|
var err error
|
2020-07-24 07:54:52 +00:00
|
|
|
if gopath == `true` {
|
2020-12-27 07:56:23 +00:00
|
|
|
beeLogger.Log.Info("Generate api project support GOPATH")
|
2020-06-25 14:29:27 +00:00
|
|
|
version.ShowShortVersionBanner()
|
|
|
|
appPath, packPath, err = utils.CheckEnv(args[0])
|
2017-03-11 08:57:06 +00:00
|
|
|
if err != nil {
|
2020-06-25 14:29:27 +00:00
|
|
|
beeLogger.Log.Fatalf("%s", err)
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-27 07:56:23 +00:00
|
|
|
beeLogger.Log.Info("Generate api project support go modules.")
|
2020-06-25 14:29:27 +00:00
|
|
|
appPath = path.Join(utils.GetBeeWorkPath(), args[0])
|
|
|
|
packPath = args[0]
|
|
|
|
if beegoVersion.String() == `` {
|
2020-11-25 12:56:37 +00:00
|
|
|
beegoVersion.Set(utils.BEEGO_VERSION)
|
2020-06-25 14:29:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsExist(appPath) {
|
|
|
|
beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), appPath)
|
|
|
|
beeLogger.Log.Warn(colors.Bold("Do you want to overwrite it? [Yes|No] "))
|
|
|
|
if !utils.AskForConfirmation() {
|
|
|
|
os.Exit(2)
|
2017-03-11 08:57:06 +00:00
|
|
|
}
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
2016-05-26 16:17:24 +00:00
|
|
|
|
2018-09-13 08:09:45 +00:00
|
|
|
appName := path.Base(args[0])
|
2013-09-11 10:18:02 +00:00
|
|
|
if err != nil {
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Fatalf("%s", err)
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|
2017-03-06 23:58:53 +00:00
|
|
|
if generate.SQLDriver == "" {
|
|
|
|
generate.SQLDriver = "mysql"
|
2014-08-08 16:55:55 +00:00
|
|
|
}
|
2016-06-01 12:30:29 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Info("Creating API...")
|
2016-06-01 12:30:29 +00:00
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
os.MkdirAll(appPath, 0755)
|
2020-07-24 07:54:52 +00:00
|
|
|
if gopath != `true` { //generate first for calc model name
|
2020-06-25 14:29:27 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "go.mod"), "\x1b[0m")
|
2020-07-11 04:29:24 +00:00
|
|
|
utils.WriteToFile(path.Join(appPath, "go.mod"), fmt.Sprintf(goMod, packPath, utils.GetGoVersionSkipMinor(), beegoVersion.String()))
|
2020-06-25 14:29:27 +00:00
|
|
|
}
|
2017-03-11 08:57:06 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath, "\x1b[0m")
|
|
|
|
os.Mkdir(path.Join(appPath, "conf"), 0755)
|
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf"), "\x1b[0m")
|
|
|
|
os.Mkdir(path.Join(appPath, "controllers"), 0755)
|
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "controllers"), "\x1b[0m")
|
|
|
|
os.Mkdir(path.Join(appPath, "tests"), 0755)
|
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "tests"), "\x1b[0m")
|
2013-09-11 10:18:02 +00:00
|
|
|
|
2017-03-06 23:58:53 +00:00
|
|
|
if generate.SQLConn != "" {
|
2018-09-13 08:09:45 +00:00
|
|
|
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")
|
|
|
|
confContent := strings.Replace(apiconf, "{{.Appname}}", appName, -1)
|
|
|
|
confContent = strings.Replace(confContent, "{{.SQLConnStr}}", generate.SQLConn.String(), -1)
|
|
|
|
utils.WriteToFile(path.Join(appPath, "conf", "app.conf"), confContent)
|
|
|
|
|
2017-03-11 08:57:06 +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")
|
|
|
|
mainGoContent := strings.Replace(apiMainconngo, "{{.Appname}}", packPath, -1)
|
|
|
|
mainGoContent = strings.Replace(mainGoContent, "{{.DriverName}}", string(generate.SQLDriver), -1)
|
2017-03-06 23:58:53 +00:00
|
|
|
if generate.SQLDriver == "mysql" {
|
2017-03-11 08:57:06 +00:00
|
|
|
mainGoContent = strings.Replace(mainGoContent, "{{.DriverPkg}}", `_ "github.com/go-sql-driver/mysql"`, -1)
|
2017-03-06 23:58:53 +00:00
|
|
|
} else if generate.SQLDriver == "postgres" {
|
2017-03-11 08:57:06 +00:00
|
|
|
mainGoContent = strings.Replace(mainGoContent, "{{.DriverPkg}}", `_ "github.com/lib/pq"`, -1)
|
2014-08-22 07:46:36 +00:00
|
|
|
}
|
2017-03-11 08:57:06 +00:00
|
|
|
utils.WriteToFile(path.Join(appPath, "main.go"),
|
2014-08-08 16:55:55 +00:00
|
|
|
strings.Replace(
|
2017-03-11 08:57:06 +00:00
|
|
|
mainGoContent,
|
2014-08-08 16:55:55 +00:00
|
|
|
"{{.conn}}",
|
2017-03-06 23:58:53 +00:00
|
|
|
generate.SQLConn.String(),
|
2014-08-08 16:55:55 +00:00
|
|
|
-1,
|
|
|
|
),
|
|
|
|
)
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Infof("Using '%s' as 'driver'", generate.SQLDriver)
|
|
|
|
beeLogger.Log.Infof("Using '%s' as 'conn'", generate.SQLConn)
|
|
|
|
beeLogger.Log.Infof("Using '%s' as 'tables'", generate.Tables)
|
2017-03-11 08:57:06 +00:00
|
|
|
generate.GenerateAppcode(string(generate.SQLDriver), string(generate.SQLConn), "3", string(generate.Tables), appPath)
|
2014-08-08 16:55:55 +00:00
|
|
|
} else {
|
2018-09-13 08:09:45 +00:00
|
|
|
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")
|
|
|
|
confContent := strings.Replace(apiconf, "{{.Appname}}", appName, -1)
|
|
|
|
confContent = strings.Replace(confContent, "{{.SQLConnStr}}", "", -1)
|
|
|
|
utils.WriteToFile(path.Join(appPath, "conf", "app.conf"), confContent)
|
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
os.Mkdir(path.Join(appPath, "models"), 0755)
|
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "models"), "\x1b[0m")
|
|
|
|
os.Mkdir(path.Join(appPath, "routers"), 0755)
|
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "routers")+string(path.Separator), "\x1b[0m")
|
2014-08-08 16:55:55 +00:00
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "controllers", "object.go"), "\x1b[0m")
|
|
|
|
utils.WriteToFile(path.Join(appPath, "controllers", "object.go"),
|
|
|
|
strings.Replace(apiControllers, "{{.Appname}}", packPath, -1))
|
2013-09-11 10:18:02 +00:00
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "controllers", "user.go"), "\x1b[0m")
|
|
|
|
utils.WriteToFile(path.Join(appPath, "controllers", "user.go"),
|
|
|
|
strings.Replace(apiControllers2, "{{.Appname}}", packPath, -1))
|
2014-06-24 06:32:17 +00:00
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "tests", "default_test.go"), "\x1b[0m")
|
|
|
|
utils.WriteToFile(path.Join(appPath, "tests", "default_test.go"),
|
|
|
|
strings.Replace(apiTests, "{{.Appname}}", packPath, -1))
|
2013-12-23 15:28:31 +00:00
|
|
|
|
2017-03-11 08:57:06 +00:00
|
|
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "routers", "router.go"), "\x1b[0m")
|
|
|
|
utils.WriteToFile(path.Join(appPath, "routers", "router.go"),
|
|
|
|
strings.Replace(apirouter, "{{.Appname}}", packPath, -1))
|
2013-08-27 14:35:48 +00:00
|
|
|
|
2017-03-11 08:57:06 +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")
|
|
|
|
utils.WriteToFile(path.Join(appPath, "models", "object.go"), APIModels)
|
2013-09-11 10:18:02 +00:00
|
|
|
|
2017-03-11 08:57:06 +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")
|
|
|
|
utils.WriteToFile(path.Join(appPath, "models", "user.go"), APIModels2)
|
2014-06-24 06:32:17 +00:00
|
|
|
|
2017-03-11 08:57:06 +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")
|
|
|
|
utils.WriteToFile(path.Join(appPath, "main.go"),
|
|
|
|
strings.Replace(apiMaingo, "{{.Appname}}", packPath, -1))
|
2014-08-08 16:55:55 +00:00
|
|
|
}
|
2017-03-06 23:58:53 +00:00
|
|
|
beeLogger.Log.Success("New API successfully created!")
|
2014-08-22 03:20:29 +00:00
|
|
|
return 0
|
2013-09-11 10:18:02 +00:00
|
|
|
}
|