The Booking sample 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 a user is logged in.
- Using validation and displaying inline errors
booking/app/ models # Structs and validation. booking.go hotel.go user.go controllers init.go # Register all of the interceptors. gorp.go # A plugin for setting up Gorp, creating tables, and managing transactions. app.go # "Login" and "Register new user" pages hotels.go # Hotel searching and booking views ...
The booking app uses go-sqlite3 database driver (which wraps the native C library).
To install on OSX:
- Install Homebrew if you don’t already have it.
- Install pkg-config and sqlite3:
$ brew install pkgconfig sqlite3
To install on Ubuntu:
$ sudo apt-get install sqlite3 libsqlite3-dev
Once you have SQLite installed, it will be possible to run the booking app:
$ revel run github.com/revel/examples/booking
Database / Gorp Plugin
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, Booking, and Hotel tables, and insert some test records.
- BeforeRequest: Begins a transaction and stores the Transaction on the Controller
- AfterRequest: Commits the transaction. Panics if there was an error.
- OnException: Rolls back the transaction.
As an example,
checkUser looks up the username in the session and redirects
the user to log in if they are not already.
The booking app does quite a bit of validation.
For example, here is the routine to validate a booking, from models/booking.go:
Revel applies the validation and records errors using the name of the
validated variable (unless overridden). For example,
required; if it evaluates to the zero date, Revel stores a
the validation context under the key “booking.CheckInDate”.
field template helper looks for errors in the validation context, using
the field name as the key.