“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Want to learn to Go Web? Let’s take a look at how to build a BeeGo project and implement simple features.

This article appears in my column:Let’s Golang

Download and install Bee and BeeGo

Let’s go to Github and download Bee and BeeGo.

We can use Go Get to download:

 go get -u github.com/beego/beego
 ​
 go get -u https://github.com/beego/bee
Copy the code

After downloading, go to the directory of Bee, open the command line window of the current directory, and use Go Build to compile.

Then run bee.exe from the command line.

2. Create a Beego project

To create a new Go Web project, we need to use bee to create a new project. We just need to execute bee new [name] on the command line

The command to create a new project needs to be executed in the SRC directory of GOPATH.

3. Install dependencies

Then we found that there were two dependencies in the go.mod file that needed to be installed, we just typed Go Tidy in the terminal

Iv. Running projects

Then type D:\Go\ Bin \ SR \github.com\beego\bee\ Bee Run the item.

Visit http:// localhost :8080

We can see that it is running successfully!

The following simple build to achieve a few small functions!

5. Regular routes

Let’s start with beego’s regular routing.

Place routing files in the routers.

We write the following code in router.go:

 package routers
 ​
 import (
     "donatechain/controllers"
     beego "github.com/beego/beego/v2/server/web"
 )
 ​
 func init(a) {
     beego.Router("/", &controllers.MainController{})
     beego.Router("/api/:username([\w]+)", &controllers.Demo1Controller{})
 }
 ​
Copy the code

Beego.Router is a Router that adds a modular Controller handler to BeeApp.

The first route is the root path to the MainController in controllers, using the controller pointer.

The second route is similar to access http://localhost:8080/api/JKJJ, JKJJ is the username parameter, we can obtain the parameters in the controller.

 package controllers
 ​
 import beego "github.com/beego/beego/v2/server/web"
 ​
 type Demo1Controller struct {
     beego.Controller
 }
 ​
 func (this *Demo1Controller) Get(a) {
     username := this.Ctx.Input.Param(":username")
     this.Ctx.WriteString("username = " + username)
 }
 ​
Copy the code

This.ctx.input.param (“:username”) returns the router parameter with the given key. Then use this.ctx. WriteString(“username = “+ username) to write to the page.

Let’s see if we can do it.

It seems possible!

Use Cooike

We sometimes use Cooike in Web projects. Now let’s learn how to use Cooike in Beego projects. Let’s create a Cooike controller:

 package controllers
 ​
 import beego "github.com/beego/beego/v2/server/web"
 ​
 type CooikeController struct {
     beego.Controller
 }
 ​
 func (this *CooikeController) Get(a) {
     this.Ctx.SetCookie("username"."ReganYue")
     cookie := this.Ctx.GetCookie("username")
     this.Ctx.WriteString("my username is " + cookie)
 }
 ​
Copy the code

Then, if we want to access the Router, we must add this controller to the Router, so we add this line to the Router:

 package routers
 ​
 import (
     "donatechain/controllers"
     beego "github.com/beego/beego/v2/server/web"
 )
 ​
 func init(a) {
     beego.Router("/cooike", &controllers.CooikeController{})
 }
 ​
Copy the code

Then run the project to access the /cooike path and we can see:

If you don’t see ReganYue, you can refresh it a few times, or empty the Cooikes and visit again.

Let’s take a closer look at the function that sets Cooike and gets Cooike.

 func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
     ctx.Output.Cookie(name, value, others...)
 }
Copy the code

The first argument is the name of Cooike, and the second argument is the value of Cooike. The third parameter is used to set the Max Age time of Cooike, the path of Cooike, the domain of Cooike, secure and Httponly. If the Cooike secure parameter is true, the Cooike can only be sent to the server over HTTPS, not HTTP. If Cooike is set to HttpOnly=true, then js will not get it and document.cookie cannot be used to output Cooike.

 func (ctx *Context) GetCookie(key string) string {
     return ctx.Input.Cookie(key)
 }
Copy the code

This is the value used to get Cooike, with only one argument, the key name of Cooike.