mirror of
https://github.com/beego/bee.git
synced 2024-11-21 23:50:54 +00:00
update the new api application
This commit is contained in:
parent
aa92fc4854
commit
ff63f454a7
324
apiapp.go
324
apiapp.go
@ -35,7 +35,8 @@ In the appname folder has the follow struct:
|
||||
├── conf
|
||||
│ └── app.conf
|
||||
├── controllers
|
||||
│ └── default.go
|
||||
│ └── object.go
|
||||
│ └── user.go
|
||||
├── routers
|
||||
│ └── router.go
|
||||
├── tests
|
||||
@ -43,46 +44,64 @@ In the appname folder has the follow struct:
|
||||
├── main.go
|
||||
└── models
|
||||
└── object.go
|
||||
└── user.go
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
var apiconf = `
|
||||
appname = {{.Appname}}
|
||||
var apiconf = `appname = {{.Appname}}
|
||||
httpport = 8080
|
||||
runmode = dev
|
||||
autorender = false
|
||||
copyrequestbody = true
|
||||
EnableDocs = true
|
||||
`
|
||||
var apiMaingo = `package main
|
||||
|
||||
import (
|
||||
_ "{{.Appname}}/docs"
|
||||
_ "{{.Appname}}/routers"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// Objects
|
||||
|
||||
// URL HTTP Verb Functionality
|
||||
// /object POST Creating Objects
|
||||
// /object/<objectId> GET Retrieving Objects
|
||||
// /object/<objectId> PUT Updating Objects
|
||||
// /object GET Queries
|
||||
// /object/<objectId> DELETE Deleting Objects
|
||||
|
||||
func main() {
|
||||
if beego.RunMode == "dev" {
|
||||
beego.DirectoryIndex = true
|
||||
beego.StaticDir["/swagger"] = "swagger"
|
||||
}
|
||||
beego.Run()
|
||||
}
|
||||
`
|
||||
var apirouter = `package routers
|
||||
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
|
||||
|
||||
import (
|
||||
"{{.Appname}}/controllers"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
func init() {
|
||||
beego.RESTRouter("/object", &controllers.ObjectController{})
|
||||
ns := beego.NewNamespace("/v1",
|
||||
beego.NSNamespace("/object",
|
||||
beego.NSInclude(
|
||||
&controllers.ObjectController{},
|
||||
),
|
||||
),
|
||||
beego.NSNamespace("/user",
|
||||
beego.NSInclude(
|
||||
&controllers.UserController{},
|
||||
),
|
||||
),
|
||||
)
|
||||
beego.AddNamespace(ns)
|
||||
}
|
||||
`
|
||||
|
||||
@ -138,23 +157,117 @@ func Update(ObjectId string, Score int64) (err error) {
|
||||
func Delete(ObjectId string) {
|
||||
delete(Objects, ObjectId)
|
||||
}
|
||||
|
||||
`
|
||||
|
||||
var apiModels2 = `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 apiControllers = `package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/astaxie/beego"
|
||||
"{{.Appname}}/models"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
type ResponseInfo struct {
|
||||
}
|
||||
|
||||
// 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)
|
||||
@ -163,6 +276,12 @@ func (this *ObjectController) Post() {
|
||||
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 != "" {
|
||||
@ -172,13 +291,28 @@ func (this *ObjectController) Get() {
|
||||
} else {
|
||||
this.Data["json"] = ob
|
||||
}
|
||||
} else {
|
||||
obs := models.GetAll()
|
||||
this.Data["json"] = obs
|
||||
}
|
||||
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
|
||||
@ -193,6 +327,12 @@ func (this *ObjectController) Put() {
|
||||
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)
|
||||
@ -200,6 +340,126 @@ func (this *ObjectController) Delete() {
|
||||
this.ServeJson()
|
||||
}
|
||||
|
||||
`
|
||||
var apiControllers2 = `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 apiTests = `package test
|
||||
@ -262,10 +522,12 @@ func createapi(cmd *Command, args []string) {
|
||||
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, "models"), 0755)
|
||||
fmt.Println(path.Join(apppath, "routers") + string(path.Separator))
|
||||
os.Mkdir(path.Join(apppath, "routers"), 0755)
|
||||
fmt.Println("create models:", path.Join(apppath, "models"))
|
||||
os.Mkdir(path.Join(apppath, "routers"), 0755)
|
||||
fmt.Println(path.Join(apppath, "routers") + string(path.Separator))
|
||||
os.Mkdir(path.Join(apppath, "tests"), 0755)
|
||||
fmt.Println("create tests:", path.Join(apppath, "tests"))
|
||||
|
||||
@ -273,10 +535,14 @@ func createapi(cmd *Command, args []string) {
|
||||
writetofile(path.Join(apppath, "conf", "app.conf"),
|
||||
strings.Replace(apiconf, "{{.Appname}}", args[0], -1))
|
||||
|
||||
fmt.Println("create controllers default.go:", path.Join(apppath, "controllers", "default.go"))
|
||||
writetofile(path.Join(apppath, "controllers", "default.go"),
|
||||
fmt.Println("create controllers object.go:", path.Join(apppath, "controllers", "object.go"))
|
||||
writetofile(path.Join(apppath, "controllers", "object.go"),
|
||||
strings.Replace(apiControllers, "{{.Appname}}", packpath, -1))
|
||||
|
||||
fmt.Println("create controllers user.go:", path.Join(apppath, "controllers", "user.go"))
|
||||
writetofile(path.Join(apppath, "controllers", "user.go"),
|
||||
strings.Replace(apiControllers2, "{{.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(apiTests, "{{.Appname}}", packpath, -1))
|
||||
@ -288,6 +554,12 @@ func createapi(cmd *Command, args []string) {
|
||||
fmt.Println("create models object.go:", path.Join(apppath, "models", "object.go"))
|
||||
writetofile(path.Join(apppath, "models", "object.go"), apiModels)
|
||||
|
||||
fmt.Println("create models user.go:", path.Join(apppath, "models", "user.go"))
|
||||
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"))
|
||||
writetofile(path.Join(apppath, "main.go"),
|
||||
strings.Replace(apiMaingo, "{{.Appname}}", packpath, -1))
|
||||
|
Loading…
Reference in New Issue
Block a user