Skip to content

Commit

Permalink
Updated codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Sep 22, 2024
1 parent 877e58b commit a3976c8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 64 deletions.
4 changes: 1 addition & 3 deletions internal/handler/proxy/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"github.com/evg4b/uncors/internal/infra"
)

func (h *Handler) makeOptionsResponse(writer http.ResponseWriter, req *http.Request) error {
func (h *Handler) makeOptionsResponse(writer http.ResponseWriter, req *http.Request) {
infra.WriteCorsHeaders(writer.Header())
tui.PrintResponse(h.logger, req, http.StatusOK)

return nil
}
2 changes: 1 addition & 1 deletion internal/tui/box-message.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func printMessageBox(out io.Writer, message, prefix string, block lipgloss.Style
height := lipgloss.Height(message)
space := strings.Repeat("\n", height-1)

block = block.Copy().Margin(0, 1, 0, 0)
block = block.Margin(0, 1, 0, 0)

_, err := fmt.Fprintln(out, lipgloss.JoinHorizontal( //nolint:forbidigo
lipgloss.Top,
Expand Down
3 changes: 1 addition & 2 deletions internal/tui/printresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const prefixWidth = 12
func printResponse(request *contracts.Request, statusCode int) string {
prefix := helpers.Sprintf("%d %s", statusCode, request.Method)
prefixStyle, textStyle := getPrefixPrinter(statusCode)
prefixStyle = prefixStyle.Copy().
Width(prefixWidth)
prefixStyle = prefixStyle.Width(prefixWidth)

return fmt.Sprintf("%s %s", prefixStyle.Render(prefix), textStyle.Render(request.URL.String()))
}
Expand Down
15 changes: 4 additions & 11 deletions internal/tui/styles/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ var baseBlock = lipgloss.NewStyle().
ColorWhitespace(true)

var (
ProxyStyle = baseBlock.Copy().
Background(lipgloss.Color("#6a71f7"))

MockStyle = baseBlock.Copy().
Background(lipgloss.Color("#ee7ff8"))

StaticStyle = baseBlock.Copy().
Background(lipgloss.Color("#ffffff"))

CacheStyle = baseBlock.Copy().
Background(lipgloss.Color("#fefc7f"))
ProxyStyle = baseBlock.Background(lipgloss.Color("#6a71f7"))
MockStyle = baseBlock.Background(lipgloss.Color("#ee7ff8"))
StaticStyle = baseBlock.Background(lipgloss.Color("#ffffff"))
CacheStyle = baseBlock.Background(lipgloss.Color("#fefc7f"))
)
40 changes: 19 additions & 21 deletions internal/tui/styles/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ var noLevel = log.Level(math.MaxInt32)
var (
boxLength = 8

debugPrefix = DebugBlock.Copy().Width(boxLength)
infoPrefix = InfoBlock.Copy().Width(boxLength)
warnPrefix = WarningBlock.Copy().Width(boxLength)
errorPrefix = ErrorBlock.Copy().Width(boxLength)
debugPrefix = DebugBlock.Width(boxLength)
infoPrefix = InfoBlock.Width(boxLength)
warnPrefix = WarningBlock.Width(boxLength)
errorPrefix = ErrorBlock.Width(boxLength)

DefaultStyles = log.Styles{
Timestamp: lipgloss.NewStyle(),
Expand All @@ -27,19 +27,19 @@ var (
Separator: lipgloss.NewStyle().Faint(true),
Levels: map[log.Level]lipgloss.Style{
noLevel: lipgloss.NewStyle().Margin(0).Padding(0),
log.DebugLevel: DebugText.Copy().
log.DebugLevel: DebugText.
SetString(debugPrefix.Render(DebugLabel)).
Bold(true),
log.InfoLevel: InfoText.Copy().
log.InfoLevel: InfoText.
SetString(infoPrefix.Render(InfoLabel)).
Bold(true),
log.WarnLevel: WarningText.Copy().
log.WarnLevel: WarningText.
SetString(warnPrefix.Render(WarningLabel)).
Bold(true),
log.ErrorLevel: ErrorText.Copy().
log.ErrorLevel: ErrorText.
SetString(errorPrefix.Render(ErrorLabel)).
Bold(true),
log.FatalLevel: ErrorText.Copy().
log.FatalLevel: ErrorText.
SetString(errorPrefix.Render(FatalLabel)).
Bold(true),
},
Expand All @@ -50,25 +50,23 @@ var (

func CreateLogger(logger *log.Logger, prefix string) *log.Logger {
newStyles := log.Styles{
Timestamp: DefaultStyles.Timestamp.Copy(),
Caller: DefaultStyles.Caller.Copy(),
Prefix: DefaultStyles.Prefix.Copy(),
Message: DefaultStyles.Message.Copy(),
Key: DefaultStyles.Key.Copy(),
Value: DefaultStyles.Value.Copy(),
Separator: DefaultStyles.Separator.Copy(),
Timestamp: DefaultStyles.Timestamp,
Caller: DefaultStyles.Caller,
Prefix: DefaultStyles.Prefix,
Message: DefaultStyles.Message,
Key: DefaultStyles.Key,
Value: DefaultStyles.Value,
Separator: DefaultStyles.Separator,
Levels: make(map[log.Level]lipgloss.Style, len(DefaultStyles.Levels)),
Keys: make(map[string]lipgloss.Style, len(DefaultStyles.Keys)),
Values: make(map[string]lipgloss.Style, len(DefaultStyles.Values)),
}

for level, style := range DefaultStyles.Levels {
if level == noLevel {
newStyles.Levels[level] = style.Copy().
SetString(prefix + style.Value())
newStyles.Levels[level] = style.SetString(prefix + style.Value())
} else {
newStyles.Levels[level] = style.Copy().
SetString(prefix, style.Value())
newStyles.Levels[level] = style.SetString(prefix, style.Value())
}
}

Expand All @@ -83,6 +81,6 @@ func CreateLogger(logger *log.Logger, prefix string) *log.Logger {

func copyMap(source, dest map[string]lipgloss.Style) {
for key, value := range source {
dest[key] = value.Copy()
dest[key] = value
}
}
36 changes: 18 additions & 18 deletions internal/tui/styles/statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ type StatusStyle struct {
}

var InformationalStyle = StatusStyle{
BlockStyle: InfoBlock.Copy(),
MainTextStyle: InfoText.Copy(),
SecondaryTextStyle: DisabledText.Copy(),
BlockStyle: InfoBlock,
MainTextStyle: InfoText,
SecondaryTextStyle: DisabledText,
}

var SuccessStyle = StatusStyle{
BlockStyle: SuccessBlock.Copy(),
MainTextStyle: SuccessText.Copy(),
SecondaryTextStyle: DisabledText.Copy(),
BlockStyle: SuccessBlock,
MainTextStyle: SuccessText,
SecondaryTextStyle: DisabledText,
}

var RedirectionStyle = StatusStyle{
BlockStyle: WarningBlock.Copy(),
MainTextStyle: WarningText.Copy(),
SecondaryTextStyle: DisabledText.Copy(),
BlockStyle: WarningBlock,
MainTextStyle: WarningText,
SecondaryTextStyle: DisabledText,
}

var ClientErrorStyle = StatusStyle{
BlockStyle: ErrorBlock.Copy(),
MainTextStyle: ErrorText.Copy(),
SecondaryTextStyle: DisabledText.Copy(),
BlockStyle: ErrorBlock,
MainTextStyle: ErrorText,
SecondaryTextStyle: DisabledText,
}

var ServerErrorStyle = ClientErrorStyle

var CanceledStyle = StatusStyle{
BlockStyle: DisabledBlock.Copy(),
MainTextStyle: DisabledText.Copy().Strikethrough(true),
SecondaryTextStyle: DisabledText.Copy().Strikethrough(true),
BlockStyle: DisabledBlock,
MainTextStyle: DisabledText.Strikethrough(true),
SecondaryTextStyle: DisabledText.Strikethrough(true),
}

var PendingStyle = StatusStyle{
BlockStyle: DisabledBlock.Copy(),
MainTextStyle: DisabledText.Copy(),
SecondaryTextStyle: DisabledText.Copy(),
BlockStyle: DisabledBlock,
MainTextStyle: DisabledText,
SecondaryTextStyle: DisabledText,
}
18 changes: 10 additions & 8 deletions testing/testutils/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ type Certs struct {
KeyPath string
}

const keyBits = 4096

func WithTmpCerts(fs afero.Fs, action func(t *testing.T, certs *Certs)) func(t *testing.T) {
if fs == nil {
fs = afero.NewOsFs()
Expand All @@ -38,6 +36,8 @@ func WithTmpCerts(fs afero.Fs, action func(t *testing.T, certs *Certs)) func(t *
}
}

var localIPAddress = net.IPv4(127, 0, 0, 1) //nolint:mnd

func certSetup(t *testing.T, fs afero.Fs) *Certs {
t.Helper()

Expand All @@ -64,7 +64,8 @@ func certSetup(t *testing.T, fs afero.Fs) *Certs {
BasicConstraintsValid: true,
}

caPrivateKey, err := rsa.GenerateKey(rand.Reader, keyBits)
const keySize = 4096
caPrivateKey, err := rsa.GenerateKey(rand.Reader, keySize)
CheckNoError(t, err)

caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivateKey.PublicKey, caPrivateKey)
Expand All @@ -77,10 +78,11 @@ func certSetup(t *testing.T, fs afero.Fs) *Certs {
})
CheckNoError(t, err)

const serialNumber = 2024
const certValidity = 10
// set up our server certificate
const certYear = 2019
cert := &x509.Certificate{
SerialNumber: big.NewInt(certYear),
SerialNumber: big.NewInt(serialNumber),
Subject: pkix.Name{
Organization: []string{"Company, INC."},
Country: []string{"US"},
Expand All @@ -89,15 +91,15 @@ func certSetup(t *testing.T, fs afero.Fs) *Certs {
StreetAddress: []string{"Golden Gate Bridge"},
PostalCode: []string{"94016"},
},
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback}, // nolint: mnd
IPAddresses: []net.IP{localIPAddress, net.IPv6loopback},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0), // nolint: mnd
NotAfter: time.Now().AddDate(certValidity, 0, 0),
SubjectKeyId: []byte{1, 2, 3, 4, 6},
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}

certPrivateKey, err := rsa.GenerateKey(rand.Reader, keyBits)
certPrivateKey, err := rsa.GenerateKey(rand.Reader, keySize)
CheckNoError(t, err)

certBytes, err := x509.CreateCertificate(rand.Reader, cert, ca, &certPrivateKey.PublicKey, caPrivateKey)
Expand Down

0 comments on commit a3976c8

Please sign in to comment.