Introduction of golang's web framework, revel

There are various web application frameworks in golang. Which among them is good is all that they are keenly developing, and new ones are coming out so I can not say anything.

Among them, revel that the framework is there from sintering構昔. This is a heavyweight framework that is comprehensive enough to compare with Rails.

If it is a thin framework, I feel like I can use the standard net / http, so I am currently using this revel to actually make it.

In addition, I have referred to the following pages.

Characteristic

Although there are various features, I will introduce the good part of revel which is not mentioned in the above site here.

  • Deployment zip file is completed with one command
  • I18n compatible
  • You can put hooks in various places
  • You can specify an execution environment such as dev / prod. Moreover, it is easy to write config

In addition to redis also session management and job execution and are also compatible with validation, I have a lot such as WebSocket support, there is マニュアル get a look at the I think whether it is easier to understand.

Deployment zip file is completed with one command

revel package When you run the command, you or tar.gz file is finished. When this is sent to the server and deployed,

.
|-- run.bat
|-- run.sh
|-- sample-app
|-- sample-app.log
|-- sample-app.tar.gz
`-- src
    `-- github.com
        |-- revel
        `-- tsukinowasha
             `-- sample-app

The file will be expanded to feel like. After the run.sh if you run, you can run immediately.

By the way, it is only revel and the application below src. Other than that is not included. Below revel there are conf files, template files, etc., not including binary and go files. All of the following applications, including go files, are included. Since revel builds at run time, I think that this is useless.

bindata might be better methods that package to one of the binary, where you think the problem Ikana in this method is. However, revel package because the system is also included source code, I think it is better to use the bindata if that bad.

I18n compatible

message/labes.ja and message/labes.ja to prepare the file you ECTS called, will return the message according to the language setting of the browser.

The messages.ja file is as follows. (It does not matter if it is unique even if it is not separated by.)

mode.dev.label=検証系
login.title=ログイン

In this way, with the template

<h3>{{msg . "login.title" }}</h3>

If you write, etc, you will see the contents of messages.ja and messages.en depending on the language.

Hook

revel is Interceptor that, there is a Hook that Shikome to various timing in the request.

For example, if you do the following, the checkUser function runs before processing the request.

func checkUser(c *revel.Controller) revel.Result {
    if user := connected(c); user == nil {
       c.Flash.Error("Please log in first")
       return c.Redirect(App.Index)
    }
  return nil
}

func init() {
   revel.InterceptFunc(checkUser, revel.BEFORE, &Users{})
}

revel.BEFORE is specified timing. Others include PANIC which is called at AFTER and panic.

Using this makes it possible to share the description and it is useful.

Execution environment control

revel run When you run the command dev rise in the mode, the log displays the hot reloading or become details or enabled, debug display will be in and out.

This area is conf/app.conf It's in the written.

[dev]
mode.dev=true
results.pretty=true
watch=true

db.host = localhost

[prod]
mode.dev=false
results.pretty=false
watch=false

db.host = prod-db.example.com

In this way, you can select execution environment by sectioning by section. [staging] You can also be added, and the like. This can change the behavior.

db.host part that is determined by adding your own. From within code

dbhost := revel.Config.StringDefault("db.host", "localhost")

As you can get it, you can say that if you use this you will connect to a different host for each environment. (In the above example, default is set to localhost)

By the way, the revel package When you create a package at the command, and the script has been created so as to rise in the prod environment. A little care is convenient.

Custom Functions in templates

Like this in the revel.TemplateFuncs Once you have defined the function to, you can use in the template.

func init() {
             revel.TemplateFuncs["eq"] = func(a, b interface{})
             bool { return a == b }
}

I am addicted to it

Lastly I will introduce you what I am into when using revel. Although there are only about one at the moment. I'll update it.

Init timing

When I tried to rewrite variables in package at startup, I did not change it properly. It was changed properly by specifying it with OnAppStart.

var DB = "localhost"

func init() {
   // これは期待通りに動かない
   // DB := revel.Config.StringDefault("db.host", "localhost")
   revel.OnAppStart(changeDBHostFunc)  // 関数を登録
}

Summary

In this article I introduced various functions of revel. Is not it quite convenient?

Revel is only called heavyweight and has abundant functions. For me, learning costs are not very high, I can start using it quite easily. Then, you do not have to implement the function yourself or add it later, you can develop it comfortably.

It should be noted that, as of the same position is beego There is, but this has not yet been tried.