miniflux-v2/storage/storage.go

37 lines
812 B
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package storage
import (
"database/sql"
2017-11-28 06:30:04 +01:00
// Postgresql driver import
2017-11-20 06:10:04 +01:00
_ "github.com/lib/pq"
2017-12-16 03:55:57 +01:00
"github.com/miniflux/miniflux/logger"
2017-11-20 06:10:04 +01:00
)
2017-11-28 06:30:04 +01:00
// Storage handles all operations related to the database.
2017-11-20 06:10:04 +01:00
type Storage struct {
db *sql.DB
}
2017-11-28 06:30:04 +01:00
// Close closes all database connections.
2017-11-20 06:10:04 +01:00
func (s *Storage) Close() {
s.db.Close()
}
2017-11-28 06:30:04 +01:00
// NewStorage returns a new Storage.
func NewStorage(databaseURL string, maxOpenConns int) *Storage {
db, err := sql.Open("postgres", databaseURL)
2017-11-20 06:10:04 +01:00
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Fatal("[Storage] Unable to connect to the database: %v", err)
2017-11-20 06:10:04 +01:00
}
db.SetMaxOpenConns(maxOpenConns)
db.SetMaxIdleConns(2)
return &Storage{db: db}
}