Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. In beego framework, support four routing Settings, they are: basic routing, fixed routing, regular routing and automatic routing.

  1. Basic routing:

Directly through beego. Get beego. POST, beego. Head, beego. Delete method such as routed to mapping, we through the code to demonstrate for you. As we know, the common HTTP request method operations are: GET, HEAD, PUT, POST, DELETE, OPTIONS and so on.

  • GET routes are the basic GET routes first. Let’s show it to you by example.

    beego.GET("".func)
    Copy the code
  • POST routing:

    beego.POST("".func)
    Copy the code
  • In addition, Patch, Head, and Delete routes are supported.

So this request and the way to find the request method type is RESTful, and the most common one we use is RESTful and it’s a very common one in development apis right now, and it’s RESTful and it’s just that the user automatically executes the GET method when they make a GET request, The Post method is executed on the Post request.

  1. Fixed route:

Such code forms as the following:

beego.Router("/",controller);
Copy the code

Get requests will correspond to Get methods, Post will correspond to Post methods, Delete will correspond to Delete methods, and Header methods will correspond to Header methods.

  1. Regular routing:

Regular routes support regular expressions that match certain formats on the basis of fixed routes. For example: ID,:username, custom re,file path and suffix switch, and full match operations.

  1. Custom Route

The above two routes are default based on the request type. Get executes Get, and Post executes Post, which are relatively limited. Since most of the development is using fixed matching to directly implement the corresponding logical control method, BeeGo provides us with a custom routing configuration that can be customized. The method is as follows:

beego.Router("/",&IndexController{},"")
Copy the code

HTTP Method can be used:

  • “*” : contains all the following functions
  • “Get” : get request
  • “Post” : Indicates a POST request
  • “Put” : Put request
  • “Delete” : indicates a delete request
  • “Patch” : indicates the patch request
  • “Options” : options request
  • “Head” : head request

Beego. Controller defines a number of request methods, such as Init, Prepare, Post, Get, Head, and Delete. The above analysis is the Router and Controller running logic of Beego framework.

  1. Routing group

Example:

func init(a) {
	https://beego.me/docs/mvc/controller/router.md / / meaning
	ns := beego.NewNamespace("/v1",
		beego.NSRouter("/home", &controllers.UserController{}, "post:GetAll"), //http://localhost:8080/v1/home
		beego.NSNamespace("/object2",
			beego.NSRouter("/home2", &controllers.UserController{}, "get:Get"), //http://localhost:8080/v1/object2/home2? uid=asdf
		),
	)
	beego.AddNamespace(ns)

	// Add a log interceptor
	var FilterLog = func(ctx *context.Context) {
		url, _ := json.Marshal(ctx.Input.Data()["RouterPattern"])
		params, _ := json.Marshal(ctx.Request.Form)
		outputBytes, _ := json.Marshal(ctx.Input.Data()["json"])
		divider := "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"
		topDivider := "┌." " + divider
		middleDivider := "├" + divider
		bottomDivider := "└" + divider
		outputStr := "\n" + topDivider + "\n│ request address :" + string(url) + "\n" + middleDivider + "\n│ request parameters :" + string(params) + "\n│ Return data :" + string(outputBytes) + "\n" + bottomDivider
		logs.Info(outputStr)
	}
	// The last parameter must be set to false otherwise the data cannot be printed
	/ / the following configuration http://localhost:8080/v1/home will not output log, http://localhost:8080/v1/object2/home2, this address will be the output log
	beego.InsertFilter("*/home2", beego.FinishRouter, FilterLog, false)}Copy the code