Create restful apps with one-click Beego

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Getting Started with API application development

Go is great for developing API apps, and I think it's the biggest advantage of go relative to other dynamic languages. Beego provides a very powerful and fast tool for developing API applications, allowing users to quickly build API application prototypes and concentrate on business logic.

Quickly build prototypes

The Bee Rapid development tool provides an API application built-in tool that allows you to quickly build an API application by executing the following command in any directory under GOPATH/SRC:

Bee API Beeapi

The directory structure of the app

The directory structure of the app is as follows:

├──conf│   └──app.conf├──controllers│   └──default.go├──models│    └──object.go└──main.go

SOURCE parsing

    • The main API configuration for app.conf is as follows:

Autorender = False//API application does not require template rendering, so turn off automatic rendering

Copyrequestbody = True//restful when the app sends the message it is the raw body, not the normal form form, so you need to read the body information extra

    • Main.go files are primarily for restful routing registrations

Beego. Restrouter ("/object", &controllers. obejctcontroller{})

This route can match the following rules

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
    • The Obejctcontroller implements the corresponding method:
Type Obejctcontroller struct {    beego. Controller}func (This *obejctcontroller) Post () {}func (this *obejctcontroller) Get () {}func (this *obejctcontroller) Put () {}func (this *obejctcontroller) Delete () {}
    • Models inside to achieve the corresponding operation of the object of adding and removing and other operations

Test

    • Add an object:

      Curl-x post-d ' {"score": 1337, "playername": "Sean Plott"} ' Http://127.0.0.1:8080/object

      Returns a corresponding objectid:astaxie1373349756660423900

    • Querying an Object

      Curl-x GET http://127.0.0.1:8080/object/astaxie1373349756660423900

    • Querying all of the objects

      Curl-x GET Http://127.0.0.1:8080/object

    • Update an Object

      Curl-x put-d ' {"Score": 10000} ' http://127.0.0.1:8080/object/astaxie1373349756660423900

    • Delete an Object

      Curl-x DELETE http://127.0.0.1:8080/object/astaxie1373349756660423900

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.