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"
|
|
|
|
"log"
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
// Postgresql driver import
|
2017-11-20 06:10:04 +01:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
log.Fatalf("Unable to connect to the database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.SetMaxOpenConns(maxOpenConns)
|
|
|
|
db.SetMaxIdleConns(2)
|
|
|
|
|
|
|
|
return &Storage{db: db}
|
|
|
|
}
|