"Go Web Development Revel+mgo" The 8th chapter realizes the email wall

Source: Internet
Author: User
Tags md5 encryption
This is a creation in Article, where the information may have evolved or changed.

Demo Address http://gblog-revel.herokuapp.com/


1. Design the email Wall page

Ah, what is the email wall ah, this, Bo Master, casually pull something, do not know to everyone West like, first to do, look at the effect behind it.
First create the emails.html content under Views/app:

{{set.} "title" "Email List-gblog"}}{{set. "EMA" "Active"}}{{template "header.html".}} <div class= "Content" >    <div class= "Email-nav" >      <span class= "Email-tag" >                < span>jov123@163.com</span>       </span>    </div>  </div>  </div>  </body>
Among them, the URL of the IMG in the string, is the blogger's own email encrypted string. We'll teach you how to do this later.
In the Controllers/app.go Join method:
Func (c App) Emails () Revel. Result {return C.render ()}

Conf/routes Add route:
GET     /email                               app.emails

OK, look at the effect:


OK, now come to realize it.

2. Implementing the email Wall

First create the Email.go content under models:
Package Modelsimport ("Github.com/revel/revel" "Labix.org/v2/mgo/bson" "Time" "Crypto/md5" "io" "FMT") type Emailobj Struct{email Stringimgurl stringcdate time. Time}func (DAO *dao) insertemail (emailobj *emailobj) Error {emailcollection: = Dao.session.DB (DbName). C (emailcollection) emailobj.cdate = time. Now (); h: = MD5. New () Io. WriteString (H, emailobj.email)  Emailobj.imgurl = Fmt. Sprintf ("%x", H.sum (nil))    FMT. Println (emailobj) _,err: = Emailcollection.upsert (Bson. m{"Email": emailobj.email}, Emailobj) if err! = Nil {Revel. WARN. Printf ("Unable to save Emailobj:%v error%v", Emailobj, Err)}return Err}func (dao *dao) findallemails () []EMAILOBJ{EMAILC Ollection: = Dao.session.DB (DbName). C (emailcollection) Emails: = []emailobj{}query: = Emailcollection.find (Bson. m{}). Sort ("-cdate") query. All (&emails) return emails}

Take a look, insert method, inside we have made MD5 encryption for the URL, this is go comes with the thing, only need to introduce can, I this come simple use a bit. In addition, our Upsert method of MgO, email-based, if there is this email, we will only do updates, if no one, do insert, this strategy is very good.
OK, in the emails method of controllers/app.go add:
Func (c App) Emails () Revel. Result {DAO, err: = models. Newdao () if err! = Nil {c.response.status = 500return c.rendererror (err)}defer DAO. Close () Emails: =dao. Findallemails (); return C.render (emails)}

The views/app/emails.html was last modified to:
{{set.} "title" "Email List-gblog"}}{{set. "EMA" "Active"}}{{template "header.html".}} <div class= "Content" >    <div class= "Email-nav" >      {{if. Emails}}       {{range $email: =. Emails}}       <span class= "Email-tag" >                <span title=" last update at:{{$email. Cdate.format "2006-01-02 15:04"} ">{{$email. Email}}</span>       </span>       {{end}}       {{end}}    </div>  </div>  </ Div>  </body>

Well, have you finished? Of course no, we did the reading of the data here, but did not insert, how to do it, OK, remember our controllers under the class with W, this is our entrance. First, in Wblog.go's Putup method, add the content before return:
Newemail: = new (models. Emailobj); newemail.email = blog. Email;dao. Insertemail (Newemail);

Also in Wcomment.go's Docomment method, add content before return:
Newemail: = new (models. Emailobj); newemail.email = comment. Email;dao. Insertemail (Newemail);

Also in Wmessage.go's Putup method, add content before return:
Newemail: = new (models. Emailobj); newemail.email = message. Email;dao. Insertemail (Newemail);

OK, so, whether it is to send blog or comment, or message will add email for us. Take a look at the final effect:

Nice, are you ready?
Source Address:Https://github.com/joveth/GBlog

AC qq:158325682



Related Article

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.