Skip to content

Commit

Permalink
Allow configuring listen port
Browse files Browse the repository at this point in the history
  • Loading branch information
byo committed Nov 19, 2023
1 parent 4a98d10 commit 9344115
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 31 deletions.
32 changes: 20 additions & 12 deletions pkg/cmd/cinode_web_proxy/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import (
"os"
"runtime"
"sort"
"strconv"
"strings"
"time"

"github.com/cinode/go/pkg/blenc"
"github.com/cinode/go/pkg/cinodefs"
"github.com/cinode/go/pkg/cinodefs/httphandler"
"github.com/cinode/go/pkg/datastore"
"github.com/cinode/go/pkg/utilities/golang"
"github.com/cinode/go/pkg/utilities/httpserver"
"golang.org/x/exp/slog"
)
Expand Down Expand Up @@ -79,10 +81,7 @@ func executeWithConfig(ctx context.Context, cfg *config) error {
"cpus", runtime.NumCPU(),
)

handler, err := setupCinodeProxy(ctx, mainDS, additionalDSs, entrypoint)
if err != nil {
return err
}
handler := setupCinodeProxy(ctx, mainDS, additionalDSs, entrypoint)

return httpserver.RunGracefully(ctx,
handler,
Expand All @@ -96,8 +95,8 @@ func setupCinodeProxy(
mainDS datastore.DS,
additionalDSs []datastore.DS,
entrypoint *cinodefs.Entrypoint,
) (http.Handler, error) {
fs, err := cinodefs.New(
) http.Handler {
fs := golang.Must(cinodefs.New(
ctx,
blenc.FromDatastore(
datastore.NewMultiSource(
Expand All @@ -108,16 +107,13 @@ func setupCinodeProxy(
),
cinodefs.RootEntrypoint(entrypoint),
cinodefs.MaxLinkRedirects(10),
)
if err != nil {
return nil, err
}
))

return &httphandler.Handler{
FS: fs,
IndexFile: "index.html",
Log: slog.Default(),
}, nil
}
}

type config struct {
Expand Down Expand Up @@ -163,7 +159,19 @@ func getConfig() (*config, error) {
cfg.additionalDSLocations = append(cfg.additionalDSLocations, location)
}

cfg.port = 8080
port := os.Getenv("CINODE_LISTEN_PORT")
if port == "" {
cfg.port = 8080
} else {
portNum, err := strconv.Atoi(port)
if err == nil && (portNum < 1 || portNum > 65535) {
err = fmt.Errorf("not in range 1..65535")
}
if err != nil {
return nil, fmt.Errorf("invalid listen port %s: %w", port, err)
}
cfg.port = portNum
}

return &cfg, nil
}
25 changes: 21 additions & 4 deletions pkg/cmd/cinode_web_proxy/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ func TestGetConfig(t *testing.T) {
"additional3",
})
})

t.Run("set listen port", func(t *testing.T) {
t.Setenv("CINODE_LISTEN_PORT", "12345")
cfg, err := getConfig()
require.NoError(t, err)
require.Equal(t, 12345, cfg.port)
})

t.Run("invalid port - not a number", func(t *testing.T) {
t.Setenv("CINODE_LISTEN_PORT", "123-45")
_, err := getConfig()
require.ErrorContains(t, err, "invalid listen port")
})

t.Run("invalid port - outside range", func(t *testing.T) {
t.Setenv("CINODE_LISTEN_PORT", "-1")
_, err := getConfig()
require.ErrorContains(t, err, "invalid listen port")
})
}

func TestWebProxyHandlerInvalidEntrypoint(t *testing.T) {
Expand All @@ -114,13 +133,12 @@ func TestWebProxyHandlerInvalidEntrypoint(t *testing.T) {

key := cipherfactory.NewKeyGenerator(blobtypes.Static).Generate()

handler, err := setupCinodeProxy(
handler := setupCinodeProxy(
context.Background(),
datastore.InMemory(),
[]datastore.DS{},
cinodefs.EntrypointFromBlobNameAndKey(n, key),
)
require.NoError(t, err)

server := httptest.NewServer(handler)
defer server.Close()
Expand Down Expand Up @@ -179,8 +197,7 @@ func TestWebProxyHandlerSimplePage(t *testing.T) {
return ep
}()

handler, err := setupCinodeProxy(context.Background(), ds, []datastore.DS{}, ep)
require.NoError(t, err)
handler := setupCinodeProxy(context.Background(), ds, []datastore.DS{}, ep)

server := httptest.NewServer(handler)
defer server.Close()
Expand Down
30 changes: 24 additions & 6 deletions pkg/cmd/public_node/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"runtime"
"sort"
"strconv"
"strings"
"time"

Expand All @@ -34,10 +35,14 @@ import (
)

func Execute(ctx context.Context) error {
return executeWithConfig(ctx, getConfig())
cfg, err := getConfig()
if err != nil {
return err
}
return executeWithConfig(ctx, cfg)
}

func executeWithConfig(ctx context.Context, cfg config) error {
func executeWithConfig(ctx context.Context, cfg *config) error {
handler, err := buildHttpHandler(cfg)
if err != nil {
return err
Expand All @@ -60,7 +65,7 @@ func executeWithConfig(ctx context.Context, cfg config) error {
)
}

func buildHttpHandler(cfg config) (http.Handler, error) {
func buildHttpHandler(cfg *config) (http.Handler, error) {
mainDS, err := datastore.FromLocation(cfg.mainDSLocation)
if err != nil {
return nil, fmt.Errorf("could not create main datastore: %w", err)
Expand Down Expand Up @@ -140,7 +145,7 @@ type config struct {
uploadPassword string
}

func getConfig() config {
func getConfig() (*config, error) {
cfg := config{
log: slog.Default(),
}
Expand All @@ -164,9 +169,22 @@ func getConfig() config {
cfg.additionalDSLocations = append(cfg.additionalDSLocations, location)
}

cfg.port = 8080
port := os.Getenv("CINODE_LISTEN_PORT")
if port == "" {
cfg.port = 8080
} else {
portNum, err := strconv.Atoi(port)
if err == nil && (portNum < 1 || portNum > 65535) {
err = fmt.Errorf("not in range 1..65535")
}
if err != nil {
return nil, fmt.Errorf("invalid listen port %s: %w", port, err)
}
cfg.port = portNum
}

cfg.uploadUsername = os.Getenv("CINODE_UPLOAD_USERNAME")
cfg.uploadPassword = os.Getenv("CINODE_UPLOAD_PASSWORD")

return cfg
return &cfg, nil
}
46 changes: 37 additions & 9 deletions pkg/cmd/public_node/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ func TestGetConfig(t *testing.T) {
os.Clearenv()

t.Run("default config", func(t *testing.T) {
cfg := getConfig()
cfg, err := getConfig()
require.NoError(t, err)
require.Equal(t, "memory://", cfg.mainDSLocation)
require.Empty(t, cfg.additionalDSLocations)
require.Equal(t, 8080, cfg.port)
})

t.Run("set main datastore", func(t *testing.T) {
t.Setenv("CINODE_MAIN_DATASTORE", "testdatastore")
cfg := getConfig()
cfg, err := getConfig()
require.NoError(t, err)
require.Equal(t, cfg.mainDSLocation, "testdatastore")
})

Expand All @@ -50,19 +52,39 @@ func TestGetConfig(t *testing.T) {
t.Setenv("CINODE_ADDITIONAL_DATASTORE_2", "additional2")
t.Setenv("CINODE_ADDITIONAL_DATASTORE_1", "additional1")

cfg := getConfig()
cfg, err := getConfig()
require.NoError(t, err)
require.Equal(t, cfg.additionalDSLocations, []string{
"additional",
"additional1",
"additional2",
"additional3",
})
})

t.Run("set listen port", func(t *testing.T) {
t.Setenv("CINODE_LISTEN_PORT", "12345")
cfg, err := getConfig()
require.NoError(t, err)
require.Equal(t, 12345, cfg.port)
})

t.Run("invalid port - not a number", func(t *testing.T) {
t.Setenv("CINODE_LISTEN_PORT", "123-45")
_, err := getConfig()
require.ErrorContains(t, err, "invalid listen port")
})

t.Run("invalid port - outside range", func(t *testing.T) {
t.Setenv("CINODE_LISTEN_PORT", "-1")
_, err := getConfig()
require.ErrorContains(t, err, "invalid listen port")
})
}

func TestBuildHttpHandler(t *testing.T) {
t.Run("Successfully created handler", func(t *testing.T) {
h, err := buildHttpHandler(config{
h, err := buildHttpHandler(&config{
mainDSLocation: t.TempDir(),
additionalDSLocations: []string{
t.TempDir(),
Expand Down Expand Up @@ -92,7 +114,7 @@ func TestBuildHttpHandler(t *testing.T) {
const VALID_PASSWORD = "secret"
const INVALID_PASSWORD = "plaintext"

h, err := buildHttpHandler(config{
h, err := buildHttpHandler(&config{
mainDSLocation: t.TempDir(),
additionalDSLocations: []string{
t.TempDir(),
Expand Down Expand Up @@ -128,15 +150,15 @@ func TestBuildHttpHandler(t *testing.T) {
})

t.Run("invalid main datastore", func(t *testing.T) {
h, err := buildHttpHandler(config{
h, err := buildHttpHandler(&config{
mainDSLocation: "",
})
require.ErrorContains(t, err, "could not create main datastore")
require.Nil(t, h)
})

t.Run("invalid additional datastore", func(t *testing.T) {
h, err := buildHttpHandler(config{
h, err := buildHttpHandler(&config{
mainDSLocation: "memory://",
additionalDSLocations: []string{""},
})
Expand All @@ -152,15 +174,15 @@ func TestExecuteWithConfig(t *testing.T) {
time.Sleep(10 * time.Millisecond)
cancel()
}()
err := executeWithConfig(ctx, config{
err := executeWithConfig(ctx, &config{
mainDSLocation: "memory://",
log: slog.Default(),
})
require.NoError(t, err)
})

t.Run("invalid configuration", func(t *testing.T) {
err := executeWithConfig(context.Background(), config{})
err := executeWithConfig(context.Background(), &config{})
require.ErrorContains(t, err, "datastore")
})
}
Expand All @@ -181,4 +203,10 @@ func TestExecute(t *testing.T) {
err := Execute(context.Background())
require.ErrorContains(t, err, "datastore")
})

t.Run("invalid configuration - port", func(t *testing.T) {
t.Setenv("CINODE_LISTEN_PORT", "-1")
err := Execute(context.Background())
require.ErrorContains(t, err, "listen port")
})
}
16 changes: 16 additions & 0 deletions pkg/utilities/golang/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package golang

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestAssert(t *testing.T) {
require.NotPanics(t, func() {
Assert(true, "must not happen")
})
require.Panics(t, func() {
Assert(false, "must panic")
})
}

0 comments on commit 9344115

Please sign in to comment.