From 9eba465a9ed5c78caabe87df697e2944507eb7d9 Mon Sep 17 00:00:00 2001 From: Dan Anstis Date: Sun, 9 Dec 2018 15:54:31 +1000 Subject: [PATCH] Feature: Move config to TOML file. Closes #120 --- CHANGELOG.md | 100 ++++++++++++++------------- GitVersion.yml | 8 +-- Gopkg.lock | 86 ----------------------- Gopkg.toml | 43 ------------ config/config.go | 71 +++++++++++++++++++ database/database.go | 8 --- go.mod | 15 ++++ go.sum | 24 +++++++ main.go | 27 +++----- models/base.go | 16 ----- models/settings.go | 47 ------------- plex/server.go | 3 - web/apiHandlers.go | 22 +++--- web/static/js/PSapp.js | 3 +- web/templates/settings/settings.html | 8 +-- 15 files changed, 191 insertions(+), 290 deletions(-) delete mode 100644 Gopkg.lock delete mode 100644 Gopkg.toml create mode 100644 config/config.go delete mode 100644 database/database.go create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 models/base.go delete mode 100644 models/settings.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ec457d..048546e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,97 +6,101 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p +## [Unreleased] + +- Changed - ([#120](https://github.com/danstis/Plex-Sync/issues/120)) Move configuration to TOML config file. + ## [v0.7.2] -* Fixed - Version number on build. +- Fixed - Version number on build. ## [v0.7.1] -* Fixed - ([#106](https://github.com/danstis/rmstale/issues/106)) Resolve issue with cache files. +- Fixed - ([#106](https://github.com/danstis/Plex-Sync/issues/106)) Resolve issue with cache files. ## [v0.7.0] -* New - ([#110](https://github.com/danstis/rmstale/issues/110)) Add exe details and Icon for windows executable. +- New - ([#110](https://github.com/danstis/Plex-Sync/issues/110)) Add exe details and Icon for windows executable. ## [v0.6.0] -* Fixed - ([#105](https://github.com/danstis/rmstale/issues/105)) Caching of thumbnail images. -* Fixed - ([#104](https://github.com/danstis/rmstale/issues/104)) HTTPS setting not populating. -* New - Create API for web interface calls. -* Changed - ([#70](https://github.com/danstis/rmstale/issues/70)) Move settings into Database. -* Changed - Refactor logging to new logger package. -* Changed - ([#57](https://github.com/danstis/rmstale/issues/57)) Update web interface settings page to enable population of all settings. -* Changed - Index images retreved from API. -* Changed - ([#95](https://github.com/danstis/rmstale/issues/95)) Update error messages to make more sense. -* Changed - ([#109](https://github.com/danstis/rmstale/issues/109)) Remove config file from Readme file. +- Fixed - ([#105](https://github.com/danstis/Plex-Sync/issues/105)) Caching of thumbnail images. +- Fixed - ([#104](https://github.com/danstis/Plex-Sync/issues/104)) HTTPS setting not populating. +- New - Create API for web interface calls. +- Changed - ([#70](https://github.com/danstis/Plex-Sync/issues/70)) Move settings into Database. +- Changed - Refactor logging to new logger package. +- Changed - ([#57](https://github.com/danstis/Plex-Sync/issues/57)) Update web interface settings page to enable population of all settings. +- Changed - Index images retreved from API. +- Changed - ([#95](https://github.com/danstis/Plex-Sync/issues/95)) Update error messages to make more sense. +- Changed - ([#109](https://github.com/danstis/Plex-Sync/issues/109)) Remove config file from Readme file. ## [v0.5.2] -* Fixed - ([#89](https://github.com/danstis/rmstale/issues/89)) Token file not generated if cache folder does not exist. -* Changed - Changelog formatting. +- Fixed - ([#89](https://github.com/danstis/Plex-Sync/issues/89)) Token file not generated if cache folder does not exist. +- Changed - Changelog formatting. ## [v0.5.1] -* Fixed - ([#87](https://github.com/danstis/Plex-Sync/issues/87)) Update to return message if watched item has already been scrobbled on remote server. -* Changed - Move config into own package. +- Fixed - ([#87](https://github.com/danstis/Plex-Sync/issues/87)) Update to return message if watched item has already been scrobbled on remote server. +- Changed - Move config into own package. ## [v0.5.0] -* Changed - ([#63](https://github.com/danstis/Plex-Sync/issues/63)) Add version number to console on startup. -* Changed - ([#58](https://github.com/danstis/Plex-Sync/issues/58)) Changed text list of selected shows to thumbnail images from destination server. -* Changed - ([#61](https://github.com/danstis/Plex-Sync/issues/61)) Modify build to control versioning. -* Changed - ([#74](https://github.com/danstis/Plex-Sync/issues/74)) Moved tvshows.txt file to config folder. -* Changed - ([#76](https://github.com/danstis/Plex-Sync/issues/76)) Update display of shows on homepage to be responsive. -* Fixed - Cleanup code errors. +- Changed - ([#63](https://github.com/danstis/Plex-Sync/issues/63)) Add version number to console on startup. +- Changed - ([#58](https://github.com/danstis/Plex-Sync/issues/58)) Changed text list of selected shows to thumbnail images from destination server. +- Changed - ([#61](https://github.com/danstis/Plex-Sync/issues/61)) Modify build to control versioning. +- Changed - ([#74](https://github.com/danstis/Plex-Sync/issues/74)) Moved tvshows.txt file to config folder. +- Changed - ([#76](https://github.com/danstis/Plex-Sync/issues/76)) Update display of shows on homepage to be responsive. +- Fixed - Cleanup code errors. ## [v0.4.1] -* Fixed - ([#59](https://github.com/danstis/Plex-Sync/issues/59)) Handle returned errors for the GetToken method. +- Fixed - ([#59](https://github.com/danstis/Plex-Sync/issues/59)) Handle returned errors for the GetToken method. ## [v0.4.0] -* Changed - ([#50](https://github.com/danstis/Plex-Sync/issues/50)) Refresh shows when reloading the WebUI homepage. -* Changed - ([#55](https://github.com/danstis/Plex-Sync/issues/55)) Ignore pre-release tags on header of web interface. -* Changed - ([#54](https://github.com/danstis/Plex-Sync/issues/54)) Move full version number into the settings page. +- Changed - ([#50](https://github.com/danstis/Plex-Sync/issues/50)) Refresh shows when reloading the WebUI homepage. +- Changed - ([#55](https://github.com/danstis/Plex-Sync/issues/55)) Ignore pre-release tags on header of web interface. +- Changed - ([#54](https://github.com/danstis/Plex-Sync/issues/54)) Move full version number into the settings page. ## [v0.3.4] -* New - Script to bump version using GitVersion. -* New - Go generate script to update version. -* Changed - Moved builds from TravisCI to Appveyor. +- New - Script to bump version using GitVersion. +- New - Go generate script to update version. +- Changed - Moved builds from TravisCI to Appveyor. ## [v0.3.1] -* Fixed - Sort selected shows on Home page. +- Fixed - Sort selected shows on Home page. ## [v0.3.0] -* New - Show selected shows on home page. -* New - Version to WebUI pages. +- New - Show selected shows on home page. +- New - Version to WebUI pages. ## [v0.2.0] -* New - Ability to remove a cached token from the web interface. -* New - Logging to file. -* New - Logging to web interface. -* New - Web interface for management of the application. -* Changed - After token generation direct user to the settings page. -* Changed - Moved generation of token file into web interface. -* Changed - Update logging to go to file and StdOut. -* Changed - Update release to remove all .go files from subdirectories. +- New - Ability to remove a cached token from the web interface. +- New - Logging to file. +- New - Logging to web interface. +- New - Web interface for management of the application. +- Changed - After token generation direct user to the settings page. +- Changed - Moved generation of token file into web interface. +- Changed - Update logging to go to file and StdOut. +- Changed - Update release to remove all .go files from subdirectories. ## [v0.1.0] -* New - Add Readme. -* New - Usage instructions to the Readme file. -* New - Cache token in local token file. -* New - Enable syncing of Show watched status from a local server to a remote server. -* New - Support for MyPlex Account. -* Fixed - App would attempt to sync even if a token was not obtained. -* Fixed - Spaces in TV Show names cause errors. +- New - Add Readme. +- New - Usage instructions to the Readme file. +- New - Cache token in local token file. +- New - Enable syncing of Show watched status from a local server to a remote server. +- New - Support for MyPlex Account. +- Fixed - App would attempt to sync even if a token was not obtained. +- Fixed - Spaces in TV Show names cause errors. [unreleased]: https://github.com/danstis/Plex-Sync/compare/v0.7.2...HEAD [v0.7.2]: https://github.com/danstis/Plex-Sync/compare/v0.7.1...v0.7.2 diff --git a/GitVersion.yml b/GitVersion.yml index 32a2981..5488751 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -1,9 +1,9 @@ assembly-versioning-scheme: None mode: Mainline -major-version-bump-message: '\+semver:\s?(breaking|major)' -minor-version-bump-message: '\+semver:\s?(feature|minor)' -patch-version-bump-message: '\+semver:\s?(fix|patch)' -no-bump-message: '\+semver:\s?(none|skip)' +major-version-bump-message: '(\+semver:\s?(breaking|major)|Major:\s?)' +minor-version-bump-message: '(\+semver:\s?(feature|minor)|Feature:\s?|Minor:\s?)' +patch-version-bump-message: '(\+semver:\s?(fix|patch)|Patch:\s?)' +no-bump-message: '(\+semver:\s?(none|skip)|NOCI:\s?)' branches: {} ignore: sha: [] diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index 7b17e93..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,86 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - branch = "master" - name = "github.com/gopherjs/gopherjs" - packages = ["js"] - revision = "df18d38287ab2ed3138d564e7c8cbe4d5a249d87" - -[[projects]] - name = "github.com/gorilla/context" - packages = ["."] - revision = "1ea25387ff6f684839d82767c1733ff4d4d15d0a" - version = "v1.1" - -[[projects]] - name = "github.com/gorilla/handlers" - packages = ["."] - revision = "90663712d74cb411cbef281bc1e08c19d1a76145" - version = "v1.3.0" - -[[projects]] - name = "github.com/gorilla/mux" - packages = ["."] - revision = "53c1911da2b537f792e7cafcb446b05ffe33b996" - version = "v1.6.1" - -[[projects]] - name = "github.com/jinzhu/gorm" - packages = [ - ".", - "dialects/sqlite" - ] - revision = "6ed508ec6a4ecb3531899a69cbc746ccf65a4166" - version = "v1.9.1" - -[[projects]] - branch = "master" - name = "github.com/jinzhu/inflection" - packages = ["."] - revision = "1c35d901db3da928c72a72d8458480cc9ade058f" - -[[projects]] - name = "github.com/jtolds/gls" - packages = ["."] - revision = "77f18212c9c7edc9bd6a33d383a7b545ce62f064" - version = "v4.2.1" - -[[projects]] - name = "github.com/mattn/go-sqlite3" - packages = ["."] - revision = "6c771bb9887719704b210e87e934f08be014bdb1" - version = "v1.6.0" - -[[projects]] - name = "github.com/smartystreets/assertions" - packages = [ - ".", - "internal/go-render/render", - "internal/oglematchers" - ] - revision = "0b37b35ec7434b77e77a4bb29b79677cced992ea" - version = "1.8.1" - -[[projects]] - name = "github.com/smartystreets/goconvey" - packages = [ - "convey", - "convey/gotest", - "convey/reporting" - ] - revision = "9e8dc3f972df6c8fcc0375ef492c24d0bb204857" - version = "1.6.3" - -[[projects]] - name = "gopkg.in/natefinch/lumberjack.v2" - packages = ["."] - revision = "a96e63847dc3c67d17befa69c303767e2f84e54f" - version = "v2.1" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - inputs-digest = "9ab2087aa18b02dbb14d0c6ea3f7e5ac662f5511c239c8298ef1bf5acf90ed73" - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index f9f0fd1..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,43 +0,0 @@ - -# Gopkg.toml example -# -# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" - - -[[constraint]] - name = "github.com/gorilla/handlers" - version = "^1.3.0" - -[[constraint]] - name = "github.com/gorilla/mux" - version = "^1.6.0" - -[[constraint]] - name = "gopkg.in/natefinch/lumberjack.v2" - version = "^2.1.0" - -[[constraint]] - name = "github.com/smartystreets/goconvey" - version = "^1.6.3" - -[[constraint]] - name = "github.com/jinzhu/gorm" - version = "^1.0.0" - diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..998aa29 --- /dev/null +++ b/config/config.go @@ -0,0 +1,71 @@ +package config + +import ( + "bufio" + "os" + "path" + + "github.com/BurntSushi/toml" + "github.com/danstis/Plex-Sync/plex" +) + +//ConfigFile defines the path to the configuration file +var ( + configFile = path.Join("config", "config.toml") +) + +// Settings defines the program configureation +type Settings struct { + WebServerPort int + CacheLifetime int + SyncInterval int + Logging logging `toml:"logging"` + LocalServer plex.Host `toml:"localserver"` + RemoteServer plex.Host `toml:"remoteserver"` +} + +type logging struct { + Logfile string + Webserverlogfile string + MaxLogSize int + MaxLogCount int + MaxLogAge int +} + +// GetConfig returns the application configuration from the config TOML file. +func GetConfig() (Settings, error) { + var s Settings + _, err := toml.DecodeFile(configFile, &s) + if os.IsNotExist(err) { + s := Settings{ + WebServerPort: 8080, + Logging: logging{ + MaxLogSize: 5, + MaxLogCount: 1, + MaxLogAge: 30, + }, + LocalServer: plex.Host{}, + RemoteServer: plex.Host{}, + CacheLifetime: 30, + SyncInterval: 3600, + } + err := UpdateConfig(s) + return s, err + } + if err != nil { + return Settings{}, err + } + return s, nil +} + +// UpdateConfig sets the configuration settings in the config TOML file. +func UpdateConfig(s Settings) error { + f, err := os.OpenFile(configFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) + if err != nil { + return err + } + defer f.Close() + w := bufio.NewWriter(f) + defer w.Flush() + return toml.NewEncoder(w).Encode(s) +} diff --git a/database/database.go b/database/database.go deleted file mode 100644 index 089e7ca..0000000 --- a/database/database.go +++ /dev/null @@ -1,8 +0,0 @@ -package database - -import ( - "github.com/jinzhu/gorm" -) - -// Conn stores the database connection for reference by the other packages -var Conn *gorm.DB diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4c58dae --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module github.com/danstis/Plex-Sync + +require ( + github.com/BurntSushi/toml v0.3.1 + github.com/gopherjs/gopherjs v0.0.0-20180227192048-df18d38287ab // indirect + github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f // indirect + github.com/gorilla/handlers v1.3.0 + github.com/gorilla/mux v1.6.1 + github.com/jtolds/gls v4.2.1+incompatible // indirect + github.com/smartystreets/assertions v0.0.0-20170925172151-0b37b35ec743 // indirect + github.com/smartystreets/goconvey v0.0.0-20170602164621-9e8dc3f972df + github.com/smartystreets/gunit v0.0.0-20180314194857-6f0d6275bdcd // indirect + gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 + gopkg.in/yaml.v2 v2.2.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d6557de --- /dev/null +++ b/go.sum @@ -0,0 +1,24 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/gopherjs/gopherjs v0.0.0-20180227192048-df18d38287ab h1:K5vjMu7+DrdLnZJHe/Brysk/aUJZkK751TFsCyPO0AQ= +github.com/gopherjs/gopherjs v0.0.0-20180227192048-df18d38287ab/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f h1:9oNbS1z4rVpbnkHBdPZU4jo9bSmrLpII768arSyMFgk= +github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= +github.com/gorilla/handlers v1.3.0 h1:tsg9qP3mjt1h4Roxp+M1paRjrVBfPSOpBuVclh6YluI= +github.com/gorilla/handlers v1.3.0/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ= +github.com/gorilla/mux v1.6.1 h1:KOwqsTYZdeuMacU7CxjMNYEKeBvLbxW+psodrbcEa3A= +github.com/gorilla/mux v1.6.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE= +github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/smartystreets/assertions v0.0.0-20170925172151-0b37b35ec743 h1:b8jeeClJ1TQznpWsxNb/eN++2qFp0sIsZ+weU5UM7b0= +github.com/smartystreets/assertions v0.0.0-20170925172151-0b37b35ec743/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v0.0.0-20170602164621-9e8dc3f972df h1:AawEzDdiSpy07QO9efSOHQ/BRincGLxilju4pOq3k8s= +github.com/smartystreets/goconvey v0.0.0-20170602164621-9e8dc3f972df/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s= +github.com/smartystreets/gunit v0.0.0-20180314194857-6f0d6275bdcd h1:p5kvxG4NHogJX1brTLtvUSGdW0/aBvIyqDSW7tmnsmQ= +github.com/smartystreets/gunit v0.0.0-20180314194857-6f0d6275bdcd/go.mod h1:XUKj4gbqj2QvJk/OdLWzyZ3FYli0f+MdpngyryX0gcw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3 h1:AFxeG48hTWHhDTQDk/m2gorfVHUEa9vo3tp3D7TzwjI= +gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index ff086ea..8cf744c 100644 --- a/main.go +++ b/main.go @@ -7,39 +7,30 @@ import ( "net/http" - "github.com/danstis/Plex-Sync/database" + "github.com/danstis/Plex-Sync/config" "github.com/danstis/Plex-Sync/logger" - "github.com/danstis/Plex-Sync/models" "github.com/danstis/Plex-Sync/plex" "github.com/danstis/Plex-Sync/web" "github.com/gorilla/handlers" - "github.com/jinzhu/gorm" ) func main() { log.Printf("Plex-Sync v%v", plex.Version) - var err error - database.Conn, err = gorm.Open("sqlite3", "config/Plex-Sync.db") + settings, err := config.GetConfig() if err != nil { - log.Printf("ERROR: %v", err) - } - defer database.Conn.Close() - - models.Init(database.Conn) - var settings models.Settings - if err := settings.Load(); err != nil { log.Fatal(err) + return } plex.CacheLifetime = settings.CacheLifetime r := web.NewRouter() - loggedRouter := handlers.LoggingHandler(logger.CreateLogger("logs/plex-sync-webserver.log", settings.MaxLogSize, settings.MaxLogCount, settings.MaxLogAge), r) - go http.ListenAndServe(fmt.Sprintf(":%v", settings.WebserverPort), loggedRouter) - log.Printf("Started webserver http://localhost:%v", settings.WebserverPort) - log.SetOutput(logger.CreateLogger("logs/plex-sync.log", settings.MaxLogSize, settings.MaxLogCount, settings.MaxLogAge)) + loggedRouter := handlers.LoggingHandler(logger.CreateLogger("logs/plex-sync-webserver.log", settings.Logging.MaxLogSize, settings.Logging.MaxLogCount, settings.Logging.MaxLogAge), r) + go http.ListenAndServe(fmt.Sprintf(":%v", settings.WebServerPort), loggedRouter) + log.Printf("Started webserver http://localhost:%v", settings.WebServerPort) + log.SetOutput(logger.CreateLogger("logs/plex-sync.log", settings.Logging.MaxLogSize, settings.Logging.MaxLogCount, settings.Logging.MaxLogAge)) for { token := plex.Token() @@ -52,7 +43,7 @@ func main() { } plex.SyncWatchedTv(settings.LocalServer, settings.RemoteServer) } - log.Printf("Sleeping for %v...", settings.SyncInterval*time.Second) - time.Sleep(settings.SyncInterval * time.Second) + log.Printf("Sleeping for %v...", time.Duration(settings.SyncInterval)*time.Second) + time.Sleep(time.Duration(settings.SyncInterval) * time.Second) } } diff --git a/models/base.go b/models/base.go deleted file mode 100644 index 5dd65b7..0000000 --- a/models/base.go +++ /dev/null @@ -1,16 +0,0 @@ -package models - -import ( - "github.com/danstis/Plex-Sync/plex" - "github.com/jinzhu/gorm" - // The sqlite DB driver - _ "github.com/jinzhu/gorm/dialects/sqlite" -) - -// Init starts the DB migration -func Init(db *gorm.DB) { - db.AutoMigrate( - &Settings{}, - &plex.Host{}, - ) -} diff --git a/models/settings.go b/models/settings.go deleted file mode 100644 index 12858de..0000000 --- a/models/settings.go +++ /dev/null @@ -1,47 +0,0 @@ -package models - -import ( - "time" - - "github.com/danstis/Plex-Sync/database" - "github.com/danstis/Plex-Sync/plex" - "github.com/jinzhu/gorm" - // The sqlite DB driver - _ "github.com/jinzhu/gorm/dialects/sqlite" -) - -// Settings stores the application settings -type Settings struct { - gorm.Model - CacheLifetime int - MaxLogAge int - MaxLogCount int - MaxLogSize int - SyncInterval time.Duration - WebserverPort int - LocalServer plex.Host - LocalServerID uint - RemoteServer plex.Host - RemoteServerID uint -} - -// Save updates the settings in the Database -func (s *Settings) Save() error { - return database.Conn.Save(&s).Error -} - -// Load retreves the settings from the Database -func (s *Settings) Load() error { - if database.Conn.Set("gorm:auto_preload", true).First(&s, 1).RecordNotFound() { - s.SyncInterval = 600 - s.WebserverPort = 8085 - s.MaxLogSize = 20 - s.MaxLogCount = 5 - s.MaxLogAge = 1 - s.CacheLifetime = 5 - s.LocalServer = plex.Host{Port: 32400} - s.RemoteServer = plex.Host{Port: 32400} - return nil - } - return database.Conn.Set("gorm:auto_preload", true).First(&s, 1).Error -} diff --git a/plex/server.go b/plex/server.go index 0ea91b2..8412e47 100644 --- a/plex/server.go +++ b/plex/server.go @@ -13,8 +13,6 @@ import ( "sort" "strings" "time" - - "github.com/jinzhu/gorm" ) // CacheLifetime controls when a cached image will be refreshed in days @@ -24,7 +22,6 @@ var cachePath = filepath.Join(".cache", "show") // Host defines the data to be stored for server objects type Host struct { - gorm.Model Name string Hostname string Port int diff --git a/web/apiHandlers.go b/web/apiHandlers.go index 5b5b968..c3f2d4c 100644 --- a/web/apiHandlers.go +++ b/web/apiHandlers.go @@ -8,7 +8,7 @@ import ( "os" "path" - "github.com/danstis/Plex-Sync/models" + "github.com/danstis/Plex-Sync/config" "github.com/danstis/Plex-Sync/plex" "github.com/gorilla/mux" ) @@ -51,11 +51,9 @@ func apiTokenDelete(w http.ResponseWriter, r *http.Request) { } func apiSettingsGet(w http.ResponseWriter, r *http.Request) { - var s models.Settings - var err error - - if err = s.Load(); err != nil { - log.Printf("Error getting settings from DB: %v\n", err) + s, err := config.GetConfig() + if err != nil { + log.Printf("Error getting settings from config file: %v\n", err) w.WriteHeader(http.StatusFailedDependency) return } @@ -70,17 +68,17 @@ func apiSettingsGet(w http.ResponseWriter, r *http.Request) { } func apiSettingsCreate(w http.ResponseWriter, r *http.Request) { - var s models.Settings - if err := s.Load(); err != nil { - log.Printf("Error reading settings from DB: %v\n", err) - w.WriteHeader(http.StatusInternalServerError) + s, err := config.GetConfig() + if err != nil { + log.Printf("Error getting settings from config file: %v\n", err) + w.WriteHeader(http.StatusFailedDependency) return } json.NewDecoder(r.Body).Decode(&s) - if err := s.Save(); err != nil { - log.Printf("Error writing settings to DB: %v\n", err) + if err := config.UpdateConfig(s); err != nil { + log.Printf("Error writing settings to config file: %v\n", err) w.WriteHeader(http.StatusInternalServerError) return } diff --git a/web/static/js/PSapp.js b/web/static/js/PSapp.js index 49477b1..33206be 100644 --- a/web/static/js/PSapp.js +++ b/web/static/js/PSapp.js @@ -26,7 +26,8 @@ PSapp.controller("settings", function($scope, $http) { $scope.statusLevel = "alert-success"; }, // Failure - function(errMsg) { + function(error) { + var errMsg = error.message || "Internal Server Error"; $scope.statusMessage = "Error updating settings: " + errMsg; $scope.statusLevel = "alert-warning"; } diff --git a/web/templates/settings/settings.html b/web/templates/settings/settings.html index 8338c4f..faa8b45 100644 --- a/web/templates/settings/settings.html +++ b/web/templates/settings/settings.html @@ -92,7 +92,7 @@

General

- +
@@ -104,19 +104,19 @@

General

- +
- +
- +