storage.go 601 B

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package storage
  5. import (
  6. "database/sql"
  7. "log"
  8. _ "github.com/lib/pq"
  9. )
  10. type Storage struct {
  11. db *sql.DB
  12. }
  13. func (s *Storage) Close() {
  14. s.db.Close()
  15. }
  16. func NewStorage(databaseUrl string, maxOpenConns int) *Storage {
  17. db, err := sql.Open("postgres", databaseUrl)
  18. if err != nil {
  19. log.Fatalf("Unable to connect to the database: %v", err)
  20. }
  21. db.SetMaxOpenConns(maxOpenConns)
  22. db.SetMaxIdleConns(2)
  23. return &Storage{db: db}
  24. }