Home>Article>Backend Development> 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 ofGo languageand 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.
#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
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