summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-11-11 13:33:11 +0700
committerShulhan <ms@kilabit.info>2023-11-11 13:34:43 +0700
commit08852664aa6952e727addb0c24ad8f56274e09bb (patch)
treed4f59e9000a2676bf1c5952c0cbeb6a5ff1f7443
parente56ee3e03a34fa738b626a4fd22fbc051bf7f0b0 (diff)
downloadgorankusu-08852664aa6952e727addb0c24ad8f56274e09bb.tar.xz
Makefile: replace the Go linter and apply all their recommendations
Previously, we use golangci-lint as linter. This linter does not provides any useful recommendation lately and the development is quite a mess, sometimes its break when using Go tip. In this changes we replace it with revive, fieldalignment, and shadow; and fix all of their recommendations.
-rw-r--r--Makefile16
-rw-r--r--attack_options.go1
-rw-r--r--doc.go2
-rw-r--r--example/example.go7
-rw-r--r--form_input.go1
-rw-r--r--http_server.go8
-rw-r--r--http_target.go1
-rw-r--r--run_response.go2
-rw-r--r--trunks.go29
9 files changed, 44 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index 1ee608c..0875d72 100644
--- a/Makefile
+++ b/Makefile
@@ -2,15 +2,23 @@
## SPDX-License-Identifier: GPL-3.0-or-later
.PHONY: all
-all: lint
+all: lint test
go run ./internal/cmd/trunks build
- CGO_ENABLED=1 go test -v -race ./...
- -golangci-lint run ./...
.PHONY: lint
-lint:
+lint: lint-www
+ -revive ./...
+ -fieldalignment ./...
+ -shadow ./...
+
+.PHONY: lint-www
+lint-www:
cd _www && npx eslint --fix .
+.PHONY: test
+test:
+ CGO_ENABLED=1 go test -v -race ./...
+
.PHONY: dev
dev:
go run ./internal/cmd/trunks
diff --git a/attack_options.go b/attack_options.go
index 417071b..60f5d5a 100644
--- a/attack_options.go
+++ b/attack_options.go
@@ -9,6 +9,7 @@ import (
vegeta "github.com/tsenart/vegeta/v12/lib"
)
+// AttackOptions define the options for attacking HTTP endpoint.
type AttackOptions struct {
// Duration define the duration for each attack to be executed,
// in seconds.
diff --git a/doc.go b/doc.go
index ee28696..289ef23 100644
--- a/doc.go
+++ b/doc.go
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
/*
-Module trunks is a library and HTTP service that provide web user interface
+Package trunks is a library and HTTP service that provide web user interface
to test HTTP service, similar to Postman, and for load testing.
For the load testing we use vegeta [1] as the backend.
diff --git a/example/example.go b/example/example.go
index 73efe51..332cbb3 100644
--- a/example/example.go
+++ b/example/example.go
@@ -48,6 +48,7 @@ type requestResponse struct {
Body string
}
+// Example contains an example how to use Trunks programmatically.
type Example struct {
trunks *trunks.Trunks
wsServer *websocket.Server
@@ -102,6 +103,7 @@ func New() (ex *Example, err error) {
return ex, nil
}
+// Start the Example servers.
func (ex *Example) Start() (err error) {
go func() {
err = ex.wsServer.Start()
@@ -113,6 +115,7 @@ func (ex *Example) Start() (err error) {
return ex.trunks.Start()
}
+// Stop the Example servers.
func (ex *Example) Stop() {
ex.wsServer.Stop()
ex.trunks.Stop()
@@ -370,7 +373,7 @@ func (ex *Example) pathExampleGet(epr *libhttp.EndpointRequest) ([]byte, error)
return json.Marshal(&res)
}
-func (ex *Example) pathExampleErrorGet(epr *libhttp.EndpointRequest) ([]byte, error) {
+func (ex *Example) pathExampleErrorGet(_ *libhttp.EndpointRequest) ([]byte, error) {
return nil, liberrors.Internal(fmt.Errorf("server error"))
}
@@ -550,7 +553,7 @@ func (ex *Example) attackExamplePostForm(rr *trunks.RunRequest) vegeta.Targeter
}
}
-func (ex *Example) handleWSExampleGet(ctx context.Context, req *websocket.Request) (res websocket.Response) {
+func (ex *Example) handleWSExampleGet(_ context.Context, req *websocket.Request) (res websocket.Response) {
res.ID = req.ID
res.Code = http.StatusOK
res.Body = req.Body
diff --git a/form_input.go b/form_input.go
index 5fd0a8d..9934f51 100644
--- a/form_input.go
+++ b/form_input.go
@@ -3,6 +3,7 @@
package trunks
+// FormInputKind define type for form input.
type FormInputKind string
// List of valid value for field FormInput.Kind.
diff --git a/http_server.go b/http_server.go
index d737352..4b89358 100644
--- a/http_server.go
+++ b/http_server.go
@@ -160,7 +160,7 @@ func (trunks *Trunks) initHttpServer(isDevelopment bool) (err error) {
}
// apiEnvironmentGet get the Trunks environment including its state.
-func (trunks *Trunks) apiEnvironmentGet(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
+func (trunks *Trunks) apiEnvironmentGet(_ *libhttp.EndpointRequest) (resbody []byte, err error) {
res := libhttp.EndpointResponse{}
res.Code = http.StatusOK
res.Data = trunks.Env
@@ -228,7 +228,7 @@ func (trunks *Trunks) apiAttackHttp(epr *libhttp.EndpointRequest) (resbody []byt
// Response codes,
// - 200 OK: success, return the RunRequest object that has been cancelled.
// - 500 ERR_INTERNAL: internal server error.
-func (trunks *Trunks) apiAttackHttpCancel(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
+func (trunks *Trunks) apiAttackHttpCancel(_ *libhttp.EndpointRequest) (resbody []byte, err error) {
var (
logp = `apiAttackHttpCancel`
runRequest *RunRequest
@@ -298,7 +298,7 @@ func (trunks *Trunks) apiAttackResultGet(epr *libhttp.EndpointRequest) (resbody
return json.Marshal(&res)
}
-func (trunks *Trunks) apiNavLinks(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
+func (trunks *Trunks) apiNavLinks(_ *libhttp.EndpointRequest) (resbody []byte, err error) {
res := libhttp.EndpointResponse{}
res.Code = http.StatusOK
res.Data = trunks.navLinks
@@ -355,7 +355,7 @@ func (trunks *Trunks) apiTargetRunWebSocket(epr *libhttp.EndpointRequest) ([]byt
return json.Marshal(&epres)
}
-func (trunks *Trunks) apiTargets(epr *libhttp.EndpointRequest) (resbody []byte, err error) {
+func (trunks *Trunks) apiTargets(_ *libhttp.EndpointRequest) (resbody []byte, err error) {
res := libhttp.EndpointResponse{}
res.Code = http.StatusOK
res.Data = trunks.targets
diff --git a/http_target.go b/http_target.go
index 154d839..8e42309 100644
--- a/http_target.go
+++ b/http_target.go
@@ -32,6 +32,7 @@ type HttpAttackHandler func(rr *RunRequest) vegeta.Targeter
// the actual Attack being called.
type HttpPreAttackHandler func(rr *RunRequest)
+// HttpTarget define the HTTP endpoint that can be attached to Trunks.
type HttpTarget struct {
Params KeyFormInput
ConvertParams HttpConvertParams `json:"-"`
diff --git a/run_response.go b/run_response.go
index 7e2d067..9df7671 100644
--- a/run_response.go
+++ b/run_response.go
@@ -25,7 +25,7 @@ type RunResponse struct {
ResponseStatusCode int
}
-// SetHttpResponse dump the HTTP request including body into the DumpRequest
+// SetHttpRequest dump the HTTP request including body into the DumpRequest
// field.
func (rres *RunResponse) SetHttpRequest(req *http.Request) (err error) {
rres.DumpRequest, err = httputil.DumpRequest(req, true)
diff --git a/trunks.go b/trunks.go
index 3043fc7..30d358a 100644
--- a/trunks.go
+++ b/trunks.go
@@ -17,9 +17,11 @@ import (
"github.com/shuLhan/share/lib/mlog"
)
-const (
- Version = `0.4.0`
+// Version of trunks module.
+const Version = `0.4.0`
+// List of default values.
+const (
DefaultAttackDuration = 10 * time.Second
DefaultAttackRatePerSecond = 500
DefaultAttackTimeout = 30 * time.Second
@@ -27,15 +29,19 @@ const (
DefaultMaxAttackRate = 3000
DefaultListenAddress = `127.0.0.1:8217`
+)
- // Setting this environment variable will enable trunks development
- // mode.
- EnvDevelopment = "TRUNKS_DEV"
+// EnvDevelopment setting this environment variable will enable trunks
+// development mode.
+const EnvDevelopment = "TRUNKS_DEV"
- // List of HTTP parameters.
+// List of HTTP parameters.
+const (
paramNameName = "name"
+)
- // List of HTTP APIs.
+// List of HTTP APIs.
+const (
pathApiAttackHttp = `/_trunks/api/attack/http`
pathApiAttackResult = `/_trunks/api/attack/result`
)
@@ -157,6 +163,7 @@ func (trunks *Trunks) RegisterNavLink(nav *NavLink) (err error) {
return nil
}
+// RegisterTarget register Target to be attached to Trunks.
func (trunks *Trunks) RegisterTarget(target *Target) (err error) {
if target == nil {
return
@@ -191,7 +198,7 @@ func (trunks *Trunks) RunHttp(req *RunRequest) (res *RunResponse, err error) {
req.HttpTarget.ConvertParams = origHttpTarget.ConvertParams
res, err = trunks.runHttpTarget(req)
} else {
- req := generateRunRequest(trunks.Env, req, origTarget, origHttpTarget)
+ req = generateRunRequest(trunks.Env, req, origTarget, origHttpTarget)
res, err = req.HttpTarget.Run(req)
}
if err != nil {
@@ -212,9 +219,9 @@ func (trunks *Trunks) Start() (err error) {
mlog.Outf(`trunks: starting HTTP server at http://%s`, trunks.Env.ListenAddress)
go func() {
- err := trunks.Httpd.Start()
- if err != nil {
- trunks.errq <- err
+ var errStart = trunks.Httpd.Start()
+ if errStart != nil {
+ trunks.errq <- errStart
}
}()