-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from iloveicedgreentea/develop
Large refactor, automatic restarts, webui, API
- Loading branch information
Showing
50 changed files
with
3,438 additions
and
2,408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
blank_issues_enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ on: | |
tags: ["v*.*.*"] | ||
pull_request: | ||
branches: ["master", "develop", "clip_support"] | ||
|
||
|
||
env: | ||
# Use docker.io for Docker Hub if empty | ||
|
@@ -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 }} | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.