Use Revel (GO) to develop a website

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

Revel a good use of the go language of the goroutine, each request is assigned to the Goroutine. No need to write a lot of callbacks. If you have written Nodejs, you will have a deep understanding of what callback hell is like. It is because Revel has the Goroutine,revel performance also has the very big promotion. The official website claims that the throughput is 3 to 10 times times that of rails. Revel has a good performance and provides a very comprehensive tool for development. Commonly used routes, templates, sessions, parameter resolution, and so on almost all, and even provide a test framework. And the development of these things is modular, that is, you can use their own implementation to replace the above mentioned tools. For example, replace the default route with a router that you think is appropriate to live your own development.

The default here is that you've configured go env. In one of the directories specified by Gopath, run the commandgo get github.com/revel/revel之后再运行go get github.com/revel/cmd/revel。好的这个时候你会报错说无法import到websocket这个包。感谢伟大的全国防火墙,这部分的代码被屏蔽了。虽然直接下载不行,但是代码在github上还是有的。所以,还是可以手动下载,之后配置上的。

Here's another great tool to know about go: http://gopm.io/. This is a nodejs-like NPM tool. He will download the corresponding go package according to the version number, not git, Hg or anything. You can download this tool or click Download to go to the download page (here: http://gopm.io/download). Then enter golang.org/x/netin the import path and click Download. The package is downloaded.

After the download is successful, unzip the download package:

It's a big bag. Finally, we need the WebSocket package. We are going to put all of these packages in the required directory. This time back to look at the error content:golang.org/x/net/websocket. But in our Gopath/src directory there is no such directory. So we need to create all the directories before websocket. Then copy the entire contents of the extract from the past. This problem can be solved. Finally, build the revel manually. Execute command: Go build github.com/revel/cmd/revel. The Revel binary executable file is then generated in the Bin directory. The installation of Revel here is complete.

After the installation is complete, the following prompt appears when you run the Revel command:

Localhost:golang user$ revel~~ revel! Http://revel.github.io~usage:revel command [arguments]the commands is:    new         Create a skeleton revel Application    Run         run a Revel application    build       Build a Revel application (e.g. for deployment)    Package     a Revel application (e.g. for deployment), clean clean       A Revel application ' s temp files
  test        Run all tests from the Command-lineuse ' Revel Help [command] ' for more information.

Run the Revel New command, and Revel will generate a default site for you. The command is as follows:Revel New Github.com/first_test/app. This site should be for testing purposes only, and I want to build more other sites, so the directory here is slightly more complicated. You can decide the directory according to your own needs. The general directory structure of this type:

Run the command RevelRun, for example:Revel run Github.com/first_test/app. This time a site has been running. Enter localhost:9000 in the browser, carriage return. You will see:

The installation and configuration are now complete.

Before development, meet the directory structure that Revel generated for us:

App                   project root ├──app               MVC Framework Directory │   ├──controllers   Controller Catalog │   ├──init.go│   ├──models        Model Catalog │   ├──routes│   ├──tmp│   └──views         View directory ├──conf│   ├──app.conf      configuration file │   └──routes        Routing file ├──messages          International directory ├──public            static file directory └──tests

It looks so complicated. In fact, for developers, the most commonly used is not so much. The simplest public, the directory below is where CSS, IMG and JS files are stored. What we use most is app/conf/routes and app/app/controllers.

Now let's add a route to the App/conf/routes file get/blogs blogs.blogs

Save. Create the Blogs directory under the Views directory and create the blogs.html file under it. This is only used as an example, so the blogs.html file was copied from index.html and changed to a name.

Create our own Controller:blogs.go file under Controllers, with the following code:

"Github.com/revel/revel"type Blogs struct {    *Revel. Controller}func (c Blogs) Blogs () Revel. Result {    return  c.render ()}

Now restart the service, enter Localhost:9000/blogs in the browser and the content of the index page will appear at the beginning. The content of index appears here, as mentioned earlier, because we have directly copied the contents of the index page.

This shows that the development on the Revel is generally the following three steps:

    1. Create the route that we need.
    2. Create a related view attempt.
    3. Create a controller.

By completing the above three steps, we have added what we need. The use of other content can be referred to the official website of the document.

There is one more thing to note. Look first:

We need to be aware of the last line in the file where we add the route. */:controller/:action:controller.:action

* Indicates the method of the network request:GET, POST, delete, etc. , the following /:controller/:action is the path of the network request , and finally : Controller.:action is a user-defined controller and the actionin it. So the content accessed by this path can be accessed if both the controller and the action are present. That is, if we do not add any routes, only this default generated route can also be accessed to blogs. You can try to access localhost:9000/blogs/blogsin the browser. You can also display the blogs.html that we added. The first blogs of this path is the controller's name (when the first letter of the controller is capitalized). One of the following blogs corresponds to the Controller's action, which is the blogs method of the blogs structure.

How the controller's data can be passed to the template. Modify the blogs.html in the

< H1 > It works! </ H1 >

For

< H1 > {{. Greeting}} </ H1 >

To modify the blogs code:

func (c Blogs) Blogs () Revel. Result {    var" Hello revel! "    return C.render (greeting)}

Don't restart the service this time. Modifications to routing, controller, and view do not have to restart the service. Revel supports Hot-reload. The modified content will be automatically recompiled. Refreshes the browser.

Start your Revel Journey!

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.