Created
April 23, 2015 15:57
-
-
Save arbarlow/5e88c2e696b6d222904b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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