Skip to content

Instantly share code, notes, and snippets.

@arbarlow
Created April 23, 2015 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arbarlow/5e88c2e696b6d222904b to your computer and use it in GitHub Desktop.
Save arbarlow/5e88c2e696b6d222904b to your computer and use it in GitHub Desktop.
package app
import (
"github.com/go-martini/martini"
"github.com/jinzhu/gorm"
"github.com/kylelemons/go-gypsy/yaml"
_ "github.com/lib/pq"
"github.com/martini-contrib/render"
"github.com/martini-contrib/secure"
"github.com/martini-contrib/sessions"
"fmt"
"path/filepath"
"github.com/madebymany/riru/controllers"
)
func Setup() *martini.Martini {
m := martini.New()
m.Use(martini.Static("public"))
m.Use(martini.Recovery())
store := sessions.NewCookieStore([]byte("somerandomstring"))
m.Use(sessions.Sessions("riru_session", store))
m.Use(secure.Secure(secure.Options{
FrameDeny: true,
ContentTypeNosniff: true,
BrowserXssFilter: true,
}))
if martini.Env != martini.Test {
m.Use(martini.Logger())
}
m.Map(SetupDbConnection("./db/dbconf.yml"))
m.Use(render.Renderer())
r := martini.NewRouter()
r.Group("/reels",
controllers.ReelsControllerRouter,
controllers.AuthenticateUser)
r.Group("/", controllers.PageControllerRouter)
m.Action(r.Handle)
return m
}
func SetupDbConnection(confLocation string) gorm.DB {
driver, open := parseDbConf(confLocation)
db, err := gorm.Open(driver, open)
if err != nil {
panic(err)
}
if martini.Env != martini.Test {
db.LogMode(true)
}
return db
}
func parseDbConf(confLocation string) (string, string) {
filepath, _ := filepath.Abs(confLocation)
file, err := yaml.ReadFile(filepath)
if err != nil {
panic(err)
}
driver, err := file.Get(fmt.Sprintf("%s.driver", martini.Env))
if err != nil {
panic(err)
}
open, err := file.Get(fmt.Sprintf("%s.open", martini.Env))
if err != nil {
panic(err)
}
return driver, open
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment