使用BEEGO建立一個基本的API架構

來源:互聯網
上載者:User

標籤:find   json   reg   using   git   cdir   架構   success   iss   

用BEE API命令產生架構。

然後自行更改MODELS,加入MYSQL支援ORM.

然後,自訂了欄位的對應,表的名稱等。

參考URL:

http://www.cnblogs.com/studyzy/p/6964612.html

main.go

package mainimport (_ "papi/routers""github.com/astaxie/beego""github.com/astaxie/beego/orm"_ "github.com/go-sql-driver/mysql")func init() {orm.RegisterDriver("mysql", orm.DRMySQL)orm.RegisterDataBase("default", "mysql", "user:[email protected](1ip2:3306)/DB?charset=utf8")orm.SetMaxIdleConns("default", 1000)orm.SetMaxOpenConns("default", 2000)}func main() {orm.Debug = trueif beego.BConfig.RunMode == "dev" {beego.BConfig.WebConfig.DirectoryIndex = truebeego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"}beego.Run()}

  

conf/app.conf

appname = papihttpport = 5757runmode = devautorender = falsecopyrequestbody = trueEnableDocs = trueEnableAdmin = trueAdminAddr = "127.0.0.1"AdminPort = 5758

  models.go

package modelsimport ("fmt""time""github.com/astaxie/beego/orm")type pmLog struct {Id            int       `orm:"column(id)"`DeployName    string    `orm:"column(deployName)"`AppName       string    `orm:"column(appName)"`SiteName      string    `orm:"column(siteName)"`IpAddress     string    `orm:"column(ipAddress)"`EnvType       string    `orm:"column(envType)"`UserName      string    `orm:"column(userName)"`OperationType string    `orm:"column(operationType)"`OperationNo   int       `orm:"column(operationNo)"`LogContent    string    `orm:"column(logContent)"`LogDateTime   time.Time `orm:"auto_now_add;type(datetime);column(logDateTime)"`}func (u *pmLog) TableName() string {return "pmlog"}func init() {orm.RegisterModel(new(pmLog))}func GetAllpmLog() []*pmLog {o := orm.NewOrm()o.Using("default")var pmlogs []*pmLogq := o.QueryTable("pmlog")q.All(&pmlogs)return pmlogs}func GetpmLogById(id int) pmLog {u := pmLog{Id: id}o := orm.NewOrm()o.Using("default")err := o.Read(&u)if err == orm.ErrNoRows {fmt.Println("no result")} else if err == orm.ErrMissPK {fmt.Println("can‘t find PK")}return u}func AddpmLog(pmlog *pmLog) int {o := orm.NewOrm()o.Using("default")o.Insert(pmlog)return pmlog.Id}func UpdatepmLog(pmlog *pmLog) {o := orm.NewOrm()o.Using("default")o.Update(pmlog)}func DeletepmLog(id int) {o := orm.NewOrm()o.Using("default")o.Delete(&pmLog{Id: id})}

  controllers.go

package controllersimport ("pmlogapi/models""encoding/json""github.com/astaxie/beego")type pmLogController struct {beego.Controller}// @Title 擷取所有部署日誌// @Description 返回所有的部署日誌// @Success 200 {object} models.pmLog// @router / [get]func (u *pmLogController) GetAll() {ss := models.GetAllpmLog()u.Data["json"] = ssu.ServeJSON()}// @Title 擷取一條日誌// @Description 返回某條日誌// @Param id path int true "The key for staticblock"// @Success 200 {object} models.pmLog// @router /:id [get]func (u *pmLogController) GetById() {id, _ := u.GetInt(":id")s := models.GetpmLogById(id)u.Data["json"] = su.ServeJSON()}// @Title 建立一條日誌// @Description 建立日誌的描述// @Param body body models.pmLog true "body  for content"// @Success 200 {int} models.pmLog.Id// @Failure 403 body is empty// @router / [post]func (u *pmLogController) Post() {var s models.pmLogjson.Unmarshal(u.Ctx.Input.RequestBody, &s)uid := models.AddpmLog(&s)u.Data["json"] = uidu.ServeJSON()}// @Title 修改日誌// @Description 修改日誌的內容// @Param body body models.pmLog true "body  for pmlog content"// @Success 200 {int} models.pmLog// @Failure 403 body is empty// @router / [put]func (u *pmLogController) Update() {var s models.pmLogjson.Unmarshal(u.Ctx.Input.RequestBody, &s)models.UpdatepmLog(&s)u.Data["json"] = su.ServeJSON()}// @Title 刪除日誌// @Description 刪除指定的日誌// @Param id path int true "The key for staticblock"// @Success 200 {object} models.pmLog// @router /:id [delete]func (u *pmLogController) Delete() {id, _ := u.GetInt(":id")models.DeletepmLog(id)u.Data["json"] = trueu.ServeJSON()}

  router.go

// @APIVersion 1.0.0// @Title beego Test API// @Description beego has a very cool tools to autogenerate documents for your API// @Contact [email protected]// @TermsOfServiceUrl http://beego.me/// @License Apache 2.0// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.htmlpackage routersimport ("pmlogapi/controllers""github.com/astaxie/beego")func init() {ns := beego.NewNamespace("/v1",beego.NSNamespace("/pmlog",beego.NSInclude(&controllers.pmLogController{},),),)beego.AddNamespace(ns)}

  

 

使用BEEGO建立一個基本的API架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.