Home  >  Article  >  Backend Development  >  Go language uses revel framework to implement user registration tutorial (with code)

Go language uses revel framework to implement user registration tutorial (with code)

尚
forward
2019-11-26 11:29:412829browse

Go language uses revel framework to implement user registration tutorial (with code)

User registration, login and logout are inevitable functions of any website. It can be said that this is an area where reinventing the wheel should be done. Everyone who builds a website should have it. Have done it many times. From such a small function, you can actually see most of the things in the web framework used.

Let us use this basic module to take a look at revel today.

Let’s first sort out the technical framework and components we selected:

web framework: revel

Database: mongodb

Database driver: mgo

If you want to do your job well, you must first sharpen your tools. Here we recommend a GUI client for mongodb - mongovue. It can be said that without this tool, we would be in a lot of pain during the development process.

It is assumed here that you already have the most basic knowledge of Go language and have configured GOROOT and GOPATH.

First, run the following command under GOPATH to install revel, and compile the revel tool.

go get github.com/robfig/revel
go build –o bin/revel.exe github.com/robfig/revel/revel

After completion, go to GOPATH\bin to see if revel.exe has been compiled. For ease of use, I added GOPATH\bin to the environment variable PATH.

Go to the place where you want to store the project files and run

revel new myapp

The entire project framework is established. As you can see from the folder structure below, revel is an MVC framework.

Go language uses revel framework to implement user registration tutorial (with code)

#The entire project is now ready to run. Run the following command line to start the site.

revel run myapp

Open the browser http://127.0.0.1:9000, you can see the following results

Go language uses revel framework to implement user registration tutorial (with code)

I won’t go into details about the internal details for the time being. Come on, let users register first. Note that most of the time there is no need to restart revel during the entire development process.

1. Prepare the Model

According to the development rhythm of MVC, we first prepare the model. Create a new models directory in the app directory, then create a new entity.go in it (you can name this file as you wish), open entity.go and add the User's entity definition.

type User struct {
  Email    string
  Nickname string
  Password []byte
}
type MockUser struct {
  Email           string
  Nickname        string
  Password        string
  ConfirmPassword string
}

I won’t go into details about the internal details for the time being. Come on, let users register first. Note that most of the time there is no need to restart revel during the entire development process.

1. Prepare the Model

According to the development rhythm of MVC, we first prepare the model. Create a new models directory in the app directory, then create a new entity.go in it (you can name this file as you wish), open entity.go and add the User's entity definition.

type User struct {
  Email    string
  Nickname string
  Password []byte
}
type MockUser struct {
  Email           string
  Nickname        string
  Password        string
  ConfirmPassword string
}

Why define MockUser? The reasons will be mentioned later.

Now write dal (data access layer) and create a new dal.go in the app\models directory. The writing method of dal can actually use the plug-in mechanism of revel. In order to avoid introducing too many concepts at once, this simple method is used first.

package models
import (
  "github.com/robfig/revel"
  "labix.org/v2/mgo"
)
const (
  DbName                         = "myapp"
  UserCollection                 = "user"
)
type Dal struct {
  session *mgo.Session
}
func NewDal() (*Dal, error) {
  revel.Config.SetSection("db")
  ip, found := revel.Config.String("ip")
  if !found {
    revel.ERROR.Fatal("Cannot load database ip from app.conf")
  }
  session, err := mgo.Dial(ip)
  if err != nil {
    return nil, err
  }
  return &Dal{session}, nil
}
func (d *Dal) Close() {
  d.session.Close()
}

revel has provided a configuration system, open conf\app.conf, add the following content

[db]
ip = 127.0.0.1

Now implement the method needed for registration, add the file dal_account in the app\models directory. go, the code is as follows.

func (d *Dal) RegisterUser(mu *MockUser) error {
  uc := d.session.DB(DbName).C(UserCollection)
  //先检查email和nickname是否已经被使用
  i, _ := uc.Find(M{"nickname": mu.Nickname}).Count()
  if i != 0 {
    return errors.New("用户昵称已经被使用")
  }
  i, _ = uc.Find(M{"email": mu.Email}).Count()
  if i != 0 {
    return errors.New("邮件地址已经被使用")
  }
  var u User
  u.Email = mu.Email
  u.Nickname = mu.Nickname
  u.Password, _ = bcrypt.GenerateFromPassword([]byte(mu.Password), bcrypt.DefaultCost)
  err := uc.Insert(u)
  return err
}

Do you see the meaning of MockUser’s existence? The user fills in the clear text password on the page, which cannot be directly stored in the database. It needs to be encrypted first. The "code.google.com/p/go.crypto/bcrypt" library is used here.

2. Prepare Controller

Prepare controller, create a new file account.go in app\controllers, and implement the Account controller in it. The code is as follows.

package controllers
import (
  "github.com/robfig/revel"
  "myapp/app/models"
)
type Account struct {
  *revel.Controller
}
func (c *Account) Register() revel.Result {
  return c.Render()
}
func (c *Account) PostRegister(user *models.MockUser) revel.Result {
  return c.Render()
}

3. Add Route

Prepare route, open conf\routes, and add Register URL mapping.

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
module:testrunner
GET     /                                          App.Index
GET     /register                                Account.Register
POST   /register                                Account.PostRegister
# Ignore favicon requests
GET     /favicon.ico                           404
# Map static resources from the /app/public folder to the /public path
GET     /public/*filepath                     Static.Serve("public")
# Catch all
*       /:controller/:action                   :controller.:action

Assuming everyone knows what Restful means, here is mapping the two URLs to the two Actions of the Controller.

As you can see, the mapping between all URLs and Controllers is defined here, which is very convenient. This file will be converted by revel into the app\routes\routes.go file before running to participate in compilation. The contents of this file will be used later when talking about ReverseRedirect.

4. Prepare View

Prepare view, create a new file Register.html under app\views, the key content is as follows

<form action="{{url "Account.PostRegister"}}" method="POST">
    {{with $field := field "user.Email" .}}
    <div class="control-group {{$field.ErrorClass}}">
      <label class="control-label" for="{{$field.Id}}">电子邮件</label>
      <div class="controls">
        <input type="email" id="{{$field.Id}}" name="{{$field.Name}}" value="{{$field.Flash}}" required>
        {{if $field.Error}}
        <span class="help-inline">{{$field.Error}}</span>
        {{end}}
      </div>
    </div>
    {{end}}
    …

Explain the above blue part bit by bit The meaning of the keyword.

url是revel提供的一个template function,可以很方便的把Controller的Action变成与之相对的url,它的运作原理实际上就是去刚才定义好的routes映射里面查找。

field是revel提供的一个template function,专门方便生成form,还记得PostRegister方法的签名吗?

func (c *Account) PostRegister(user *models.MockUser) revel.Result

它接受一个名为user的*models.User类型的参数,所以,使用{{with $field := field “user.Email”}}就可以通知revel将form的参数封装到user结构中再传递给PostRegister。

我们都知道用户注册的时候填写的值是需要做有效性检验的,当用户填写的值不符合标准时需要出现错误提示,通常来说会是下面这样

Go language uses revel framework to implement user registration tutorial (with code)

$field.ErrorClass的作用就是当这个参数出现错误的时候可以方便的通过添加class的方式在页面上显示错误状态。ErrorClass的值可以通过下面的代码修改。

revel.ERROR_CLASS = "error"

$field.Id和$field.Name就不解释了,大家待会儿打开浏览器中看看生成的源代码就明白了。

$field.Flash这里就需要解释一下Flash的概念了。

Flash是一个字典,适用于在两个Request中间传递数据,数据存储在cookie中。

大家都知道,HTTP协议本身是无状态的,所以,考虑一下这个用例,用户在注册的时候输入了一个无效的email地址,点击注册之后页面刷新了一下,“电子邮件”下面出现一行红字“你输入的Email地址无效”,此刻文本框里面需要出现上次用户输入的值。那么,$field.Flash就是在Flash里去找以$field.Name为Key的值。

$field.Error就是在Flash里找以$field.Name_error为Key的值,也就是上图中红色的“密码必须大于等于6位”这个错误信息。

好了,现在大家就按照这个节奏在view中添加“昵称”,“密码”和“确认密码”吧。

添加完成之后就去访问http://127.0.0.1/register看看吧。是不是这样呢?

Go language uses revel framework to implement user registration tutorial (with code)

revel会通过Controller.Action的名称去查找同名的view文件,例如,Register方法对应的就是Register.html。这里需要注意的一点是,revel是通过反射去查找Controller.Render方法的调用者,而且只向上查找一层。

例如,下面这段代码是不能工作的。

func (c *Account) someMethod() revel.Result {
  ...
  return c.Render()
}
func (c *Account) Register() revel.Result {
  return c.someMethod()
}

5. 实现Controller

现在让我们为PostRegister添加处理注册的逻辑。

首先,验证参数的有效性。

func (c *Account) PostRegister(user *models.MockUser) revel.Result {
  c.Validation.Required(user)
  c.Validation.Email(user.Email)
  c.Validation.Required(user.Nickname)
  c.Validation.Required(user.Password)
  c.Validation.Required(user.ConfirmPassword == user.Password)
  if c.Validation.HasErrors() {
    c.FlashParams()
    return c.Redirect((*Account).Register)
  }
  return c.Render()
}

revel提供了挺好用的Validation机制,上面的代码应该不需要太多解释,只有一行

c.FlashParams()

它的作用就是把form提交的参数原样存入Flash中,还记得刚才的$field.Flash吗?

现在去玩玩注册页面吧,填写一些错误的值看看反应吧,嗯,你应该很快就会发现,错误信息虽然已经显示出来,但可惜却是英文的,修改一下吧。

func (c *Account) PostRegister(user *models.MockUser) revel.Result {
  c.Validation.Email(user.Email).Message("电子邮件格式无效")
  c.Validation.Required(user.Nickname).Message("用户昵称不能为空")
  c.Validation.Required(user.Password).Message("密码不能为空")
  c.Validation.Required(user.ConfirmPassword == user.Password).Message("两次输入的密码不一致")
  if c.Validation.HasErrors() {
    c.FlashParams()
    return c.Redirect((*Account).Register)
  }
  return c.Render()
}

Validation提供了好几个常用的验证方法,大家可以自己看看,应该是简单易懂的。

继续,当所有参数检查都通过之后,就调用dal.RegisterUser方法将用户信息存入数据库。

func (c *Account) PostRegister(user *models.MockUser) revel.Result {
  c.Validation.Email(user.Email).Message("电子邮件格式无效")
  c.Validation.Required(user.Nickname).Message("用户昵称不能为空")
  c.Validation.Required(user.Password).Message("密码不能为空")
  c.Validation.Required(user.ConfirmPassword == user.Password).Message("两次输入的密码不一致")
  if c.Validation.HasErrors() {
    c.FlashParams()
    return c.Redirect((*Account).Register)
  }
  dal, err := models.NewDal()
  if err != nil {
    c.Response.Status = 500
    return c.RenderError(err)
  }
  defer dal.Close()
  err = dal.RegisterUser(user)
  if err != nil {
    c.Flash.Error(err.Error())
    return c.Redirect((*Account).Register)
  }
  return c.Redirect((*Account).RegisterSuccessful)
}
func (c *Account) RegisterSuccessful() revel.Result {
  return c.Render()
}

我增加了一个方法RegisterSuccessful,用于显示注册成功,大家别忘了在routes和view中添加相应的东西。

至此,用户注册已经完成。不知道大家注意到没有,就算修改go代码,依然不需要重新启动revel,直接刷新浏览器页面就会发现新的代码已经自动编译并且启用了。

更多go语言相关文章请关注go语言教程栏目。

The above is the detailed content of Go language uses revel framework to implement user registration tutorial (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete