Skip to content

Commit

Permalink
fix(cspp): Fix PORT and BASE_URL behavior
Browse files Browse the repository at this point in the history
Originally, if `CSPP_PORT` and the port found in `CSPP_BASE_URL` weren't the
same, it would service on `CSPP_PORT` but link out and log with
`CSPP_BASE_URL`. THis was bad. To fix this, we now honor the port found in
`CSPP_BASE_URL` at all times, unless it's not set (e.g. it's just a string like
`http://example.com/cspp`) and `CSPP_PORT` is set. Then it will use `CSPP_PORT`
for all things. If neither is specified, it's just port 8080.
  • Loading branch information
Michael Stahnke committed Feb 11, 2024
1 parent 7bb8b8e commit 44afcba
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cspp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ You need to set the following enviornment variables.
| `CSPP_UPLOADS_DIR` | directory to save images | optional | `/var/lib/cspp/uploads` | `./data/uploads` |
| `CSPP_CREDENTIALS_DIR`| directory to save API keys as json blobs | optional | `/var/lib/cspp/credentials` | `./data/credentials` |

:warning: If you specifiy `CSPP_BASE_URL` with a port on the string and specify `CSPP_PORT` and they do not match, you may get unpredictable results
:warning: How do ports work?

If you specify a port via `CSPP_PORT` and `CSPP_BASE_URL` the one found in `CSPP_BASE_URL` will be used. If you don't specify a port in `CSPP_BASE_URL` the one found in `CSPP_PORT` will be used. If neither is specified, the default port `8080` will be used.

## Slack Specifics

Expand Down
27 changes: 27 additions & 0 deletions cspp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/url"
"os"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -66,6 +67,32 @@ func init() {
setupDirectory(viper.GetString("processed_dir"))
setupDirectory(viper.GetString("uploads_dir"))
setupDirectory(viper.GetString("credentials_dir"))

validatePortVsBaseURL()

}

func validatePortVsBaseURL() {
log.Debugln("validatePortVsBaseURL")
baseurl := viper.GetString("base_url")
port := viper.GetString("port")
if baseurl != "" && port != "" {
parsedURL, err := url.Parse(baseurl)
if err != nil {
log.Errorln("Error parsing base URL:", err)
os.Exit(1)
}
baseport := parsedURL.Port()
if baseport == "" && port != "" {
return
}
if baseport != port {
viper.Set("port", baseport)
if port != "8080" {
log.Infoln("CSPP_PORT overridden by value specified in CSPP_BASE_URL.")
}
}
}
}

func main() {
Expand Down
79 changes: 79 additions & 0 deletions cspp/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"testing"

"github.com/spf13/viper"
)

// MockLogger is a mock logger for testing purposes
type MockLogger struct{}

func (l *MockLogger) Debugln(args ...interface{}) {}
func (l *MockLogger) Errorln(args ...interface{}) {}

func TestValidatePortVsBaseURL(t *testing.T) {
// Mock configuration
viper.Set("base_url", "http://example.com:8080")
viper.Set("port", "8081")

validatePortVsBaseURL()

if port := viper.GetString("port"); port != "8080" {
t.Errorf("Expected port to be set to 8080, got %s", port)
}
}

func TestValidatePortVsBaseURL_NoBaseURL(t *testing.T) {
// Mock configuration
viper.Set("base_url", "")
viper.Set("port", "8081")

validatePortVsBaseURL()

if port := viper.GetString("port"); port != "8081" {
t.Errorf("Expected port to remain unchanged, got %s", port)
}
}

func TestValidatePortVsBaseURL_InvalidBaseURL(t *testing.T) {
// Mock configuration
viper.Set("base_url", "invalid-url")
viper.Set("port", "8081")

validatePortVsBaseURL()

// Expect the error message to be logged
}

func TestValidatePortVsBaseURL_BaseURLWithoutPort(t *testing.T) {
// Mock configuration
viper.Set("base_url", "http://example.com")
viper.Set("port", "8081")

validatePortVsBaseURL()

if port := viper.GetString("port"); port != "8081" {
t.Errorf("Expected port to remain unchanged, got %s", port)
}
}

func TestValidatePortVsBaseURL_Port8080(t *testing.T) {
// Mock configuration
viper.Set("base_url", "http://example.com:8080")
viper.Set("port", "8080")

validatePortVsBaseURL()

// Expect no message to be logged
}

func TestValidatePortVsBaseURL_CustomPort(t *testing.T) {
// Mock configuration
viper.Set("base_url", "http://example.com:9000")
viper.Set("port", "8081")

validatePortVsBaseURL()

// Expect the overridden message to be logged
}

0 comments on commit 44afcba

Please sign in to comment.