go-simple-news-aggregator
The app demonstrates:
- Using an SQL (SQLite) database and configuring the Revel DB module.
- Using the third party GORP ORM-ish library
- Interceptors for checking that an user is logged in.
- Using validation and displaying inline errors
Database Install and Setup
This example used sqlite, (Alternatively can use mysql, postgres, etc.)
sqlite Installation
- The article app uses go-sqlite3 database driver, which depends on the C library
Install sqlite on OSX:
- Install Homebrew if you don't already have it.
- Install pkg-config and sqlite3:
$ brew install pkgconfig sqlite3
Install sqlite on Ubuntu:
$ sudo apt-get install sqlite3 libsqlite3-dev
Once SQLite is installed, it will be possible to run the article app:
$ revel run github.com/PROger4ever/go-simple-news-aggregator
Database / Gorp Plugin
app/controllers/gorp.go
defines GorpPlugin
, which is a plugin that does a couple things:
OnAppStart
- Uses the DB module to open a SQLite in-memory database, create the User
, Article
, and Source
tables, and insert some test records.
- BeforeRequest - Begins a transaction and stores the Transaction on the Controller
- AfterRequest - Commits the transaction, or panics if there was an error.
- OnException - Rolls back the transaction
Interceptors
app/controllers/init.go
registers the interceptors that runs before each action:
func init() {
revel.OnAppStart(Init)
revel.InterceptMethod((*GorpController).Begin, revel.BEFORE)
revel.InterceptMethod(Application.AddUser, revel.BEFORE)
revel.InterceptMethod(Sources.checkUser, revel.BEFORE)
revel.InterceptMethod((*GorpController).Commit, revel.AFTER)
revel.InterceptMethod((*GorpController).Rollback, revel.FINALLY)
}
As an example, checkUser
looks up the username in the session
and redirect
s
the user to log in if they do not have a session
cookie.
func (c Sources) checkUser() revel.Result {
if user := c.connected(); user == nil {
c.Flash.Error("Please log in first")
return c.Redirect(Application.Index)
}
return nil
}
Validation
The article app does quite a bit of validation.
Revel applies the validation and records errors using the name of the
validated variable (unless overridden).
The field
template helper looks for errors in the validation context, using
the field name as the key.