func newDbForTest(t *testing.T) *sqlx.DB { var err error pguser, pgpass, pghost, pgport, pgsslmode := os.Getenv("PGUSER"), os.Getenv("PGPASSWORD"), os.Getenv("PGHOST"), os.Getenv("PGPORT"), os.Getenv("PGSSLMODE") if pguser == "" { pguser, err = libunix.CurrentUser() if err != nil { t.Fatalf("Getting current user should never fail. Error: %v", err) } } if pghost == "" { pghost = "localhost" } if pgport == "" { pgport = "5432" } if pgsslmode == "" { pgsslmode = "disable" } db, err := sqlx.Connect("postgres", fmt.Sprintf("postgres://%v@%v:%v/fpc-test?sslmode=%v&password=%v", pguser, pghost, pgport, pgsslmode, pgpass)) if err != nil { t.Fatalf("Connecting to local postgres should never fail. Error: %v", err) } return db }
// NewApplication is the constructor for Application struct. func NewApplication() (*Application, error) { u, err := libunix.CurrentUser() if err != nil { return nil, err } dsn := libenv.EnvWithDefault("DSN", fmt.Sprintf("postgres://%v@localhost:5432/fpc?sslmode=disable", u)) db, err := sqlx.Connect("postgres", dsn) if err != nil { return nil, err } cookieStoreSecret := libenv.EnvWithDefault("COOKIE_SECRET", "qhXi3KCGubTYzcMQ") app := &Application{} app.dsn = dsn app.db = db app.cookieStore = sessions.NewCookieStore([]byte(cookieStoreSecret)) return app, err }