Skip to content

Commit

Permalink
Refactored app packages
Browse files Browse the repository at this point in the history
  • Loading branch information
cld9x committed Dec 3, 2020
1 parent 084d3a0 commit 5ed8e7a
Show file tree
Hide file tree
Showing 23 changed files with 127 additions and 113 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/markphelps/optional v0.7.0
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mholt/archiver v3.1.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
//go:generate fileb0x .assets.toml

import (
"github.com/xbapps/xbvr/pkg/xbvr"
"github.com/xbapps/xbvr/pkg/server"
)

var version = "CURRENT"
Expand All @@ -12,5 +12,5 @@ var branch = "master"
var date = "moment ago"

func main() {
xbvr.StartServer(version, commit, branch, date)
server.StartServer(version, commit, branch, date)
}
5 changes: 4 additions & 1 deletion pkg/xbvr/api.go → pkg/api/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package xbvr
package api

import (
"github.com/emicklei/go-restful"
"github.com/xbapps/xbvr/pkg/common"
)

var log = common.Log

func APIError(req *restful.Request, resp *restful.Response, status int, err error) {
resp.WriteError(status, err)
}
14 changes: 4 additions & 10 deletions pkg/xbvr/api_deovr.go → pkg/api/deovr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xbvr
package api

import (
"encoding/json"
Expand All @@ -13,6 +13,7 @@ import (
"github.com/emicklei/go-restful"
restfulspec "github.com/emicklei/go-restful-openapi"
"github.com/markphelps/optional"
"github.com/xbapps/xbvr/pkg/common"
"github.com/xbapps/xbvr/pkg/models"
)

Expand Down Expand Up @@ -90,16 +91,9 @@ type DeoSceneVideoSource struct {
URL string `json:"url"`
}

func deoAuthEnabled() bool {
if DEOPASSWORD != "" && DEOUSER != "" {
return true
} else {
return false
}
}

func restfulAuthFilter(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
if deoAuthEnabled() {
if common.IsDeoAuthEnabled() {
var authorized bool

u, err := req.BodyParameter("login")
Expand All @@ -112,7 +106,7 @@ func restfulAuthFilter(req *restful.Request, resp *restful.Response, chain *rest
authorized = false
}

if u == DEOUSER && p == DEOPASSWORD {
if u == common.DEOUSER && p == common.DEOPASSWORD {
authorized = true
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/xbvr/api_dms.go → pkg/api/dms.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xbvr
package api

import (
"context"
Expand Down Expand Up @@ -148,7 +148,7 @@ func watchSessionFlush() {
}
}

func checkForDeadSession() {
func CheckForDeadSession() {
if time.Since(lastSessionEnd).Seconds() > 60 && lastSessionSceneID != 0 && lastSessionID != 0 {
watchSessionFlush()
lastSessionID = 0
Expand Down
2 changes: 1 addition & 1 deletion pkg/xbvr/api_files.go → pkg/api/files.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xbvr
package api

import (
"context"
Expand Down
15 changes: 8 additions & 7 deletions pkg/xbvr/api_options.go → pkg/api/options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xbvr
package api

import (
"context"
Expand All @@ -21,6 +21,7 @@ import (
"github.com/xbapps/xbvr/pkg/common"
"github.com/xbapps/xbvr/pkg/config"
"github.com/xbapps/xbvr/pkg/models"
"github.com/xbapps/xbvr/pkg/tasks"
"golang.org/x/oauth2"
"gopkg.in/resty.v1"
)
Expand Down Expand Up @@ -321,7 +322,7 @@ func (i ConfigResource) removeStorage(req *restful.Request, resp *restful.Respon
// Inform UI about state change
common.PublishWS("state.change.optionsStorage", nil)

RescanVolumes()
tasks.RescanVolumes()

log.WithField("task", "rescan").Info("Removed storage", vol.Path)

Expand Down Expand Up @@ -380,7 +381,7 @@ func (i ConfigResource) getState(req *restful.Request, resp *restful.Response) {
out.CurrentState.Web.SceneEdit = config.Config.Web.SceneEdit

// DLNA
out.CurrentState.DLNA.Running = IsDMSStarted()
out.CurrentState.DLNA.Running = tasks.IsDMSStarted()
out.CurrentState.DLNA.RecentIP = config.RecentIPAddresses
dlnaImages, _ := assets.WalkDirs("dlna", false)
for _, v := range dlnaImages {
Expand Down Expand Up @@ -434,13 +435,13 @@ func (i ConfigResource) saveOptionsDLNA(req *restful.Request, resp *restful.Resp
config.Config.Interfaces.DLNA.AllowedIP = r.AllowedIP
config.SaveConfig()

if IsDMSStarted() {
StopDMS()
if tasks.IsDMSStarted() {
tasks.StopDMS()
time.Sleep(1 * time.Second)
}

if r.Enabled {
StartDMS()
tasks.StartDMS()
}

resp.WriteHeaderAndEntity(http.StatusOK, r)
Expand Down Expand Up @@ -494,7 +495,7 @@ func (i ConfigResource) generateTestPreview(req *restful.Request, resp *restful.
if _, err := os.Stat(destFile); os.IsNotExist(err) {
// Preview file does not exist, generate it
go func() {
renderPreview(
tasks.RenderPreview(
files[0].GetPath(),
destFile,
r.StartTime,
Expand Down
2 changes: 1 addition & 1 deletion pkg/xbvr/api_playlist.go → pkg/api/playlist.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xbvr
package api

import (
"net/http"
Expand Down
12 changes: 7 additions & 5 deletions pkg/xbvr/api_scenes.go → pkg/api/scenes.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package xbvr
package api

import (
"fmt"
"github.com/go-test/deep"
"net/http"
"strconv"
"time"

"github.com/go-test/deep"
"github.com/xbapps/xbvr/pkg/tasks"

"github.com/blevesearch/bleve"
"github.com/emicklei/go-restful"
restfulspec "github.com/emicklei/go-restful-openapi"
Expand Down Expand Up @@ -234,8 +236,8 @@ func (i SceneResource) searchSceneIndex(req *restful.Request, resp *restful.Resp
db, _ := models.GetDB()
defer db.Close()

idx := NewIndex("scenes")
defer idx.bleve.Close()
idx := tasks.NewIndex("scenes")
defer idx.Bleve.Close()
query := bleve.NewQueryStringQuery(q)

searchRequest := bleve.NewSearchRequest(query)
Expand All @@ -245,7 +247,7 @@ func (i SceneResource) searchSceneIndex(req *restful.Request, resp *restful.Resp
searchRequest.Size = 25
searchRequest.SortBy([]string{"-_score"})

searchResults, err := idx.bleve.Search(searchRequest)
searchResults, err := idx.Bleve.Search(searchRequest)
if err != nil {
log.Error(err)
return
Expand Down
19 changes: 10 additions & 9 deletions pkg/xbvr/api_tasks.go → pkg/api/tasks.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package xbvr
package api

import (
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful-openapi"
"github.com/xbapps/xbvr/pkg/tasks"
)

type RequestScrapeJAVR struct {
Expand Down Expand Up @@ -48,36 +49,36 @@ func (i TaskResource) WebService() *restful.WebService {
}

func (i TaskResource) rescan(req *restful.Request, resp *restful.Response) {
go RescanVolumes()
go tasks.RescanVolumes()
}

func (i TaskResource) cleanTags(req *restful.Request, resp *restful.Response) {
go CleanTags()
go tasks.CleanTags()
}

func (i TaskResource) index(req *restful.Request, resp *restful.Response) {
go SearchIndex()
go tasks.SearchIndex()
}

func (i TaskResource) scrape(req *restful.Request, resp *restful.Response) {
qSiteID := req.QueryParameter("site")
if qSiteID == "" {
qSiteID = "_enabled"
}
go Scrape(qSiteID)
go tasks.Scrape(qSiteID)
}

func (i TaskResource) importBundle(req *restful.Request, resp *restful.Response) {
url := req.QueryParameter("url")
go ImportBundle(url)
go tasks.ImportBundle(url)
}

func (i TaskResource) exportBundle(req *restful.Request, resp *restful.Response) {
go ExportBundle()
go tasks.ExportBundle()
}

func (i TaskResource) previewGenerate(req *restful.Request, resp *restful.Response) {
go GeneratePreviews()
go tasks.GeneratePreviews()
}

func (i TaskResource) scrapeJAVR(req *restful.Request, resp *restful.Response) {
Expand All @@ -89,6 +90,6 @@ func (i TaskResource) scrapeJAVR(req *restful.Request, resp *restful.Response) {
}

if r.Query != "" {
go ScrapeJAVR(r.Query)
go tasks.ScrapeJAVR(r.Query)
}
}
31 changes: 31 additions & 0 deletions pkg/common/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package common

import (
"golang.org/x/crypto/bcrypt"
)

func IsDeoAuthEnabled() bool {
if DEOPASSWORD != "" && DEOUSER != "" {
return true
} else {
return false
}
}

func IsUIAuthEnabled() bool {
if UIPASSWORD != "" && UIUSER != "" {
return true
} else {
return false
}
}

func GetUISecret(user string, realm string) string {
if user == UIUSER {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(UIPASSWORD), bcrypt.DefaultCost)
if err == nil {
return string(hashedPassword)
}
}
return ""
}
4 changes: 4 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (

var (
DEBUG = os.Getenv("DEBUG")
DEOPASSWORD = os.Getenv("DEO_PASSWORD")
DEOUSER = os.Getenv("DEO_USERNAME")
UIPASSWORD = os.Getenv("UI_PASSWORD")
UIUSER = os.Getenv("UI_USERNAME")
DISABLE_ANALYTICS = os.Getenv("DISABLE_ANALYTICS")
SQL_DEBUG = envToBool("SQL_DEBUG", false)
DATABASE_URL = ""
Expand Down
7 changes: 3 additions & 4 deletions pkg/metrics/metrics.go → pkg/common/metrics.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package metrics
package common

import (
"os"
"path/filepath"
"time"

"github.com/lomik/go-whisper"
"github.com/xbapps/xbvr/pkg/common"
)

func GetMetric(name string) (*whisper.Whisper, error) {
retentions, err := whisper.ParseRetentionDefs("1m:1d,1h:60d,12h:20y")
path := filepath.Join(common.MetricsDir, name+".wsp")
path := filepath.Join(MetricsDir, name+".wsp")

wsp, err := whisper.Create(path, retentions, whisper.Last, 0.5)

Expand All @@ -30,7 +29,7 @@ func GetMetric(name string) (*whisper.Whisper, error) {
return wsp, nil
}

func WritePoint(name string, value float64) error {
func AddMetricPoint(name string, value float64) error {
db, err := GetMetric(name)
defer db.Close()
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions pkg/xbvr/task_cron.go → pkg/server/cron.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package xbvr
package server

import (
"fmt"

"github.com/robfig/cron/v3"
"github.com/xbapps/xbvr/pkg/api"
"github.com/xbapps/xbvr/pkg/config"
"github.com/xbapps/xbvr/pkg/tasks"
)

var cronInstance *cron.Cron

func SetupCron() {
cronInstance := cron.New()
cronInstance.AddFunc("@every 20s", checkForDeadSession)
cronInstance.AddFunc("@every 20s", api.CheckForDeadSession)
cronInstance.AddFunc(fmt.Sprintf("@every %vh", config.Config.Cron.ScrapeContentInterval), scrapeCron)
cronInstance.AddFunc(fmt.Sprintf("@every %vh", config.Config.Cron.RescanLibraryInterval), RescanVolumes)
cronInstance.AddFunc(fmt.Sprintf("@every %vh", config.Config.Cron.RescanLibraryInterval), tasks.RescanVolumes)
cronInstance.Start()
}

func scrapeCron() {
Scrape("_enabled")
tasks.Scrape("_enabled")
}
Loading

0 comments on commit 5ed8e7a

Please sign in to comment.