refactor and typo fixes

This commit is contained in:
yusing
2024-11-02 03:14:47 +08:00
parent 76454df5e6
commit a86d316d07
34 changed files with 160 additions and 128 deletions

View File

@@ -5,8 +5,10 @@ import (
"net/http"
)
type ContentType string
type AcceptContentType []ContentType
type (
ContentType string
AcceptContentType []ContentType
)
func GetContentType(h http.Header) ContentType {
ct := h.Get("Content-Type")

View File

@@ -55,7 +55,6 @@ func New(cfg *Config) *LoadBalancer {
Logger: logger.With().Str("name", cfg.Link).Logger(),
Config: new(Config),
pool: newPool(),
task: task.DummyTask(),
}
lb.UpdateConfigIfNeeded(cfg)
return lb

View File

@@ -1,7 +1,7 @@
package loadbalancer
import (
U "github.com/yusing/go-proxy/internal/utils"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
type Mode string
@@ -14,7 +14,7 @@ const (
)
func (mode *Mode) ValidateUpdate() bool {
switch U.ToLowerNoSnake(string(*mode)) {
switch strutils.ToLowerNoSnake(string(*mode)) {
case "":
return true
case string(RoundRobin):

View File

@@ -109,9 +109,9 @@ func fetchUpdateCFIPRange(endpoint string, cfCIDRs []*types.CIDR) error {
_, cidr, err := net.ParseCIDR(line)
if err != nil {
return fmt.Errorf("cloudflare responeded an invalid CIDR: %s", line)
} else {
cfCIDRs = append(cfCIDRs, (*types.CIDR)(cidr))
}
cfCIDRs = append(cfCIDRs, (*types.CIDR)(cidr))
}
return nil

View File

@@ -19,24 +19,33 @@ import (
const errPagesBasePath = common.ErrorPagesBasePath
var (
setupMu sync.Mutex
dirWatcher W.Watcher
fileContentMap = F.NewMapOf[string, []byte]()
)
var setup = sync.OnceFunc(func() {
func setup() {
setupMu.Lock()
defer setupMu.Unlock()
if dirWatcher != nil {
return
}
task := task.GlobalTask("error page")
dirWatcher = W.NewDirectoryWatcher(task.Subtask("dir watcher"), errPagesBasePath)
loadContent()
go watchDir(task)
})
}
func GetStaticFile(filename string) ([]byte, bool) {
setup()
return fileContentMap.Load(filename)
}
// try <statusCode>.html -> 404.html -> not ok.
func GetErrorPageByStatus(statusCode int) (content []byte, ok bool) {
content, ok = fileContentMap.Load(fmt.Sprintf("%d.html", statusCode))
content, ok = GetStaticFile(fmt.Sprintf("%d.html", statusCode))
if !ok && statusCode != 404 {
return fileContentMap.Load("404.html")
}

View File

@@ -9,7 +9,6 @@ import (
"github.com/yusing/go-proxy/internal/common"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/utils"
U "github.com/yusing/go-proxy/internal/utils"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
@@ -21,7 +20,7 @@ var (
)
func Get(name string) (*Middleware, Error) {
middleware, ok := allMiddlewares[U.ToLowerNoSnake(name)]
middleware, ok := allMiddlewares[strutils.ToLowerNoSnake(name)]
if !ok {
return nil, ErrUnknownMiddleware.
Subject(name).
@@ -34,7 +33,7 @@ func All() map[string]*Middleware {
return allMiddlewares
}
// initialize middleware names and label parsers
// initialize middleware names and label parsers.
func init() {
allMiddlewares = map[string]*Middleware{
"setxforwarded": SetXForwarded,
@@ -67,7 +66,7 @@ func init() {
func LoadComposeFiles() {
errs := E.NewBuilder("middleware compile errors")
middlewareDefs, err := U.ListFiles(common.MiddlewareComposeBasePath, 0)
middlewareDefs, err := utils.ListFiles(common.MiddlewareComposeBasePath, 0)
if err != nil {
logger.Err(err).Msg("failed to list middleware definitions")
return
@@ -82,7 +81,7 @@ func LoadComposeFiles() {
errs.Add(ErrDuplicatedMiddleware.Subject(name))
continue
}
allMiddlewares[U.ToLowerNoSnake(name)] = m
allMiddlewares[strutils.ToLowerNoSnake(name)] = m
logger.Info().
Str("name", name).
Str("src", path.Base(defFile)).

View File

@@ -22,8 +22,10 @@ type Trace struct {
type Traces []*Trace
var traces = Traces{}
var tracesMu sync.Mutex
var (
traces = make(Traces, 0)
tracesMu sync.Mutex
)
const MaxTraceNum = 100

View File

@@ -10,18 +10,20 @@ import (
"net/http"
)
type ModifyResponseFunc func(*http.Response) error
type ModifyResponseWriter struct {
w http.ResponseWriter
r *http.Request
type (
ModifyResponseFunc func(*http.Response) error
ModifyResponseWriter struct {
w http.ResponseWriter
r *http.Request
headerSent bool
code int
headerSent bool
code int
modifier ModifyResponseFunc
modified bool
modifierErr error
}
modifier ModifyResponseFunc
modified bool
modifierErr error
}
)
func NewModifyResponseWriter(w http.ResponseWriter, r *http.Request, f ModifyResponseFunc) *ModifyResponseWriter {
return &ModifyResponseWriter{