Skip to content

Commit

Permalink
updates for v0.4.0 release. Slight refactor/organization.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed May 9, 2022
1 parent 54178ea commit 381a679
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 105 deletions.
6 changes: 5 additions & 1 deletion collector/cmd/collector-metrics/collector-metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"log"
"os"
"strings"
"time"

utils "github.com/analogj/go-util/utils"
Expand Down Expand Up @@ -113,7 +114,10 @@ OPTIONS:
}

if c.IsSet("api-endpoint") {
config.Set("api.endpoint", c.String("api-endpoint"))
//if the user is providing an api-endpoint with a basepath (eg. http://localhost:8080/scrutiny),
//we need to ensure the basepath has a trailing slash, otherwise the url.Parse() path concatenation doesnt work.
apiEndpoint := strings.TrimSuffix(c.String("api-endpoint"), "/") + "/"
config.Set("api.endpoint", apiEndpoint)
}

collectorLogger := logrus.WithFields(logrus.Fields{
Expand Down
5 changes: 3 additions & 2 deletions collector/pkg/collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (mc *MetricsCollector) Run() error {
}

apiEndpoint, _ := url.Parse(mc.apiEndpoint.String())
apiEndpoint.Path = "/api/devices/register"
apiEndpoint, _ = apiEndpoint.Parse("api/devices/register") //this acts like filepath.Join()

deviceRespWrapper := new(models.DeviceWrapper)

Expand All @@ -73,6 +73,7 @@ func (mc *MetricsCollector) Run() error {

if !deviceRespWrapper.Success {
mc.logger.Errorln("An error occurred while retrieving filtered devices")
mc.logger.Debugln(deviceRespWrapper)
return errors.ApiServerCommunicationError("An error occurred while retrieving filtered devices")
} else {
mc.logger.Debugln(deviceRespWrapper)
Expand Down Expand Up @@ -146,7 +147,7 @@ func (mc *MetricsCollector) Publish(deviceWWN string, payload []byte) error {
mc.logger.Infof("Publishing smartctl results for %s\n", deviceWWN)

apiEndpoint, _ := url.Parse(mc.apiEndpoint.String())
apiEndpoint.Path = fmt.Sprintf("/api/device/%s/smart", strings.ToLower(deviceWWN))
apiEndpoint, _ = apiEndpoint.Parse(fmt.Sprintf("api/device/%s/smart", strings.ToLower(deviceWWN)))

resp, err := httpClient.Post(apiEndpoint.String(), "application/json", bytes.NewBuffer(payload))
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions collector/pkg/collector/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package collector

import (
"github.com/stretchr/testify/require"
"net/url"
"testing"
)

func TestApiEndpointParse(t *testing.T) {
baseURL, _ := url.Parse("http://localhost:8080/")

url1, _ := baseURL.Parse("d/e")
require.Equal(t, "http://localhost:8080/d/e", url1.String())

url2, _ := baseURL.Parse("/d/e")
require.Equal(t, "http://localhost:8080/d/e", url2.String())
}

func TestApiEndpointParse_WithBasepathWithoutTrailingSlash(t *testing.T) {
baseURL, _ := url.Parse("http://localhost:8080/scrutiny")

//This testcase is unexpected and can cause issues. We need to ensure the apiEndpoint always has a trailing slash.
url1, _ := baseURL.Parse("d/e")
require.Equal(t, "http://localhost:8080/d/e", url1.String())

url2, _ := baseURL.Parse("/d/e")
require.Equal(t, "http://localhost:8080/d/e", url2.String())
}

func TestApiEndpointParse_WithBasepathWithTrailingSlash(t *testing.T) {
baseURL, _ := url.Parse("http://localhost:8080/scrutiny/")

url1, _ := baseURL.Parse("d/e")
require.Equal(t, "http://localhost:8080/scrutiny/d/e", url1.String())

url2, _ := baseURL.Parse("/d/e")
require.Equal(t, "http://localhost:8080/d/e", url2.String())
}
2 changes: 1 addition & 1 deletion docker/Dockerfile.web
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ COPY webapp/frontend /opt/scrutiny/src
RUN npm install -g @angular/[email protected] && \
mkdir -p /opt/scrutiny/dist && \
npm install && \
ng build --output-path=/opt/scrutiny/dist --deploy-url="/web/" --base-href="/web/" --prod
ng build --output-path=/opt/scrutiny/dist --prod


########
Expand Down
11 changes: 8 additions & 3 deletions example.scrutiny.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ web:
listen:
port: 8080
host: 0.0.0.0

# if you're using a reverse proxy like apache/nginx, you can override this value to serve scrutiny on a subpath.
# eg. http://example.com/scrutiny/* vs http://example.com:8080
# see docs/TROUBLESHOOTING_REVERSE_PROXY.md
# basepath: `/scrutiny`
# leave empty unless behind a path prefixed proxy
basepath: ''
database:
# can also set absolute path here
location: /opt/scrutiny/config/scrutiny.db
src:
# the location on the filesystem where scrutiny javascript + css is located
frontend:
path: /opt/scrutiny/web
# leave empty unless behind a path prefixed proxy
backend:
basepath: # if behind a path prefixed proxy set to path value with leading /
influxdb:
host: 0.0.0.0
port: 8086
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/analogj/go-util v0.0.0-20190301173314-5295e364eb14
github.com/citilinkru/libudev v1.0.0
github.com/containrrr/shoutrrr v0.4.4
github.com/fatih/color v1.10.0
github.com/gin-gonic/gin v1.6.3
Expand Down
2 changes: 1 addition & 1 deletion webapp/backend/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (c *configuration) Init() error {
//set defaults
c.SetDefault("web.listen.port", "8080")
c.SetDefault("web.listen.host", "0.0.0.0")
c.SetDefault("web.listen.basepath", "")
c.SetDefault("web.src.frontend.path", "/opt/scrutiny/web")
c.SetDefault("web.src.backend.basepath", "")
c.SetDefault("web.database.location", "/opt/scrutiny/config/scrutiny.db")

c.SetDefault("log.level", "INFO")
Expand Down
2 changes: 1 addition & 1 deletion webapp/backend/pkg/web/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func LoggerMiddleware(logger logrus.FieldLogger) gin.HandlerFunc {
entry.Info(msg)
}
}
if strings.HasPrefix(path, "/api/") {
if strings.Contains(path, "/api/") {
//only debug log request/response from api endpoint.
if len(reqBody) > 0 {
entry.WithField("bodyType", "request").Debugln(reqBody) // Print request body
Expand Down
17 changes: 9 additions & 8 deletions webapp/backend/pkg/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func (ae *AppEngine) Setup(logger logrus.FieldLogger) *gin.Engine {
r.Use(middleware.ConfigMiddleware(ae.Config))
r.Use(gin.Recovery())

basePath := ae.Config.GetString("web.src.backend.basepath")
basePath := ae.Config.GetString("web.listen.basepath")
logger.Debugf("basepath: %s", basePath)

base := r.Group(basePath)
{
api := base.Group("/api")
Expand All @@ -40,12 +41,12 @@ func (ae *AppEngine) Setup(logger logrus.FieldLogger) *gin.Engine {
})
api.POST("/health/notify", handler.SendTestNotification) //check if notifications are configured correctly

api.POST("/devices/register", handler.RegisterDevices) //used by Collector to register new devices and retrieve filtered list
api.GET("/summary", handler.GetDevicesSummary) //used by Dashboard
api.GET("/summary/temp", handler.GetDevicesSummaryTempHistory) //used by Dashboard (Temperature history dropdown)
api.POST("/device/:wwn/smart", handler.UploadDeviceMetrics) //used by Collector to upload data
api.POST("/device/:wwn/selftest", handler.UploadDeviceSelfTests)
api.GET("/device/:wwn/details", handler.GetDeviceDetails) //used by Details
api.POST("/devices/register", handler.RegisterDevices) //used by Collector to register new devices and retrieve filtered list
api.GET("/summary", handler.GetDevicesSummary) //used by Dashboard
api.GET("/summary/temp", handler.GetDevicesSummaryTempHistory) //used by Dashboard (Temperature history dropdown)
api.POST("/device/:wwn/smart", handler.UploadDeviceMetrics) //used by Collector to upload data
api.POST("/device/:wwn/selftest", handler.UploadDeviceSelfTests)
api.GET("/device/:wwn/details", handler.GetDeviceDetails) //used by Details
}
}

Expand All @@ -54,7 +55,7 @@ func (ae *AppEngine) Setup(logger logrus.FieldLogger) *gin.Engine {

//redirect base url to /web
base.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusFound, basePath + "/web")
c.Redirect(http.StatusFound, basePath+"/web")
})

//catch-all, serve index page.
Expand Down
Loading

0 comments on commit 381a679

Please sign in to comment.