Skip to content

Commit

Permalink
lookout webpack devserver!
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Shanley <dave@quobix.com>
  • Loading branch information
daveshanley committed Jul 3, 2023
1 parent 2bf0319 commit 51d877b
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 24 deletions.
50 changes: 38 additions & 12 deletions cmd/root_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"embed"
"github.com/mitchellh/mapstructure"
"net/url"
"os"

Expand Down Expand Up @@ -44,7 +45,7 @@ var (

cerr := viper.ReadInConfig()
if cerr != nil && configFlag != "" {
pterm.Warning.Printf("No wiretap configuration located. Using defaults: %s\n", cerr.Error())
pterm.Error.Printf("No wiretap configuration located. Using defaults: %s\n", cerr.Error())
}
if cerr != nil && configFlag == "" {
pterm.Info.Println("No wiretap configuration located. Using defaults.")
Expand All @@ -57,6 +58,9 @@ var (
var port string
var monitorPort string
var wsPort string
var staticPort string
var staticDir string
var pathConfigurations map[string]*shared.WiretapPathConfig
var redirectHost string
var redirectPort string
var redirectScheme string
Expand All @@ -81,6 +85,25 @@ var (
wsPort = viper.GetString("WEBSOCKET_PORT")
}

if viper.IsSet("STATIC_PORT") {
staticPort = viper.GetString("STATIC_PORT")
}

if viper.IsSet("STATIC_DIR") {
staticDir = viper.GetString("STATIC_DIR")
}

if viper.IsSet("PATHS") {
paths := viper.Get("PATHS")
var pc map[string]*shared.WiretapPathConfig
err := mapstructure.Decode(paths, &pc)
if err != nil {
pterm.Error.Printf("Unable to decode paths from configuration: %s\n", err.Error())
} else {
pathConfigurations = pc
}
}

if viper.IsSet("REDIRECT_URL") {
redirectURL = viper.GetString("REDIRECT_URL")
}
Expand Down Expand Up @@ -171,17 +194,20 @@ var (
}

config := shared.WiretapConfiguration{
Contract: spec,
RedirectURL: redirectURL,
RedirectHost: redirectHost,
RedirectBasePath: redirectBasePath,
RedirectPort: redirectPort,
RedirectProtocol: redirectScheme,
Port: port,
MonitorPort: monitorPort,
GlobalAPIDelay: globalAPIDelay,
WebSocketPort: wsPort,
FS: FS,
Contract: spec,
RedirectURL: redirectURL,
RedirectHost: redirectHost,
RedirectBasePath: redirectBasePath,
RedirectPort: redirectPort,
RedirectProtocol: redirectScheme,
Port: port,
MonitorPort: monitorPort,
GlobalAPIDelay: globalAPIDelay,
WebSocketPort: wsPort,
StaticDir: staticDir,
StaticPort: staticPort,
PathConfigurations: pathConfigurations,
FS: FS,
}

// ready to boot, let's go!
Expand Down
18 changes: 18 additions & 0 deletions config/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT

package config

import (
"github.com/pb33f/wiretap/shared"
)

func FindPath(path string, configuration *shared.WiretapConfiguration) []*shared.WiretapPathConfig {
var foundConfigurations []*shared.WiretapPathConfig
for key := range configuration.CompiledPaths {
if configuration.CompiledPaths[key].CompiledKey.Match(path) {
foundConfigurations = append(foundConfigurations, configuration.CompiledPaths[key].PathConfig)
}
}
return foundConfigurations
}
51 changes: 51 additions & 0 deletions config/paths_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT

package config

import (
"github.com/mitchellh/mapstructure"
"github.com/pb33f/wiretap/shared"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)

func TestFindPath(t *testing.T) {

config := `
paths:
/pb33f/test/**:
target: /
secure: false
pathRewrite:
'^/pb33f/test/': ''`

viper.SetConfigType("yaml")
verr := viper.ReadConfig(strings.NewReader(config))
assert.NoError(t, verr)

paths := viper.Get("paths")
var pc map[string]*shared.WiretapPathConfig

derr := mapstructure.Decode(paths, &pc)
assert.NoError(t, derr)

wcConfig := &shared.WiretapConfiguration{
PathConfigurations: pc,
}

// compile paths
wcConfig.CompilePaths()

res := FindPath("/pb33f/test/123", wcConfig)
assert.Len(t, res, 1)

res = FindPath("/pb33f/test/123/sing/song", wcConfig)
assert.Len(t, res, 1)

res = FindPath("/pb33f/no-match/wrong", wcConfig)
assert.Len(t, res, 0)

}
59 changes: 47 additions & 12 deletions shared/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,55 @@

package shared

import "embed"
import (
"embed"
"github.com/gobwas/glob"
)

type WiretapConfiguration struct {
Contract string `json:"-"`
RedirectHost string `json:"redirectHost,omitempty"`
RedirectPort string `json:"redirectPort,omitempty"`
RedirectBasePath string `json:"redirectBasePath,omitempty"`
RedirectProtocol string `json:"redirectProtocol,omitempty"`
RedirectURL string `json:"redirectURL,omitempty"`
Port string `json:"port,omitempty"`
MonitorPort string `json:"monitorPort,omitempty"`
WebSocketPort string `json:"webSocketPort,omitempty"`
GlobalAPIDelay int `json:"globalAPIDelay,omitempty"`
FS embed.FS `json:"-"`
Contract string `json:"-"`
RedirectHost string `json:"redirectHost,omitempty"`
RedirectPort string `json:"redirectPort,omitempty"`
RedirectBasePath string `json:"redirectBasePath,omitempty"`
RedirectProtocol string `json:"redirectProtocol,omitempty"`
RedirectURL string `json:"redirectURL,omitempty"`
Port string `json:"port,omitempty"`
MonitorPort string `json:"monitorPort,omitempty"`
WebSocketPort string `json:"webSocketPort,omitempty"`
GlobalAPIDelay int `json:"globalAPIDelay,omitempty"`
StaticDir string `json:"staticDir,omitempty"`
StaticPort string `json:"staticPort,omitempty"`
PathConfigurations map[string]*WiretapPathConfig `json:"paths,omitempty"`
CompiledPaths map[string]*CompiledPath `json:"-"`
FS embed.FS `json:"-"`
}

func (wtc *WiretapConfiguration) CompilePaths() {
wtc.CompiledPaths = make(map[string]*CompiledPath)
for x := range wtc.PathConfigurations {
wtc.CompiledPaths[x] = wtc.PathConfigurations[x].Compile(x)
}
}

type WiretapPathConfig struct {
Target string `json:"target,omitempty"`
PathRewrite map[string]string `json:"pathRewrite,omitempty"`
Secure bool `json:"secure,omitempty"`
}

type CompiledPath struct {
PathConfig *WiretapPathConfig
CompiledKey glob.Glob
CompiledTarget glob.Glob
}

func (wpc *WiretapPathConfig) Compile(key string) *CompiledPath {
cp := &CompiledPath{
PathConfig: wpc,
CompiledKey: glob.MustCompile(key),
CompiledTarget: glob.MustCompile(wpc.Target),
}
return cp
}

const ConfigKey = "config"
Expand Down

0 comments on commit 51d877b

Please sign in to comment.