storage.go 812 B

123456789101112131415161718192021222324252627282930313233343536
  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. // Postgresql driver import
  8. _ "github.com/lib/pq"
  9. "github.com/miniflux/miniflux/logger"
  10. )
  11. // Storage handles all operations related to the database.
  12. type Storage struct {
  13. db *sql.DB
  14. }
  15. // Close closes all database connections.
  16. func (s *Storage) Close() {
  17. s.db.Close()
  18. }
  19. // NewStorage returns a new Storage.
  20. func NewStorage(databaseURL string, maxOpenConns int) *Storage {
  21. db, err := sql.Open("postgres", databaseURL)
  22. if err != nil {
  23. logger.Fatal("[Storage] Unable to connect to the database: %v", err)
  24. }
  25. db.SetMaxOpenConns(maxOpenConns)
  26. db.SetMaxIdleConns(2)
  27. return &Storage{db: db}
  28. }