Golang (5) using Beego to develop API server and front-end classmate split development, using swagger

Source: Internet
Author: User

1,beego API

Swagger is a normative and complete framework for generating, describing, invoking, and visualizing RESTful Web services. The overall goal is to have the client and the file system update at the same speed as the server.
Project address is: http://swagger.io/
Use Beego to develop the stuff that comes with integrated swagger.
Https://beego.me/docs/advantage/docs.md
GitHub Address:
Https://github.com/beego/swagger
Beego using the latest version: 1.8.0

2, very convenient

Create a project: Apiserver

bee api apiservercd apiserver/-gendoc=true-downdoc=true

The latest swagger compressed files are automatically downloaded:

 ./Geneva/ .  -: the: ,INFO?0001Using' Apiserver 'As' appname ' ./Geneva/ .  -: the: ,INFO?0002Loading Default configuration ... ./Geneva/ .  -: the: ,INFO?0003Downloading' Https://github.com/beego/swagger/archive/v2.zip 'To' Swagger.zip '... ./Geneva/ .  -: the: +SUCCESS?0004 800610Bytes downloaded!

With a successful start, direct access is available:
Http://localhost:8080/swagger/#/user

Very flashy interface, red green yellow purple.

3, the code is messy.

The configuration file adds

EnableDocs = true

Where commentsrouter_controllers.go this file is automatically generated according to the contents of the controller.
And the NameSpace is configured in the router. General configuration of the next version of the information.

JSON of test data can be submitted directly to the page

Queries can be made directly on the page.
Not quite the same as the official documentation. Because the version has been upgraded.

4, one form submission

The following notes are studied:

// @DescriptionCreate user// @ParamUsername FormData String"Zhangsan"  true    "User name"// @ParamPassword FormData String"123456"    true    "Password"// @Success  ${int} models. User.ID models. User.username models. User// @Failure 403Body isEmpty// @router/adduser [Post]func (U *usercontroller) post () {
    1. Name of parameter
    2. Parameter type, the value can be formData, query, path, body, Header,formdata represents the data of the POST request, query represents the parameters with the URL, path represents the request path parameters, such as the above example of the key, Body representation is a
      Raw data request, the header represents the parameters that are taken in the header information.
    3. Parameter type
    4. Default value
    5. Whether you must
    6. Comments

The resulting interface is:

You can send a parameter request, and you can bring the cookie in.
But it's not a fake cookie.
Consider using the header to hold the Token parameter to keep the user logged in.
Then the request is all put in, write dead a token to keep the user logged in.
Convenient for the front-end students debugging and development.

4, summary

The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/69486943 not allowed to reprint without the Bo master.
Bo main address is: Http://blog.csdn.net/freewebsys

Beego has supported swagger, and it is most convenient to develop a project that separates projects from front to back.
You can also request that cookies be planted in a cookie and then use subdomains for cross-domain access.
Turn on cross-domain access in Beego.
Tokens can also be stored and placed in the header to pass user logins.
At the same time for convenient testing, you can write several default values to write dead log on.
Convenient test excuses.
This swagger can be used as a postman set to test and debug directly.
Very consistent with project split development. It's very convenient.

Golang (5) using Beego to develop API server and front-end classmate split development, using swagger

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.