From 76aa976e98cc5ded609884f7ce42612b30ade249 Mon Sep 17 00:00:00 2001 From: Eray Ates Date: Sat, 7 Sep 2024 18:49:43 +0000 Subject: [PATCH] fix: linting update Signed-off-by: Eray Ates --- .golangci.yml | 30 ++++++++++++------------------ Makefile | 2 +- README.md | 4 ++-- cmd/forward/args/root.go | 4 ++-- cmd/forward/main.go | 1 - internal/handler/methods.go | 1 + internal/handler/methods_test.go | 3 +++ internal/handler/socket.go | 3 +-- internal/server/http.go | 5 ++++- internal/server/parser.go | 3 +++ internal/server/parser_test.go | 14 +++++++++----- 11 files changed, 38 insertions(+), 32 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index fc0b555..6068536 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,19 +1,13 @@ -# Refer to golangci-lint's example config file for more options and information: -# https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml - -run: - timeout: 5m - modules-download-mode: readonly - linters: - enable: - - errcheck - - goimports - - golint - - govet - - staticcheck - -issues: - exclude-use-default: false - max-issues-per-linter: 0 - max-same-issues: 0 + enable-all: true + disable: + - depguard + - prealloc + - lll + - nlreturn + - funlen + - mnd + - gochecknoinits + - testpackage + - gomnd # Deprecated + fast: true diff --git a/Makefile b/Makefile index baa3bc6..21f6730 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ build: ## Build the binary lint: ## Lint Go files golangci-lint --version - golangci-lint run ./... + GOPATH="$(shell dirname $(PWD))" golangci-lint run ./... test: ## Run unit tests @go test -race ./... diff --git a/README.md b/README.md index a437c4d..f521a14 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![License](https://img.shields.io/github/license/worldline-go/forward?color=red&style=flat-square)](https://raw.githubusercontent.com/worldline-go/forward/main/LICENSE) [![Coverage](https://img.shields.io/sonar/coverage/worldline-go_forward?logo=sonarcloud&server=https%3A%2F%2Fsonarcloud.io&style=flat-square)](https://sonarcloud.io/summary/overall?id=worldline-go_forward) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/worldline-go/forward/Test?logo=github&style=flat-square&label=ci)](https://github.com/worldline-go/forward/actions) -[![Go Reference](https://pkg.go.dev/badge/github.com/worldline-go/forward.svg)](https://pkg.go.dev/github.com/worldline-go/forward) +[![Go PKG](https://raw.githubusercontent.com/worldline-go/guide/main/badge/custom/reference.svg)](https://pkg.go.dev/github.com/worldline-go/forward) Export socket connection to HTTP service with filter options. @@ -48,7 +48,7 @@ sudo ./forward -s '/var/run/docker.sock:/:-POST,-PUT,-DELETE' There are scratch and alpine version of container image. ```sh -docker run -p 8080:8080 -v /var/run/docker.sock:/docker.sock ghcr.io/worldline-go/forward -s /docker.sock:/:-POST,-PUT,-DELETE,-PATCH +docker run --rm -it -p 8080:8080 -v /var/run/docker.sock:/docker.sock ghcr.io/worldline-go/forward -s /docker.sock:/:-POST,-PUT,-DELETE,-PATCH ``` ## Development diff --git a/cmd/forward/args/root.go b/cmd/forward/args/root.go index 950f7f0..9d1b97a 100644 --- a/cmd/forward/args/root.go +++ b/cmd/forward/args/root.go @@ -5,11 +5,10 @@ import ( "log/slog" "github.com/rakunlabs/into" + "github.com/spf13/cobra" "github.com/worldline-go/forward/internal/config" "github.com/worldline-go/forward/internal/info" "github.com/worldline-go/forward/internal/server" - - "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ @@ -32,6 +31,7 @@ var rootCmd = &cobra.Command{ func Execute(ctx context.Context) error { rootCmd.Version = info.AppInfo.Version rootCmd.Long += "\n" + longInfo() + return rootCmd.ExecuteContext(ctx) } diff --git a/cmd/forward/main.go b/cmd/forward/main.go index 6b24042..79804b8 100644 --- a/cmd/forward/main.go +++ b/cmd/forward/main.go @@ -8,7 +8,6 @@ import ( ) var ( - // Populated by goreleaser during build version = "v0.0.0" commit = "-" date = "-" diff --git a/internal/handler/methods.go b/internal/handler/methods.go index 5be725f..2108e0e 100644 --- a/internal/handler/methods.go +++ b/internal/handler/methods.go @@ -75,6 +75,7 @@ func printMethods(methods map[string]struct{}) string { for k := range methods { keys = append(keys, k) } + sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) diff --git a/internal/handler/methods_test.go b/internal/handler/methods_test.go index fc51dba..83e16f2 100644 --- a/internal/handler/methods_test.go +++ b/internal/handler/methods_test.go @@ -10,13 +10,16 @@ func TestFilterMethods_Parse(t *testing.T) { Allow map[string]struct{} Deny map[string]struct{} } + type match struct { Method string Check bool } + type args struct { methods []string } + tests := []struct { name string want want diff --git a/internal/handler/socket.go b/internal/handler/socket.go index d0d81bd..b8f9059 100644 --- a/internal/handler/socket.go +++ b/internal/handler/socket.go @@ -10,7 +10,7 @@ import ( "strings" ) -// SocketDirector is a reverse proxy director that rewrites the URL path to +// SocketDirector is a reverse proxy director that touch request before sending to the socket. func SocketDirector(req *http.Request) { req.Header.Add("X-Forwarded-Host", req.Host) req.URL.Scheme = "http" @@ -49,7 +49,6 @@ func SocketHandler(socketURLPath string, socketPath string, methods *FilterMetho return } - // TODO: return allowed methods w.WriteHeader(http.StatusMethodNotAllowed) w.Write([]byte("Method not allowed")) //nolint:errcheck } diff --git a/internal/server/http.go b/internal/server/http.go index 4dc0d72..ad68e33 100644 --- a/internal/server/http.go +++ b/internal/server/http.go @@ -23,9 +23,10 @@ type Server struct { // ServeHTTP returns a new HTTP server. func ServeHTTP() []Server { - values := Parse(config.Application.Hosts, config.Application.Sockets) var servers []Server + values := Parse(config.Application.Hosts, config.Application.Sockets) + for _, value := range values { mux := http.NewServeMux() // Add handler functions here @@ -56,6 +57,7 @@ func StartHTTP(server []Server) error { for _, s := range server { group.Go(func() error { slog.Info(fmt.Sprintf("server %s on [%s]", s.Name, s.Server.Addr)) + if err := s.Server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { return err } @@ -86,6 +88,7 @@ func StopHTTP(server []Server) error { } wg.Add(1) + go func(name string, s *http.Server) { defer wg.Done() diff --git a/internal/server/parser.go b/internal/server/parser.go index 9a44c5b..6e9cac0 100644 --- a/internal/server/parser.go +++ b/internal/server/parser.go @@ -12,7 +12,9 @@ type Holder struct { func Parse(hosts, sockets []string) []Holder { var holders []Holder + socketList := make(map[string][]string) + for _, socket := range sockets { name, sValue := atParse(socket) socketList[name] = append(socketList[name], sValue) @@ -33,6 +35,7 @@ func Parse(hosts, sockets []string) []Holder { func atParse(v string) (name string, value string) { vSplit := strings.SplitN(v, "@", 2) + switch { case len(vSplit) < 2: name = "default" diff --git a/internal/server/parser_test.go b/internal/server/parser_test.go index 6ddb31e..5bb7173 100644 --- a/internal/server/parser_test.go +++ b/internal/server/parser_test.go @@ -1,8 +1,10 @@ -package server +package server_test import ( "reflect" "testing" + + "github.com/worldline-go/forward/internal/server" ) func TestParse(t *testing.T) { @@ -10,10 +12,11 @@ func TestParse(t *testing.T) { hosts []string sockets []string } + tests := []struct { name string args args - want []Holder + want []server.Holder }{ { name: "default", @@ -21,7 +24,7 @@ func TestParse(t *testing.T) { hosts: []string{"0.0.0.0:8080"}, sockets: []string{"/docker/:*,-POST,-PUT,-DELETE"}, }, - want: []Holder{ + want: []server.Holder{ { Name: "default", Host: "0.0.0.0:8080", @@ -35,7 +38,7 @@ func TestParse(t *testing.T) { hosts: []string{"default@0.0.0.0:8080", "test@0.0.0.0:8081"}, sockets: []string{"/docker/:*,-POST,-PUT,-DELETE", "test@/docker/:*,-POST,-PUT,-DELETE"}, }, - want: []Holder{ + want: []server.Holder{ { Name: "default", Host: "0.0.0.0:8080", @@ -49,9 +52,10 @@ func TestParse(t *testing.T) { }, }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := Parse(tt.args.hosts, tt.args.sockets); !reflect.DeepEqual(got, tt.want) { + if got := server.Parse(tt.args.hosts, tt.args.sockets); !reflect.DeepEqual(got, tt.want) { t.Errorf("Parse() = %v, want %v", got, tt.want) } })