factor db creation to its own package
This commit is contained in:
@@ -2,14 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
|
||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
"github.com/peterbourgon/ff"
|
"github.com/peterbourgon/ff"
|
||||||
|
|
||||||
|
"github.com/sentriz/gonic/db"
|
||||||
"github.com/sentriz/gonic/server"
|
"github.com/sentriz/gonic/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,10 +42,7 @@ func main() {
|
|||||||
if _, err := os.Stat(*musicPath); os.IsNotExist(err) {
|
if _, err := os.Stat(*musicPath); os.IsNotExist(err) {
|
||||||
log.Fatal("please provide a valid music directory")
|
log.Fatal("please provide a valid music directory")
|
||||||
}
|
}
|
||||||
db, err := gorm.Open("sqlite3", fmt.Sprintf(
|
db, err := db.New(*dbPath)
|
||||||
"%s?cache=shared&_busy_timeout=%d",
|
|
||||||
*dbPath, 2000,
|
|
||||||
))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error opening database: %v\n", err)
|
log.Fatalf("error opening database: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
|
||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
"github.com/peterbourgon/ff"
|
"github.com/peterbourgon/ff"
|
||||||
|
|
||||||
|
"github.com/sentriz/gonic/db"
|
||||||
"github.com/sentriz/gonic/scanner"
|
"github.com/sentriz/gonic/scanner"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,14 +28,10 @@ func main() {
|
|||||||
if _, err := os.Stat(*musicPath); os.IsNotExist(err) {
|
if _, err := os.Stat(*musicPath); os.IsNotExist(err) {
|
||||||
log.Fatal("please provide a valid music directory")
|
log.Fatal("please provide a valid music directory")
|
||||||
}
|
}
|
||||||
db, err := gorm.Open("sqlite3", fmt.Sprintf(
|
db, err := db.New(*dbPath)
|
||||||
"%s?cache=shared&_busy_timeout=%d",
|
|
||||||
*dbPath, 2000,
|
|
||||||
))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error opening database: %v\n", err)
|
log.Fatalf("error opening database: %v\n", err)
|
||||||
}
|
}
|
||||||
db.SetLogger(log.New(os.Stdout, "gorm ", 0))
|
|
||||||
s := scanner.New(
|
s := scanner.New(
|
||||||
db,
|
db,
|
||||||
*musicPath,
|
*musicPath,
|
||||||
|
|||||||
34
db/db.go
Normal file
34
db/db.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
dbMaxOpenConns = 1
|
||||||
|
dbOptions = url.Values{
|
||||||
|
// with this, multiple connections share a single data and schema cache.
|
||||||
|
// see https://www.sqlite.org/sharedcache.html
|
||||||
|
"cache": []string{"shared"},
|
||||||
|
// with this, the db sleeps for a little while when locked. can prevent
|
||||||
|
// a SQLITE_BUSY. see https://www.sqlite.org/c3ref/busy_timeout.html
|
||||||
|
"_busy_timeout": []string{"30000"},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(path string) (*gorm.DB, error) {
|
||||||
|
pathAndArgs := fmt.Sprintf("%s?%s", path, dbOptions.Encode())
|
||||||
|
db, err := gorm.Open("sqlite3", pathAndArgs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "with gorm")
|
||||||
|
}
|
||||||
|
db.DB().SetMaxOpenConns(dbMaxOpenConns)
|
||||||
|
db.SetLogger(log.New(os.Stdout, "gorm ", 0))
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user