Create restful APIs based on Beego one-click

Source: Internet
Author: User

Learn from here

The restful protocol can be referred to below, of course, the best, see rails rake routes, the most standard

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

Direct Sticker Beego Code

App.conf

AppName =8080=true

Router.go

Package Routersimport (    "hello/controllers"    "github.com /astaxie/beego") func init () {    beego. Restrouter ("/object", &controllers. objectcontroller{})    Beego. Router ("/", &controllers. maincontroller{})}

Objectcontroller.go

Package Controllersimport ("FMT"    "Github.com/abbot/go-http-auth"    "Github.com/astaxie/beego") Type Objectcontrollerstruct{Beego. Controller}//API apps don't need template rendering, so turn off automatic renderingFunc ( This*Objectcontroller) Prepare () { This. Enablerender =false}func ( This*Objectcontroller) Post () {}func Secret (user, Realmstring)string {    ifuser = ="John" {        //password is "Hello"        return "$1$DLPL2MQE$OQMN16Q49SQDMHENQUNGS1"    }    return ""}//Basic Auth needs a three-party library to doFunc ( This*Objectcontroller) Get () {authenticate:= Auth. Newbasicauthenticator ("example.com", Secret)ifUsername: = Authenticate. Checkauth ( This. Ctx.request); Username = =""{authenticate. Requireauth ( This. Ctx.responsewriter, This. Ctx.request)}//the parameters inside the URL get this. ctx.input.params[": objectId"]    params:= This. Ctx.Input.Params forKey, Value: = Rangeparams{info:= Fmt. Sprintf ("%s:%s\n", key, value) This. Ctx.writestring (Info)}}func ( This*Objectcontroller) Put () {}func ( This*Objectcontroller) Delete () {}

No, it's all in the code.

Create restful APIs based on Beego one-click

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.