Skip to content

Commit

Permalink
Merge pull request #36 from iloveicedgreentea/develop
Browse files Browse the repository at this point in the history
Large refactor, automatic restarts, webui, API
  • Loading branch information
iloveicedgreentea authored Dec 27, 2023
2 parents c322add + a7f3e00 commit 5e58291
Show file tree
Hide file tree
Showing 50 changed files with 3,438 additions and 2,408 deletions.
13 changes: 9 additions & 4 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: Bug report
about: Create a report to help us improve
about: Create a bug report
title: ''
labels: bug
assignees: iloveicedgreentea
Expand All @@ -20,12 +20,17 @@ Steps to reproduce the behavior:
**Expected behavior**
A clear and concise description of what you expected to happen.

**Configuration**
Navigate to `(your-server-ip):9999/config` and paste the output here

**Debug logs**
Run the program with LOG_LEVEL=debug and send me the entire log output from start to finish
Run the program with LOG_LEVEL=debug and send me the entire log output while you trigger the bug, if possible.

Get logs from the `(your-server-ip):9999/logs` endpoint

**Desktop (please complete the following information):**
**Info (please complete the following information):**
- OS: [Windows, Unraid, etc]
- Version [e.g. 22]
- Version [e.g. 1.2]
- Deployment method: [Docker, binary]

**Additional context**
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blank_issues_enabled: false
9 changes: 3 additions & 6 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''
labels: 'feature'
assignees: 'iloveicedgreentea'

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
A clear and concise description of what the problem is. Ex. I'm frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
17 changes: 9 additions & 8 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ on:
tags: ["v*.*.*"]
pull_request:
branches: ["master", "develop", "clip_support"]


env:
# Use docker.io for Docker Hub if empty
Expand All @@ -32,16 +31,18 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/[email protected]
uses: docker/[email protected]

# TODO: run tests

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2.2.0
if: ${{ github.event_name == 'pull_request' && github.head_ref == 'develop' }}
uses: docker/login-action@v3.0.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
Expand All @@ -51,19 +52,19 @@ jobs:
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4.6.0
uses: docker/metadata-action@v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v4.1.1
uses: docker/build-push-action@v5.0.0
with:
context: .
# platforms: linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7
push: ${{ github.event_name != 'pull_request' }}
push: ${{ github.event_name == 'pull_request' && github.head_ref == 'develop' }}
tags: ${{ steps.meta.outputs.tags }}
# cache-from: type=gha
# cache-to: type=gha,mode=max
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
build
.DS_Store
config.json
*config.json
media.*.priv*
docker/data

# Binaries for programs and plugins
*.exe
Expand Down
13 changes: 13 additions & 0 deletions Contributing.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Contributing Rules

## Pull Requests
Pull requests *will not be accepted*. If you have a change you want to make, create an issue and I will look at it.

## Feature Requests
All feature requests must follow the template.

## Issues
Issues should be used for bug reports. Please follow the template when creating an issue.

## Support
Use the Discussions tab for support.
13 changes: 13 additions & 0 deletions Contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Contributing Rules

## Pull Requests
Pull requests *will not be accepted*. If you have a change you want to make, create an issue and I will look at it.

## Feature Requests
All feature requests must follow the template.

## Issues
Issues should be used for bug reports. Please follow the template when creating an issue.

## Support
Use the Discussions tab for support.
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ FROM golang:1.21 as build

WORKDIR /go/src/app
COPY . .

RUN go mod download
WORKDIR /go/src/app/cmd
RUN go vet -v

RUN CGO_ENABLED=0 go build -o /go/bin/app

FROM gcr.io/distroless/static-debian11
FROM alpine:20230901

RUN apk add supervisor
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY docker/watch.py /watch.py

COPY --from=build /go/bin/app /
CMD ["/app"]
COPY --from=build /go/src/app/web /web
EXPOSE 9999

# CMD ["/app"]
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
29 changes: 18 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
.PHONY: build test run

.PHONY: build test run docker-run docker-build
SHELL := /bin/bash
build:
go build -o ./build/server
cd cmd && go build -o ../build/server

test:
@go vet
@unset LOG_LEVEL && cd handlers && go test
@unset LOG_LEVEL && cd homeassistant && go test
@unset LOG_LEVEL && cd plex && go test
@unset LOG_LEVEL && cd mqtt && go test
@unset LOG_LEVEL && cd ezbeq && go test

# @go vet
@unset LOG_LEVEL && cd internal/config && go test -v
@unset LOG_LEVEL && cd internal/handlers && go test -v
@unset LOG_LEVEL && cd internal/homeassistant && go test -v
@unset LOG_LEVEL && cd internal/denon && go test -v
@unset LOG_LEVEL && cd internal/plex && go test -v
@unset LOG_LEVEL && cd internal/mqtt && go test -v
@unset LOG_LEVEL && cd internal/ezbeq && go test -v
docker-build:
docker buildx build --load --tag plex-webhook-automation-local .
docker-push:
docker buildx build --push --platform linux/amd64 --tag ghcr.io/iloveicedgreentea/plex-webhook-automation:test .
docker-run:
docker run -p 9999:9999 -e LOG_LEVEL=debug -v $(shell pwd)/docker/data:/data plex-webhook-automation-local
run: build
@./build/server
LOG_FILE=false LOG_LEVEL=debug ./build/server
146 changes: 146 additions & 0 deletions api/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package api

import (
"fmt"
"os"

"path/filepath"

"github.com/gin-gonic/gin"
"github.com/iloveicedgreentea/go-plex/internal/config"
"github.com/iloveicedgreentea/go-plex/internal/logger"
)

var log = logger.GetLogger()

func GenConfigPaths() (string, string) {
ex, err := os.Executable()
if err != nil {
log.Error(err)
}

exPath := filepath.Dir(ex)
configPath1 := "/data/config.json" // docker
configPath2 := filepath.Join(exPath, "../config.json") // Fallback path (for local)

log.Debugf("Config paths: %s, %s", configPath1, configPath2)

return configPath1, configPath2
}

// GetConfigPath returns the path to the config file
func GetConfigPath() (string, error) {
configPath1, configPath2 := GenConfigPaths()

if _, err := os.Stat(configPath1); err == nil {
return configPath1, nil
} else if _, err := os.Stat(configPath2); err == nil {
return configPath2, nil
}
return "", os.ErrNotExist
}

// ConfigExists checks if the config exists for the API
func ConfigExists(c *gin.Context) {
configPath, err := GetConfigPath()
if err != nil {
c.JSON(500, gin.H{"exists": false})
return
}
_, err = os.Stat(configPath)
c.JSON(200, gin.H{"exists": err == nil})
}

// Route for getting logs
func GetLogs(c *gin.Context) {
logFile, err := os.ReadFile("/data/application.log")
if err != nil {
c.JSON(500, gin.H{"error": "unable to read log file: " + err.Error()})
return
}
c.Data(200, "text/plain", logFile)
}

// GetConfig returns the config for the API
func GetConfig(c *gin.Context) {
path, err := GetConfigPath()
// if not found, create it
if err != nil {
log.Debugf("Didn't get config: %v", err)
err = CreateConfig(c)
if err != nil {
log.Debugf("Didn't create config: %v", err)
c.JSON(500, gin.H{"error": "unable to create config"})
return
}
}
data, err := os.ReadFile(path)
if err != nil {
log.Debugf("Didn't read config: %v", err)
c.JSON(500, gin.H{"error": "unable to read config"})
return
}
c.Data(200, "application/json", data)
}

// CreateConfig creates a new config file
func CreateConfig(c *gin.Context) error {
log.Debug("Creating new config")
configPath1, configPath2 := GenConfigPaths()

// try to create config in the first path
file, err := os.Create(configPath1)
if err != nil {
log.Debugf("Unable to create config in %s: %v", configPath1, err)
// try to create config in the second path
file, err = os.Create(configPath2)
if err != nil {
// if we can't create it in either path, return the error
log.Errorf("Unable to create config in %s: %v", configPath2, err)
return fmt.Errorf("unable to create config in %s or %s", configPath1, configPath2)
}
}
defer file.Close()

log.Debug("Successfully created config file")
return nil
}

// SaveConfig saves the config for the API
func SaveConfig(c *gin.Context) {
var jsonData map[string]interface{}

if err := c.ShouldBindJSON(&jsonData); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
fmt.Println(c.Request.Body)
return
}

path, err := GetConfigPath()
if err != nil {
log.Error("unable to get config")
c.JSON(500, gin.H{"error": "unable to get config"})
return
}

// Loop through the incoming JSON map to set keys in Viper
for key, value := range jsonData {
switch v := value.(type) {
case map[string]interface{}:
for subKey, subValue := range v {
config.Set(fmt.Sprintf("%s.%s", key, subKey), subValue)
}
default:
config.Set(key, value)
}
}

// Use your SaveConfigFile function to save the updated configuration
if err := config.SaveConfigFile(path); err != nil {
log.Error("unable to save config")
c.JSON(500, gin.H{"error": "Unable to save config"})
return
}

c.JSON(200, gin.H{"message": "Config saved successfully"})
}
12 changes: 12 additions & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api

import (
"github.com/gin-gonic/gin"
)

// RegisterRoutes registers the routes for the API contained in handlers.go
func RegisterRoutes(router *gin.Engine) {
router.GET("/config", GetConfig)
router.POST("/config", SaveConfig)
router.GET("/logs", GetLogs)
}
23 changes: 22 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,25 @@ use both denon and plex to compare codecs, make sure they are playing whats expe
Add support for detecting HDMI sync using madvr Envy. It will detect when media is synced and continue playing.
This prevents a black screen but stuff is playing which is annoying
It also makes the denon codec detection more accurate
New config homeAssistant.envyName
New config homeAssistant.envyName


9-10-23
Web UI flow for configuring server
New MQTT automations:
* "topicbeqcurrentprofile": -> which current profile is loaded
* "topicminidspmutestatus": -> true/false if minidsp(s) muted
* "topicplayingstatus": -> true/false if the tool is playing
Some functionality changes, check the readme
HDMI Sync automation in testing
*Breaking changes* All config fields are lowercase now

10-9-23
Modify UUID filter to accept comma for multiple

12-sometime-23
* Breaking change - volume must now be mounted to /data
* log to a file
* add /logs endpoint
* add prelim jellyfin support
* new name/logo
Loading

0 comments on commit 5e58291

Please sign in to comment.