forked from revel/revel.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
573 lines (514 loc) · 20.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
---
root: .
title: The Revel Web Framework for Go
---
<!DOCTYPE html>
<html lang="en">
<head>
{% include head.html %}
<link href="{{ page.root }}/css/prettify.css" type="text/css" rel="stylesheet" />
<script src="{{ page.root }}/js/prettify.js" type="text/javascript"></script>
<script src="{{ page.root }}/js/lang-go-rich.js" type="text/javascript"></script>
{% include analytics.html %}
</head>
<body onload="prettyPrint()">
<a href="https://github.com/revel/revel">
<img style="position: absolute; top: 0; right: 0; border: 0; z-index:1000;"
src="img/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
{% include topnav.html %}
<header class="hero-unit">
<div class="container">
<div class="row" style="margin-left:-50px;">
<div class="hero-img"><img src="img/RevelWhiteLines.png" height="500" width="350"></div>
<div class="hero-text">
<h1>Revel</h1>
<p>A high-productivity web framework for the Go language.</p>
</div>
</div>
</div>
</header>
<div class="container">
<div class="page-header">
<h1>Features</h1>
</div>
<div class="row">
<div class="span4">
<h2>Hot Code Reload</h2>
<p>
Edit, save, and refresh. Revel compiles your code and templates for
you, so you don't miss a beat. Code doesn't compile? It gives you a
<a href="img/CompilationError.png">helpful description</a>.
Run-time code panic? Revel <a href="img/Panic.png">has you
covered</a>.
</p>
</div>
<div class="span4">
<h2>Comprehensive</h2>
<p>
Revel provides
<a href="manual/routing.html">routing</a>,
<a href="manual/binding.html">parameter parsing</a>,
<a href="manual/validation.html">validation</a>,
<a href="manual/sessionflash.html">session/flash</a>,
<a href="manual/templates.html">templating</a>,
<a href="manual/cache.html">caching</a>,
<a href="manual/jobs.html">job running</a>,
<a href="manual/testing.html">a testing framework</a>,
and even <a href="manual/i18n-messages.html">internationalization</a>.
</p>
</div>
<div class="span4">
<h2>High Performance</h2>
<p>
Revel builds on top of the Go HTTP server, which was
<a href="http://www.techempower.com/benchmarks/#section=data-r8">recently
benchmarked</a> to serve <b>three to ten times</b> as many requests
as Rails across a variety of loads.
</p>
</div>
</div>
<div class="page-header">
<h1>Framework Design</h1>
</div>
<div class="row">
<div class="span4">
<h2>Synchronous</h2>
<p>
The <a href="http://golang.org/pkg/net/http/">Go HTTP server</a>
runs each request in its own
<a href="http://golang.org/doc/effective_go.html#goroutines">goroutine</a>.
Write simple callback-free code without guilt.
</p>
</div>
<div class="span4">
<h2>Stateless</h2>
<p>
Revel provides primitives that keep the web tier stateless for
predictable scaling. For example, session data is stored in the user
cookie, and the cache is backed by a memcached cluster.
</p>
</div>
<div class="span4">
<h2>Modular</h2>
<p>
Revel is built around composable middleware called <b>filters</b>,
which implement nearly all request-processing
functionality. Developers have the freedom to replace the default
filters with custom implementations (e.g. a custom router).
</p>
</div>
</div>
<div class="row">
</div>
<section id="quickstart">
<div class="page-header">
<h1>Quick Start</h1>
</div>
<div class="row">
<div class="span6">
<p>
Revel ships with sample applications to provide examples of
real-world usage.
</p>
<p>
You will need a <a href="http://golang.org/doc/install">functioning Go 1.2
installation</a> for this to work.
</p>
<p>
The commands at right will:
<ol>
<li>Install Revel into your GOPATH
<li>Build the Revel command-line tool
<li>Run the Chat sample application
</ol>
Once it's running, open a browser to
<a href="http://localhost:9000/">http://localhost:9000/</a>, and
<a href="samples/chat.html">read how the chat demo is implemented</a>.
</p>
</div>
<div class="span6" style="vertical-align:bottom;">
<pre>
go get github.com/revel/cmd/revel
revel run github.com/revel/revel/samples/chat
</pre>
</div>
</div>
</section>
<section id="development">
<div class="page-header">
<h1>Development Status <small>Early adopters only. Pull requests welcome.</small></h1>
</div>
<p>
Development is closing in on the "final" 1.0 design, but the rate of
change is still high. Expect to get your hands dirty.
</p>
<p style="font-weight:bold;">
Join our <a href="https://groups.google.com/forum/#!forum/revel-framework">Google Group</a>
to take part in the design and development, or in IRC at
<a href="http://webchat.freenode.net/?channels=revel&uio=d4">Freenode #revel</a>.
You may join <a href="https://groups.google.com/group/revel-framework-announce">our
announcement list</a> to only be notified for new releases.
</p>
<p>
<a href="https://twitter.com/revelframework" class="twitter-follow-button" data-show-count="false" data-size="large">Follow @revelframework</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<a href="https://twitter.com/share" class="twitter-share-button" data-via="revelframework" data-size="large" data-count="none" data-hashtags="golang">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</p>
</section>
<section id="teaser">
<div class="page-header">
<h1>Overview</h1>
</div>
<p>
This section gives you a taste of various parts of the framework:
</p>
<dl class="dl-horizontal">
<dt><a href="#routing">Routing</a></dt>
<dd>A simple declarative routing syntax. Type-safe reverse routing.</dd>
<dt><a href="#controllers">Controllers</a></dt>
<dd>
Revel organizes endpoints into Controllers. They provide easy
data binding and form validation.
</dd>
<dt><a href="#templates">Templates</a></dt>
<dd>Revel makes Go Templates simple to use at scale.</dd>
<dt><a href="#interceptors">Interceptors</a></dt>
<dd>
Register functionality to be called before or after actions. They
can be activated per Controller.
</dd>
<dt><a href="#filters">Filters</a></dt>
<dd>
More general functionality can be implemented with Filters.
</dd>
</dl>
<div class="row">
<div class="span6">
<h2 id="routing">Routing</h2>
<p>
Revel uses a declarative routing syntax. It collects all routes
for an app in a single file, with a simple syntax for matching
requests, extracting arguments from URIs, and specifying
route-specific arguments to the action. Here's a commented
sample...
</p>
</div>
<div class="span12">
<pre>
# conf/routes
# This file defines all application routes (Higher priority routes first)
GET /login Application.Login <b># A simple path</b>
GET /hotels/ Hotels.Index <b># Matches with or without trailing slash</b>
GET /hotels/:id Hotels.Show <b># Extract an embedded argument</b>
WS /hotels/:id/feed Hotels.Feed <b># WebSockets.</b>
POST /hotels/:id/:action Hotels.:action <b># Automatically route some actions.</b>
GET /public/*filepath Static.Serve("public") <b># Assets served from /public/...</b>
* /:controller/:action :controller.:action <b># Catch all; Automatic URL generation</b></pre>
</div>
<div class="span6">
<p>
Reverse routes can be generated in a type-safe manner. For example:
</p>
</div>
<div class="span12">
<pre class="prettyprint lang-go">
// Show the hotel information.
func (c Hotels) Show(id int) revel.Result {
hotel := HotelById(id)
return c.Render(hotel)
}
// Save the updated hotel information and redirect back to Show.
func (c Hotels) Save(hotel Hotel) revel.Result {
// validate and save hotel
return c.Redirect(<b>routes.Hotels.Show(hotel.Id)</b>)
}</pre>
</div>
</div>
<div class="row">
<div class="span6">
<h2 id="controllers">Controllers</h2>
<p>
All Actions are methods on a Controller. This teaser shows a couple cool things:
<ul>
<li> <b>Data binding.</b> Revel binds simple values and structs from
the URL or form and passes them as parameters to your method.
(If you prefer to access them directly from a parameter map, that's ok too!)
<li> <b>Validation.</b> Helpers to manage validation errors.
<li> <b>Flash.</b> The flash is a cookie that lives for one
request (errors, success messages, etc).
<li> <b>Session.</b> The session is a cryptographically signed
cookie, exposed as a <code>map[string]string</code>.
<li> <b>Results.</b> Redirections take advantage of reverse
routing. Template rendering makes your data available using the
name of the local variable!
</ul>
Here's an example:
</p>
</div>
<div class="span12">
<pre class="prettyprint lang-go">
// app/controllers/app.go
type Application struct {
*revel.Controller
}
func (c Application) Register() revel.Result {
title := "Register"
return c.Render(title)
}
func (c Application) SaveUser(user models.User, verifyPassword string) revel.Result {
c.Validation.Required(verifyPassword)
c.Validation.Required(verifyPassword == user.Password)
Message("Password does not match")
user.Validate(c.Validation)
if c.Validation.HasErrors() {
c.Validation.Keep()
c.FlashParams()
return c.Redirect(routes.Application.Register())
}
user.HashedPassword, _ = bcrypt.GenerateFromPassword(
[]byte(user.Password), bcrypt.DefaultCost)
err := c.Txn.Insert(&user)
if err != nil {
panic(err)
}
c.Session["user"] = user.Username
c.Flash.Success("Welcome, " + user.Name)
return c.Redirect(routes.Hotels.Index())
}
</pre>
</div>
</div>
<div class="row">
<div class="span6">
<h2 id="templates">Templates</h2>
<p>
By convention, Revel manages to integrate
<a href="http://golang.org/pkg/text/template/">Go Templates</a>
easily into the rest of the web app. Here is part of the template
rendered in the <b>Register</b> action shown above.
</p>
Note that:
<ul>
<li> Revel found it automatically using the name of the action.
<li> <b>field</b> is a simple helper function that returns a map
of validation errors and parameter values for the named field.
The app may inject any helper funcs that it wants.
<li> The <b>title</b> variable is available in the template as if
it had been explicitly put in the RenderArgs. (It's used in
<a href="http://github.com/revel/revel/samples/booking/app/views/header.html">
header.html</a> in this case)
</ul>
</div>
<div class="span12">
<pre class="prettyprint lang-html">{% capture teaser_template %}{% raw %}
{{/* app/views/Application/Register.html */}}
{{template "header.html" .}}
<h1>Register:</h1>
<form action="/register" method="POST">
{{with $field := field "user.Username" .}}
<p class="{{$field.ErrorClass}}">
<strong>Username:</strong>
<input type="text" name="{{$field.Name}}" size="16" value="{{$field.Flash}}"> *
<span class="error">{{$field.Error}}</span>
</p>
{{end}}
{{/* other fields */}}
<p class="buttons">
<input type="submit" value="Register"> <a href="/">Cancel</a>
</p>
</form>
{{template "footer.html" .}}{% endraw %}{% endcapture %}{{ teaser_template|escape }}</pre>
</div>
</div>
<div class="row">
<div class="span6">
<h2 id="interceptors">Interceptors</h2>
<p>
Interceptors are controller methods that are run before or after
requests, or in response to panics. By embedding a controller
into another, a developer can share interceptors and fields across
many controllers.
</p>
<p>
As an example, the database module may be used to open a
connection on initialization, made available through a global
handle. Additionally, embedding the <b>db.Transactional</b> type
adds a <b>sql.Txn</b> field plus interceptors that begin and commit
transactions (or rollback on panic).
</p>
<p>
Here's what the interceptor looks like (minus error handling):
</p>
</div>
<div class="span12">
<pre class="prettyprint">
// github.com/revel/revel/modules/db/app/db.go
var Db *sql.DB
func Init() {
// Read configuration.
Driver, _ = revel.Config.String("db.driver")
Spec, _ = revel.Config.String("db.spec")
// Open a connection.
Db, _ = sql.Open(Driver, Spec)
}
// Transactional adds transaction management to your controller.
type Transactional struct {
*revel.Controller
Txn *sql.Tx
}
func (c *Transactional) Begin() revel.Result {
c.Txn, _ = Db.Begin()
return nil
}
func (c *Transactional) Commit() revel.Result {
_ = c.Txn.Commit()
c.Txn = nil
return nil
}
func (c *Transactional) Rollback() revel.Result {
_ = c.Txn.Rollback()
c.Txn = nil
return nil
}
func init() {
revel.InterceptMethod((*Transactional).Begin, revel.BEFORE)
revel.InterceptMethod((*Transactional).Commit, revel.AFTER)
revel.InterceptMethod((*Transactional).Rollback, revel.PANIC)
}</pre>
<p>
Here is how it can be mixed in to an application controller:
</p>
<pre class="prettyprint">
type Bookings struct {
*revel.Controller
db.Transactional // Adds .Txn
user.Login // Adds .User
}
func (c Bookings) ShowFirstBooking() revel.Result {
row := c.Txn.QueryRow(`
select id, hotel_id, user_id, price, nights
from Booking
where UserId = ?
limit 1`, c.User.Id)
...
return c.Render(booking)
}</pre>
</div>
</div>
<div class="row">
<div class="span6">
<h2 id="filters">Filters</h2>
<p>
Filters are the middleware of the application. They are simply
funcs with a specific signature:
</p>
<pre class="prettyprint">
type Filter func(c *Controller, filterChain []Filter)</pre>
<p>
Even complicated "built-in" functionality like the interceptor
framework is implemented as a filter:
</p>
</div>
<div class="span12">
<pre class="prettyprint">
// github.com/revel/revel/intercept.go
var InterceptorFilter = func(c *Controller, fc []Filter) {
defer invokeInterceptors(FINALLY, c)
defer func() {
if err := recover(); err != nil {
invokeInterceptors(PANIC, c)
panic(err)
}
}()
// Invoke the BEFORE interceptors and return early, if we get a result.
invokeInterceptors(BEFORE, c)
if c.Result != nil {
return
}
fc[0](c, fc[1:])
invokeInterceptors(AFTER, c)
}</pre>
<p>
Revel provides a default stack of Filters which the developer can
override. This makes it easy for the developer to select exactly
the parts of the framework that they want to use.
</p>
<pre class="prettyprint">
// github.com/revel/revel/filter.go
// Filters is the default set of global filters.
// It may be set by the application on initialization.
var Filters = []Filter{
PanicFilter, // Recover from panics and display an error page instead.
RouterFilter, // Use the routing table to select the right Action
FilterConfiguringFilter, // A hook for adding or removing per-Action filters.
ParamsFilter, // Parse parameters into Controller.Params.
SessionFilter, // Restore and write the session cookie.
FlashFilter, // Restore and write the flash cookie.
ValidationFilter, // Restore kept validation errors and save new ones from cookie.
I18nFilter, // Resolve the requested language
InterceptorFilter, // Run interceptors around the action.
ActionInvoker, // Invoke the action.
}
</pre>
<p>
Nearly all framework functionality is implemented in the filters,
and the filter stack is directly exposed to the developer as part
of the configuration. This makes Revel understandable and modular.
</p>
<p>
As proof of modularity, look how simple
the <a href="https://github.com/revel/revel/blob/master/server.go">
main server handler</a> is:
</p>
<pre class="prettyprint">
// github.com/revel/revel/server.go
func handleInternal(w http.ResponseWriter, r *http.Request, ws *websocket.Conn) {
var (
req = NewRequest(r)
resp = NewResponse(w)
c = NewController(req, resp)
)
req.Websocket = ws
Filters[0](c, Filters[1:])
if c.Result != nil {
c.Result.Apply(req, resp)
}
}</pre>
</div>
</div>
</section>
<section id="wishlist">
<div class="page-header">
<h1>Wishlist</h1>
</div>
<p>
There are some areas that could benefit from some TLC.
</p>
<ul>
<li> <b>ORM</b> -- Presently Revel is BYOORM (bring-your-own-ORM). A
good ORM integration would make simple things simple.
(e.g. <a href="http://github.com/coopernurse/gorp">gorp</a>,
<a href="https://github.com/eaigner/hood">hood</a>,
<a href="https://github.com/coocood/qbs">qbs</a>,
<a href="https://github.com/eaigner/jet">jet</a>,
<a href="https://github.com/astaxie/beedb">beedb</a>,
<a href="https://github.com/gosexy/db">gosexy</a>,
<a href="https://github.com/jinzhu/gorm">gorm</a>)
<li> <b>Pluggable template loader</b> -- Presently only Go templates
are supported by Revel (although the developer could use their own
library independently). Providing an interface that makes any
template language pluggable would be ideal.
</ul>
</section>
<hr>
<footer>
<p>MIT License</p>
<p>
Gopher images remixed from those produced by
<a href="http://www.golang.org">Go team</a>.
</p>
</footer>
</div> <!-- /container -->
</body>
</html>