func newDbForTest(t *testing.T) *sqlx.DB { var err error pguser, _, 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/$GO_BOOTSTRAP_PROJECT_NAME-test?sslmode=%v", pguser, pghost, pgport, pgsslmode)) if err != nil { t.Fatalf("Connecting to local postgres should never fail. Error: %v", err) } return db }
func newConfig() (*viper.Viper, error) { u, err := libunix.CurrentUser() if err != nil { return nil, err } c := viper.New() c.SetDefault("dsn", fmt.Sprintf("postgres://%v@localhost:5432/$GO_BOOTSTRAP_PROJECT_NAME?sslmode=disable", u)) c.SetDefault("cookie_secret", "$GO_BOOTSTRAP_COOKIE_SECRET") c.SetDefault("http_addr", ":8888") c.SetDefault("http_cert_file", "") c.SetDefault("http_key_file", "") c.SetDefault("http_drain_interval", "1s") c.AutomaticEnv() return c, nil }
// 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/$GO_BOOTSTRAP_PROJECT_NAME?sslmode=disable", u)) db, err := sqlx.Connect("postgres", dsn) if err != nil { return nil, err } cookieStoreSecret := libenv.EnvWithDefault("COOKIE_SECRET", "$GO_BOOTSTRAP_COOKIE_SECRET") app := &Application{} app.dsn = dsn app.db = db app.cookieStore = sessions.NewCookieStore([]byte(cookieStoreSecret)) return app, err }