Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port and Test work #55

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspp/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
cspp
9 changes: 9 additions & 0 deletions cspp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,14 @@ install: build

platforms: mac linux

test: fmt
go test -v .

tests: test


apirequest:
curl -X POST -H "Content-Type: application/json" -d @fixtures/api_request.json http://localhost:7171/api


.PHONY: all clean fmt tidy build install test tests apirequest stuff platforms mac linux darwin-arm64 darwin-amd64 linux-arm64 linux-amd64 mac-arm64 mac-amd64
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
}
Loading