转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/54114606,建议读者阅读原文,确保获得完整的信息
本文介绍利用beego框架快速构建web api服务,可以通过beego提供的命令来创建API工程。beego命令更详细的使用说明可以参考官方文档
bee api apiproject
工程创建后,主要包含如图中的conf、controllers、models模块
conf文件夹中包含默认文件app.conf,在models中创建user.go文件,controllers中创建user.go文件,具体如下:
models文件夹中的user.go
package models
import (
"github.com/astaxie/beego/orm"
"fmt"
)
type User struct {
Id int
User_name string
Age int
Email string
Staff_id string
Position string
Extension_number string
Telephone_number string
Office_location string
}
func init() {
// 需要在init中注册定义的model
orm.RegisterModel(new(User))
}
func UserAdd(o orm.Ormer,user User) (int64, error){
// insert
return o.Insert(&user)
}
func UserGetOne(o orm.Ormer,id int) (User, error){
// query
var user User
//var sql = "select id,user_name,age,email,staff_id,position,extension_number,telephone_number,office_location from user where id = ? and enabled = 1"
var sql = "select * from user where id = ? and enabled = 1"
err := o.Raw(sql,id).QueryRow(&user)
fmt.Printf("ERR: %v\n", err)
return user,err
}
func UserQuery(o orm.Ormer,sql string,paras [] interface{}) ([] User,error){
// query
var users [] User
num,err := o.Raw(sql,paras).QueryRows(&users)
fmt.Printf("NUM: %d, ERR: %v\n", num, err)
return users,err
}
func UserCut(o orm.Ormer,user User) (int64, error){
// delete
return o.Delete(&user,"user_name")
}
func UserUpdate(o orm.Ormer,user User) (int64, error){
// update
return o.Update(&user,"user_name")
}
controllers文件夹中的user.go
package controllers
import (
_"helloapi/models"
"github.com/astaxie/beego"
_"github.com/astaxie/beego/orm"
_"strconv"
"strconv"
"github.com/astaxie/beego/orm"
"helloapi/models"
)
// Operations about user
type UserController struct {
beego.Controller
}
func (this *UserController) Get() {
userId := this.Input().Get("userid")
beego.Info(userId)
if userId != "" {
id,err:=strconv.Atoi(userId)
db := orm.NewOrm()
user, err := models.UserGetOne(db,id)
if err != nil {
this.Data["json"] = err.Error()
} else {
this.Data["json"] = user
}
}
this.ServeJSON()
}
func (this *UserController) Post() {
user_name := this.Input().Get("user_name")
age := this.Input().Get("age")
email := this.Input().Get("email")
staff_id := this.Input().Get("staff_id")
beego.Info(user_name,age,email,staff_id)
if user_name != "" {
db := orm.NewOrm()
age,err:=strconv.Atoi(age)
user := models.User{User_name:user_name,Age:age,Email:email,Staff_id:staff_id}
num, err := models.UserAdd(db,user)
if err != nil {
this.Data["json"] = err.Error()
} else {
this.Data["json"] = num
}
}
this.ServeJSON()
}
main.go
package main
import (
"github.com/astaxie/beego/orm"
_"github.com/go-sql-driver/mysql"
"github.com/astaxie/beego"
"helloapi/controllers"
)
func init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "manager:Abc$1234@tcp(100.69.197.189:3306)/test?charset=utf8")
orm.SetMaxIdleConns("default",1000)
orm.SetMaxOpenConns("default",2000)
}
func main() {
beego.Router("/v1/user", &controllers.UserController{})
beego.Router("/v1/object", &controllers.ObjectController{})
beego.Run()
}
启动main.go
该demo中提供了2个接口方法:1、用户查询;2、用户创建
浏览器中输入如下内容:
http://30.85.231.20:9090/v1/user?userid=41
返回结果如下:
此方法为POST方法,故利用到了http请求工具restclient-ui-fat-3.6.1.jar,此工具对于测试接口请求很是方便,后续抽空专门介绍下这个工具。
http://30.85.231.20:9090/v1/[email protected]&staff_id=095621
返回结果如下:
数据库入库结果:
当长时间没有请求数据库,再次请求时,提示如下信息,原来的连接管道已损坏,重新连接。