style: apply structpack & goimport (#469)

* refactor: reorder import statements in fetch.go and fetch_test.go

Signed-off-by: Jason Cameron <git@jasoncameron.dev>

* fix: optimize struct field alignment to reduce memory usage

Signed-off-by: Jason Cameron <git@jasoncameron.dev>

---------

Signed-off-by: Jason Cameron <git@jasoncameron.dev>
This commit is contained in:
Jason Cameron 2025-05-09 12:54:15 -04:00 committed by GitHub
parent 8c7640aa09
commit 529f65674e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 60 additions and 61 deletions

View File

@ -158,12 +158,12 @@ func TestGetOGTagsWithHostConsideration(t *testing.T) {
} }
testCases := []struct { testCases := []struct {
name string name string
ogCacheConsiderHost bool requests []struct {
requests []struct {
host string host string
expectedLoadCount int // Expected load count *after* this request expectedLoadCount int
} }
ogCacheConsiderHost bool // Expected load count *after* this request
}{ }{
{ {
name: "Host Not Considered - Same Host", name: "Host Not Considered - Same Host",

View File

@ -4,12 +4,13 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"golang.org/x/net/html"
"io" "io"
"log/slog" "log/slog"
"mime" "mime"
"net" "net"
"net/http" "net/http"
"golang.org/x/net/html"
) )
var ( var (

View File

@ -2,7 +2,6 @@ package ogtags
import ( import (
"fmt" "fmt"
"golang.org/x/net/html"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -10,6 +9,8 @@ import (
"strings" "strings"
"testing" "testing"
"time" "time"
"golang.org/x/net/html"
) )
func TestFetchHTMLDocument(t *testing.T) { func TestFetchHTMLDocument(t *testing.T) {

View File

@ -56,10 +56,10 @@ func TestIntegrationGetOGTags(t *testing.T) {
// Test with different configurations // Test with different configurations
testCases := []struct { testCases := []struct {
expectedTags map[string]string
name string name string
path string path string
query string query string
expectedTags map[string]string
expectError bool expectError bool
}{ }{
{ {

View File

@ -20,12 +20,12 @@ const (
type OGTagCache struct { type OGTagCache struct {
cache *decaymap.Impl[string, map[string]string] cache *decaymap.Impl[string, map[string]string]
targetURL *url.URL targetURL *url.URL
ogCacheConsiderHost bool client *http.Client
ogPassthrough bool
ogTimeToLive time.Duration
approvedTags []string approvedTags []string
approvedPrefixes []string approvedPrefixes []string
client *http.Client ogTimeToLive time.Duration
ogCacheConsiderHost bool
ogPassthrough bool
} }
func NewOGTagCache(target string, ogPassthrough bool, ogTimeToLive time.Duration, ogTagsConsiderHost bool) *OGTagCache { func NewOGTagCache(target string, ogPassthrough bool, ogTimeToLive time.Duration, ogTagsConsiderHost bool) *OGTagCache {

View File

@ -18,9 +18,9 @@ func TestExtractOGTags(t *testing.T) {
testCache.approvedPrefixes = []string{"og:"} testCache.approvedPrefixes = []string{"og:"}
tests := []struct { tests := []struct {
expected map[string]string
name string name string
htmlStr string htmlStr string
expected map[string]string
}{ }{
{ {
name: "Basic OG tags", // Includes standard 'description' meta tag name: "Basic OG tags", // Includes standard 'description' meta tag

View File

@ -104,10 +104,11 @@ const (
type action string type action string
type testCase struct { type testCase struct {
name string name string
action action action action
isHard bool realIP string
realIP, userAgent string userAgent string
isHard bool
} }
func doesNPXExist(t *testing.T) { func doesNPXExist(t *testing.T) {

View File

@ -65,12 +65,12 @@ func TestXForwardedForUpdateAddToChain(t *testing.T) {
func TestComputeXFFHeader(t *testing.T) { func TestComputeXFFHeader(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
err error
name string name string
remoteAddr string remoteAddr string
origXFFHeader string origXFFHeader string
pref XFFComputePreferences
result string result string
err error pref XFFComputePreferences
}{ }{
{ {
name: "StripPrivate", name: "StripPrivate",

View File

@ -58,14 +58,14 @@ var (
) )
type Server struct { type Server struct {
mux *http.ServeMux
next http.Handler next http.Handler
priv ed25519.PrivateKey mux *http.ServeMux
pub ed25519.PublicKey
policy *policy.ParsedConfig policy *policy.ParsedConfig
opts Options
DNSBLCache *decaymap.Impl[string, dnsbl.DroneBLResponse] DNSBLCache *decaymap.Impl[string, dnsbl.DroneBLResponse]
OGTags *ogtags.OGTagCache OGTags *ogtags.OGTagCache
priv ed25519.PrivateKey
pub ed25519.PublicKey
opts Options
} }
func (s *Server) challengeFor(r *http.Request, difficulty int) string { func (s *Server) challengeFor(r *http.Request, difficulty int) string {
@ -232,8 +232,8 @@ func (s *Server) MakeChallenge(w http.ResponseWriter, r *http.Request) {
challenge := s.challengeFor(r, rule.Challenge.Difficulty) challenge := s.challengeFor(r, rule.Challenge.Difficulty)
err = encoder.Encode(struct { err = encoder.Encode(struct {
Challenge string `json:"challenge"`
Rules *config.ChallengeRules `json:"rules"` Rules *config.ChallengeRules `json:"rules"`
Challenge string `json:"challenge"`
}{ }{
Challenge: challenge, Challenge: challenge,
Rules: rule.Challenge, Rules: rule.Challenge,

View File

@ -23,24 +23,21 @@ import (
) )
type Options struct { type Options struct {
Next http.Handler Next http.Handler
Policy *policy.ParsedConfig Policy *policy.ParsedConfig
RedirectDomains []string Target string
ServeRobotsTXT bool CookieDomain string
PrivateKey ed25519.PrivateKey CookieName string
BasePrefix string
CookieExpiration time.Duration WebmasterEmail string
CookieDomain string RedirectDomains []string
CookieName string PrivateKey ed25519.PrivateKey
CookiePartitioned bool CookieExpiration time.Duration
OGPassthrough bool
OGTimeToLive time.Duration OGTimeToLive time.Duration
OGCacheConsidersHost bool OGCacheConsidersHost bool
Target string OGPassthrough bool
CookiePartitioned bool
WebmasterEmail string ServeRobotsTXT bool
BasePrefix string
} }
func LoadPoliciesOrDefault(fname string, defaultDifficulty int) (*policy.ParsedConfig, error) { func LoadPoliciesOrDefault(fname string, defaultDifficulty int) (*policy.ParsedConfig, error) {

View File

@ -8,10 +8,10 @@ import (
) )
type Bot struct { type Bot struct {
Rules Checker
Challenge *config.ChallengeRules
Name string Name string
Action config.Rule Action config.Rule
Challenge *config.ChallengeRules
Rules Checker
} }
func (b Bot) Hash() string { func (b Bot) Hash() string {

View File

@ -12,8 +12,8 @@ import (
) )
type CELChecker struct { type CELChecker struct {
src string
program cel.Program program cel.Program
src string
} }
func NewCELChecker(cfg *config.ExpressionOrList) (*CELChecker, error) { func NewCELChecker(cfg *config.ExpressionOrList) (*CELChecker, error) {

View File

@ -8,11 +8,11 @@ import (
func TestRemoteAddrChecker(t *testing.T) { func TestRemoteAddrChecker(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string
cidrs []string
ip string
ok bool
err error err error
name string
ip string
cidrs []string
ok bool
}{ }{
{ {
name: "match_ipv4", name: "match_ipv4",
@ -86,13 +86,13 @@ func TestRemoteAddrChecker(t *testing.T) {
func TestHeaderMatchesChecker(t *testing.T) { func TestHeaderMatchesChecker(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
err error
name string name string
header string header string
rexStr string rexStr string
reqHeaderKey string reqHeaderKey string
reqHeaderValue string reqHeaderValue string
ok bool ok bool
err error
}{ }{
{ {
name: "match", name: "match",

View File

@ -51,15 +51,14 @@ const (
) )
type BotConfig struct { type BotConfig struct {
Name string `json:"name"`
UserAgentRegex *string `json:"user_agent_regex"` UserAgentRegex *string `json:"user_agent_regex"`
PathRegex *string `json:"path_regex"` PathRegex *string `json:"path_regex"`
HeadersRegex map[string]string `json:"headers_regex"` HeadersRegex map[string]string `json:"headers_regex"`
RemoteAddr []string `json:"remote_addresses"`
Expression *ExpressionOrList `json:"expression"` Expression *ExpressionOrList `json:"expression"`
Challenge *ChallengeRules `json:"challenge,omitempty"`
Action Rule `json:"action"` Name string `json:"name"`
Challenge *ChallengeRules `json:"challenge,omitempty"` Action Rule `json:"action"`
RemoteAddr []string `json:"remote_addresses"`
} }
func (b BotConfig) Zero() bool { func (b BotConfig) Zero() bool {
@ -171,9 +170,9 @@ func (b BotConfig) Valid() error {
} }
type ChallengeRules struct { type ChallengeRules struct {
Algorithm Algorithm `json:"algorithm"`
Difficulty int `json:"difficulty"` Difficulty int `json:"difficulty"`
ReportAs int `json:"report_as"` ReportAs int `json:"report_as"`
Algorithm Algorithm `json:"algorithm"`
} }
var ( var (

View File

@ -15,9 +15,9 @@ func p[V any](v V) *V { return &v }
func TestBotValid(t *testing.T) { func TestBotValid(t *testing.T) {
var tests = []struct { var tests = []struct {
err error
name string name string
bot BotConfig bot BotConfig
err error
}{ }{
{ {
name: "simple user agent", name: "simple user agent",
@ -239,9 +239,9 @@ func TestConfigValidKnownGood(t *testing.T) {
func TestImportStatement(t *testing.T) { func TestImportStatement(t *testing.T) {
type testCase struct { type testCase struct {
err error
name string name string
importPath string importPath string
err error
} }
var tests []testCase var tests []testCase

View File

@ -8,11 +8,11 @@ import (
func TestExpressionOrListUnmarshal(t *testing.T) { func TestExpressionOrListUnmarshal(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
name string
inp string
err error err error
validErr error validErr error
result *ExpressionOrList result *ExpressionOrList
name string
inp string
}{ }{
{ {
name: "simple", name: "simple",

View File

@ -14,11 +14,11 @@ func TestJoin(t *testing.T) {
} }
for _, tt := range []struct { for _, tt := range []struct {
name string
clauses []string
op JoinOperator
err error err error
name string
op JoinOperator
resultStr string resultStr string
clauses []string
}{ }{
{ {
name: "no-clauses", name: "no-clauses",

View File

@ -12,8 +12,8 @@ func Base(title string, body templ.Component) templ.Component {
func BaseWithChallengeAndOGTags(title string, body templ.Component, challenge string, rules *config.ChallengeRules, ogTags map[string]string) (templ.Component, error) { func BaseWithChallengeAndOGTags(title string, body templ.Component, challenge string, rules *config.ChallengeRules, ogTags map[string]string) (templ.Component, error) {
return base(title, body, struct { return base(title, body, struct {
Challenge string `json:"challenge"`
Rules *config.ChallengeRules `json:"rules"` Rules *config.ChallengeRules `json:"rules"`
Challenge string `json:"challenge"`
}{ }{
Challenge: challenge, Challenge: challenge,
Rules: rules, Rules: rules,