Skip to content

Commit

Permalink
fix: linting update
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <[email protected]>
  • Loading branch information
rytsh committed Sep 7, 2024
1 parent a7308c0 commit 76aa976
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 32 deletions.
30 changes: 12 additions & 18 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/forward/args/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}

Expand Down
1 change: 0 additions & 1 deletion cmd/forward/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

var (
// Populated by goreleaser during build
version = "v0.0.0"
commit = "-"
date = "-"
Expand Down
1 change: 1 addition & 0 deletions internal/handler/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
})
Expand Down
3 changes: 3 additions & 0 deletions internal/handler/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/handler/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -86,6 +88,7 @@ func StopHTTP(server []Server) error {
}

wg.Add(1)

go func(name string, s *http.Server) {
defer wg.Done()

Expand Down
3 changes: 3 additions & 0 deletions internal/server/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down
14 changes: 9 additions & 5 deletions internal/server/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package server
package server_test

import (
"reflect"
"testing"

"github.com/worldline-go/forward/internal/server"
)

func TestParse(t *testing.T) {
type args struct {
hosts []string
sockets []string
}

tests := []struct {
name string
args args
want []Holder
want []server.Holder
}{
{
name: "default",
args: args{
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",
Expand All @@ -35,7 +38,7 @@ func TestParse(t *testing.T) {
hosts: []string{"[email protected]:8080", "[email protected]: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",
Expand All @@ -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)
}
})
Expand Down

0 comments on commit 76aa976

Please sign in to comment.