move get funcs to db
This commit is contained in:
35
db/db.go
35
db/db.go
@@ -24,7 +24,38 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(path string) (*gorm.DB, error) {
|
type DB struct {
|
||||||
|
*gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetSetting(key string) string {
|
||||||
|
setting := &model.Setting{}
|
||||||
|
db.
|
||||||
|
Where("key = ?", key).
|
||||||
|
First(setting)
|
||||||
|
return setting.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) SetSetting(key, value string) {
|
||||||
|
db.
|
||||||
|
Where(model.Setting{Key: key}).
|
||||||
|
Assign(model.Setting{Value: value}).
|
||||||
|
FirstOrCreate(&model.Setting{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetUserFromName(name string) *model.User {
|
||||||
|
user := &model.User{}
|
||||||
|
err := db.
|
||||||
|
Where("name = ?", name).
|
||||||
|
First(user).
|
||||||
|
Error
|
||||||
|
if gorm.IsRecordNotFoundError(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(path string) (*DB, error) {
|
||||||
pathAndArgs := fmt.Sprintf("%s?%s", path, dbOptions.Encode())
|
pathAndArgs := fmt.Sprintf("%s?%s", path, dbOptions.Encode())
|
||||||
db, err := gorm.Open("sqlite3", pathAndArgs)
|
db, err := gorm.Open("sqlite3", pathAndArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -45,5 +76,5 @@ func New(path string) (*gorm.DB, error) {
|
|||||||
Password: "admin",
|
Password: "admin",
|
||||||
IsAdmin: true,
|
IsAdmin: true,
|
||||||
})
|
})
|
||||||
return db, nil
|
return &DB{DB: db}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rainycape/unidecode"
|
"github.com/rainycape/unidecode"
|
||||||
|
|
||||||
|
"github.com/sentriz/gonic/db"
|
||||||
"github.com/sentriz/gonic/mime"
|
"github.com/sentriz/gonic/mime"
|
||||||
"github.com/sentriz/gonic/model"
|
"github.com/sentriz/gonic/model"
|
||||||
"github.com/sentriz/gonic/scanner/stack"
|
"github.com/sentriz/gonic/scanner/stack"
|
||||||
@@ -51,14 +52,14 @@ func decoded(in string) string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func withTx(db *gorm.DB, cb func(tx *gorm.DB)) {
|
func withTx(db *db.DB, cb func(tx *gorm.DB)) {
|
||||||
tx := db.Begin()
|
tx := db.Begin()
|
||||||
defer tx.Commit()
|
defer tx.Commit()
|
||||||
cb(tx)
|
cb(tx)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Scanner struct {
|
type Scanner struct {
|
||||||
db *gorm.DB
|
db *db.DB
|
||||||
musicPath string
|
musicPath string
|
||||||
// these two are for the transaction we do for every folder.
|
// these two are for the transaction we do for every folder.
|
||||||
// the boolean is there so we dont begin or commit multiple
|
// the boolean is there so we dont begin or commit multiple
|
||||||
@@ -78,7 +79,7 @@ type Scanner struct {
|
|||||||
seenTracksErr int // n tracks we we couldn't scan
|
seenTracksErr int // n tracks we we couldn't scan
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db *gorm.DB, musicPath string) *Scanner {
|
func New(db *db.DB, musicPath string) *Scanner {
|
||||||
return &Scanner{
|
return &Scanner{
|
||||||
db: db,
|
db: db,
|
||||||
musicPath: musicPath,
|
musicPath: musicPath,
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
|
||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
|
|
||||||
"github.com/sentriz/gonic/db"
|
"github.com/sentriz/gonic/db"
|
||||||
@@ -24,7 +23,7 @@ func init() {
|
|||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(ioutil.Discard)
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetTables(db *gorm.DB) {
|
func resetTables(db *db.DB) {
|
||||||
tx := db.Begin()
|
tx := db.Begin()
|
||||||
defer tx.Commit()
|
defer tx.Commit()
|
||||||
tx.Exec("delete from tracks")
|
tx.Exec("delete from tracks")
|
||||||
@@ -32,7 +31,7 @@ func resetTables(db *gorm.DB) {
|
|||||||
tx.Exec("delete from albums")
|
tx.Exec("delete from albums")
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetTablesPause(db *gorm.DB, b *testing.B) {
|
func resetTablesPause(db *db.DB, b *testing.B) {
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
defer b.StartTimer()
|
defer b.StartTimer()
|
||||||
resetTables(db)
|
resetTables(db)
|
||||||
|
|||||||
@@ -3,10 +3,9 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
|
||||||
"github.com/wader/gormstore"
|
"github.com/wader/gormstore"
|
||||||
|
|
||||||
"github.com/sentriz/gonic/model"
|
"github.com/sentriz/gonic/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
type contextKey int
|
type contextKey int
|
||||||
@@ -17,35 +16,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
DB *gorm.DB
|
DB *db.DB
|
||||||
SessDB *gormstore.Store
|
SessDB *gormstore.Store
|
||||||
Templates map[string]*template.Template
|
Templates map[string]*template.Template
|
||||||
MusicPath string
|
MusicPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) GetSetting(key string) string {
|
|
||||||
setting := &model.Setting{}
|
|
||||||
c.DB.
|
|
||||||
Where("key = ?", key).
|
|
||||||
First(setting)
|
|
||||||
return setting.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Controller) SetSetting(key, value string) {
|
|
||||||
c.DB.
|
|
||||||
Where(model.Setting{Key: key}).
|
|
||||||
Assign(model.Setting{Value: value}).
|
|
||||||
FirstOrCreate(&model.Setting{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Controller) GetUserFromName(name string) *model.User {
|
|
||||||
user := &model.User{}
|
|
||||||
err := c.DB.
|
|
||||||
Where("name = ?", name).
|
|
||||||
First(user).
|
|
||||||
Error
|
|
||||||
if gorm.IsRecordNotFoundError(err) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return user
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
|
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.GetUserFromName(username)
|
user := c.DB.GetUserFromName(username)
|
||||||
if user == nil || password != user.Password {
|
if user == nil || password != user.Password {
|
||||||
session.AddFlash("invalid username / password")
|
session.AddFlash("invalid username / password")
|
||||||
sessionLogSave(w, r, session)
|
sessionLogSave(w, r, session)
|
||||||
@@ -60,7 +60,7 @@ func (c *Controller) ServeHome(w http.ResponseWriter, r *http.Request) {
|
|||||||
Order("updated_at DESC").
|
Order("updated_at DESC").
|
||||||
Limit(8).
|
Limit(8).
|
||||||
Find(&data.RecentFolders)
|
Find(&data.RecentFolders)
|
||||||
data.CurrentLastFMAPIKey = c.GetSetting("lastfm_api_key")
|
data.CurrentLastFMAPIKey = c.DB.GetSetting("lastfm_api_key")
|
||||||
scheme := firstExisting(
|
scheme := firstExisting(
|
||||||
"http", // fallback
|
"http", // fallback
|
||||||
r.Header.Get("X-Forwarded-Proto"),
|
r.Header.Get("X-Forwarded-Proto"),
|
||||||
@@ -104,8 +104,8 @@ func (c *Controller) ServeLinkLastFMDo(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
sessionKey, err := lastfm.GetSession(
|
sessionKey, err := lastfm.GetSession(
|
||||||
c.GetSetting("lastfm_api_key"),
|
c.DB.GetSetting("lastfm_api_key"),
|
||||||
c.GetSetting("lastfm_secret"),
|
c.DB.GetSetting("lastfm_secret"),
|
||||||
token,
|
token,
|
||||||
)
|
)
|
||||||
session := r.Context().Value(contextSessionKey).(*sessions.Session)
|
session := r.Context().Value(contextSessionKey).(*sessions.Session)
|
||||||
@@ -240,8 +240,8 @@ func (c *Controller) ServeCreateUserDo(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (c *Controller) ServeUpdateLastFMAPIKey(w http.ResponseWriter, r *http.Request) {
|
func (c *Controller) ServeUpdateLastFMAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||||
data := &templateData{}
|
data := &templateData{}
|
||||||
data.CurrentLastFMAPIKey = c.GetSetting("lastfm_api_key")
|
data.CurrentLastFMAPIKey = c.DB.GetSetting("lastfm_api_key")
|
||||||
data.CurrentLastFMAPISecret = c.GetSetting("lastfm_secret")
|
data.CurrentLastFMAPISecret = c.DB.GetSetting("lastfm_secret")
|
||||||
renderTemplate(w, r, c.Templates["update_lastfm_api_key.tmpl"], data)
|
renderTemplate(w, r, c.Templates["update_lastfm_api_key.tmpl"], data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,8 +256,8 @@ func (c *Controller) ServeUpdateLastFMAPIKeyDo(w http.ResponseWriter, r *http.Re
|
|||||||
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
|
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.SetSetting("lastfm_api_key", apiKey)
|
c.DB.SetSetting("lastfm_api_key", apiKey)
|
||||||
c.SetSetting("lastfm_secret", secret)
|
c.DB.SetSetting("lastfm_secret", secret)
|
||||||
http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
|
http.Redirect(w, r, "/admin/home", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ func (c *Controller) GetAlbumList(w http.ResponseWriter, r *http.Request) {
|
|||||||
respondError(w, r, 10, "please provide a `type` parameter")
|
respondError(w, r, 10, "please provide a `type` parameter")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
q := c.DB
|
q := c.DB.DB
|
||||||
switch listType {
|
switch listType {
|
||||||
case "alphabeticalByArtist":
|
case "alphabeticalByArtist":
|
||||||
q = q.Joins(`
|
q = q.Joins(`
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ func (c *Controller) GetAlbumListTwo(w http.ResponseWriter, r *http.Request) {
|
|||||||
respondError(w, r, 10, "please provide a `type` parameter")
|
respondError(w, r, 10, "please provide a `type` parameter")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
q := c.DB
|
q := c.DB.DB
|
||||||
switch listType {
|
switch listType {
|
||||||
case "alphabeticalByArtist":
|
case "alphabeticalByArtist":
|
||||||
q = q.Joins(`
|
q = q.Joins(`
|
||||||
|
|||||||
@@ -129,8 +129,8 @@ func (c *Controller) Scrobble(w http.ResponseWriter, r *http.Request) {
|
|||||||
First(track, id)
|
First(track, id)
|
||||||
// scrobble with above info
|
// scrobble with above info
|
||||||
err = lastfm.Scrobble(
|
err = lastfm.Scrobble(
|
||||||
c.GetSetting("lastfm_api_key"),
|
c.DB.GetSetting("lastfm_api_key"),
|
||||||
c.GetSetting("lastfm_secret"),
|
c.DB.GetSetting("lastfm_secret"),
|
||||||
user.LastFMSession,
|
user.LastFMSession,
|
||||||
track,
|
track,
|
||||||
// clients will provide time in miliseconds, so use that or
|
// clients will provide time in miliseconds, so use that or
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func (c *Controller) WithUserSession(next http.HandlerFunc) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// take username from sesion and add the user row to the context
|
// take username from sesion and add the user row to the context
|
||||||
user := c.GetUserFromName(username)
|
user := c.DB.GetUserFromName(username)
|
||||||
if user == nil {
|
if user == nil {
|
||||||
// the username in the client's session no longer relates to a
|
// the username in the client's session no longer relates to a
|
||||||
// user in the database (maybe the user was deleted)
|
// user in the database (maybe the user was deleted)
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ func (c *Controller) WithValidSubsonicArgs(next http.HandlerFunc) http.HandlerFu
|
|||||||
"please provide parameters `t` and `s`, or just `p`")
|
"please provide parameters `t` and `s`, or just `p`")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := c.GetUserFromName(username)
|
user := c.DB.GetUserFromName(username)
|
||||||
if user == nil {
|
if user == nil {
|
||||||
respondError(w, r, 40, "invalid username `%s`", username)
|
respondError(w, r, 40, "invalid username `%s`", username)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -11,31 +11,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type templateData struct {
|
type templateData struct {
|
||||||
AlbumCount int
|
// common
|
||||||
AllUsers []*model.User
|
Flashes []interface{}
|
||||||
ArtistCount int
|
User *model.User
|
||||||
|
// home
|
||||||
|
AlbumCount int
|
||||||
|
ArtistCount int
|
||||||
|
TrackCount int
|
||||||
|
RequestRoot string
|
||||||
|
RecentFolders []*model.Album
|
||||||
|
AllUsers []*model.User
|
||||||
|
//
|
||||||
CurrentLastFMAPIKey string
|
CurrentLastFMAPIKey string
|
||||||
CurrentLastFMAPISecret string
|
CurrentLastFMAPISecret string
|
||||||
Flashes []interface{}
|
|
||||||
RecentFolders []*model.Album
|
|
||||||
RequestRoot string
|
|
||||||
SelectedUser *model.User
|
SelectedUser *model.User
|
||||||
TrackCount int
|
|
||||||
User *model.User
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTemplate(w http.ResponseWriter, r *http.Request,
|
func renderTemplate(
|
||||||
tmpl *template.Template, data *templateData) {
|
w http.ResponseWriter,
|
||||||
session := r.Context().Value(contextSessionKey).(*sessions.Session)
|
r *http.Request,
|
||||||
|
tmpl *template.Template,
|
||||||
|
data *templateData,
|
||||||
|
) {
|
||||||
if data == nil {
|
if data == nil {
|
||||||
data = &templateData{}
|
data = &templateData{}
|
||||||
}
|
}
|
||||||
|
session := r.Context().Value(contextSessionKey).(*sessions.Session)
|
||||||
data.Flashes = session.Flashes()
|
data.Flashes = session.Flashes()
|
||||||
sessionLogSave(w, r, session)
|
sessionLogSave(w, r, session)
|
||||||
user, ok := r.Context().Value(contextUserKey).(*model.User)
|
data.User = r.Context().Value(contextUserKey).(*model.User)
|
||||||
if ok {
|
|
||||||
data.User = user
|
|
||||||
}
|
|
||||||
err := tmpl.Execute(w, data)
|
err := tmpl.Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error executing template: %v\n", err)
|
log.Printf("error executing template: %v\n", err)
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/sentriz/gonic/db"
|
||||||
|
|
||||||
"github.com/sentriz/gonic/server/handler"
|
"github.com/sentriz/gonic/server/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +17,7 @@ type Server struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
db *gorm.DB,
|
db *db.DB,
|
||||||
musicPath string,
|
musicPath string,
|
||||||
listenAddr string,
|
listenAddr string,
|
||||||
assetPath string,
|
assetPath string,
|
||||||
|
|||||||
@@ -80,12 +80,12 @@ func staticHandler(assets *Assets, path string) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) SetupAdmin() error {
|
func (s *Server) SetupAdmin() error {
|
||||||
sessionKey := []byte(s.GetSetting("session_key"))
|
sessionKey := []byte(s.DB.GetSetting("session_key"))
|
||||||
if len(sessionKey) == 0 {
|
if len(sessionKey) == 0 {
|
||||||
sessionKey = securecookie.GenerateRandomKey(32)
|
sessionKey = securecookie.GenerateRandomKey(32)
|
||||||
s.SetSetting("session_key", string(sessionKey))
|
s.DB.SetSetting("session_key", string(sessionKey))
|
||||||
}
|
}
|
||||||
s.SessDB = gormstore.New(s.DB, sessionKey)
|
s.SessDB = gormstore.New(s.DB.DB, sessionKey)
|
||||||
go s.SessDB.PeriodicCleanup(time.Hour, nil)
|
go s.SessDB.PeriodicCleanup(time.Hour, nil)
|
||||||
//
|
//
|
||||||
tmplBase := template.
|
tmplBase := template.
|
||||||
|
|||||||
Reference in New Issue
Block a user