Calorie counting web application written in the Go language

init.go 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package app
  2. import (
  3. "strings"
  4. "github.com/revel/revel"
  5. )
  6. func init() {
  7. // Filters is the default set of global filters.
  8. revel.Filters = []revel.Filter{
  9. ContentTypeFilter,
  10. revel.PanicFilter, // Recover from panics and display an error page instead.
  11. revel.RouterFilter, // Use the routing table to select the right Action
  12. revel.FilterConfiguringFilter, // A hook for adding or removing per-Action filters.
  13. revel.ParamsFilter, // Parse parameters into Controller.Params.
  14. revel.SessionFilter, // Restore and write the session cookie.
  15. revel.FlashFilter, // Restore and write the flash cookie.
  16. revel.ValidationFilter, // Restore kept validation errors and save new ones from cookie.
  17. revel.I18nFilter, // Resolve the requested language
  18. HeaderFilter, // Add some security based headers
  19. revel.InterceptorFilter, // Run interceptors around the action.
  20. revel.CompressFilter, // Compress the result.
  21. revel.ActionInvoker, // Invoke the action.
  22. }
  23. // register startup functions with OnAppStart
  24. // ( order dependent )
  25. // revel.OnAppStart(InitDB())
  26. // revel.OnAppStart(FillCache())
  27. }
  28. // TODO turn this into revel.HeaderFilter
  29. // should probably also have a filter for CSRF
  30. // not sure if it can go in the same filter or not
  31. var HeaderFilter = func(c *revel.Controller, fc []revel.Filter) {
  32. // This was commented out so I could mask the origins with DNS
  33. //c.Response.Out.Header().Add("X-Frame-Options", "SAMEORIGIN")
  34. // Add some common security headers
  35. c.Response.Out.Header().Add("X-XSS-Protection", "1; mode=block")
  36. c.Response.Out.Header().Add("X-Content-Type-Options", "nosniff")
  37. fc[0](c, fc[1:]) // Execute the next filter stage.
  38. }
  39. var ContentTypeFilter = func(c *revel.Controller, fc []revel.Filter) {
  40. path := c.Request.Request.URL.Path
  41. formats := []string{"json", "xml"}
  42. for _, format := range formats {
  43. if strings.HasSuffix(path, "." + format) {
  44. trimmed := strings.TrimSuffix(path, "." + format)
  45. c.Request.Request.URL.Path = trimmed
  46. c.Request.Request.RequestURI = trimmed
  47. c.Request.Format = format
  48. break
  49. }
  50. }
  51. fc[0](c, fc[1:])
  52. }