From 6e27a84dba52e68fe7b75df50190934ab96c1946 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sun, 21 Aug 2022 02:14:04 +0700 Subject: all: reformat with Go 1.19 gofmt --- common.go | 2 -- doc.go | 24 +++++++++--------------- environment.go | 2 -- example/example.go | 6 ------ form_input.go | 2 -- http_target.go | 6 ------ key_form_input.go | 10 ---------- run_request.go | 6 ------ target.go | 2 -- trunks.go | 18 ------------------ websocket_target.go | 4 ---- 11 files changed, 9 insertions(+), 73 deletions(-) diff --git a/common.go b/common.go index ccd8ec5..629975f 100644 --- a/common.go +++ b/common.go @@ -8,9 +8,7 @@ import ( "unicode" ) -// // generateID replace non-letter and non-number from input string with '_'. -// func generateID(in string) (out string) { var r []rune = make([]rune, 0, len(in)) in = strings.ToLower(in) diff --git a/doc.go b/doc.go index 06e31dc..ee28696 100644 --- a/doc.go +++ b/doc.go @@ -7,17 +7,16 @@ to test HTTP service, similar to Postman, and for load testing. For the load testing we use vegeta [1] as the backend. -Usage +# Usage See the example package on how to programmatically use and create service using this module, or - * clone this repository, - * execute `make run`, and - * open http://127.0.0.1:8217. + - clone this repository, + - execute `make run`, and + - open http://127.0.0.1:8217. - -Screen shots +# Screen shots The following screenshot display the main interface to Run or Attack the registered HTTP service, @@ -29,15 +28,13 @@ metrics and vegeta histogram, https://git.sr.ht/~shulhan/trunks/blob/main/_screenshots/trunks_attack_result.png - -Web user interface +# Web user interface By default, the Trunks user interface can be viewed by opening in browser at http://127.0.0.1:8217. One can change address through Environment's ListenAddress. - -File name format +# File name format Each attack result is saved in Environment's ResultsDir with the following file name format, @@ -50,17 +47,14 @@ The "DateTime" is in the following layout, The "ResultsSuffix" is the one that defined in Environment. - -License +# License Copyright 2021, Shulhan . All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. - -References +# References [1] https://github.com/tsenart/vegeta - */ package trunks diff --git a/environment.go b/environment.go index 1c2b4df..ec9f639 100644 --- a/environment.go +++ b/environment.go @@ -11,9 +11,7 @@ import ( "time" ) -// // Environment contains global configuration for load testing. -// type Environment struct { // AttackRunning will be set to non-nil if there is a load // testing currently running. diff --git a/example/example.go b/example/example.go index 6c5bf4a..25c1532 100644 --- a/example/example.go +++ b/example/example.go @@ -1,7 +1,6 @@ // SPDX-FileCopyrightText: 2021 M. Shulhan // SPDX-License-Identifier: GPL-3.0-or-later -// // Package example provide an example how to use the Trunks library from // setting it up to creating targets. // @@ -10,7 +9,6 @@ // $ go run ./internal/cmd/trunks // // It will run a web user interface at http://127.0.0.1:8217 . -// package example import ( @@ -59,9 +57,7 @@ type Example struct { targetExamplePostForm vegeta.Target } -// // New create, initialize, and setup an example service. -// func New() (ex *Example, err error) { env := &trunks.Environment{ ResultsDir: "example/testdata/", @@ -122,9 +118,7 @@ func (ex *Example) Stop() { ex.trunks.Stop() } -// // registerEndpoints register HTTP endpoints for testing. -// func (ex *Example) registerEndpoints() (err error) { err = ex.trunks.Httpd.RegisterEndpoint(&libhttp.Endpoint{ Method: libhttp.RequestMethodGet, diff --git a/form_input.go b/form_input.go index bb7c065..48ad067 100644 --- a/form_input.go +++ b/form_input.go @@ -10,7 +10,6 @@ const ( FormInputKindString = "string" ) -// // FormInput provide the information to create an input component. // // The Label field define the input text, the Hint field provide a @@ -20,7 +19,6 @@ const ( // // The Max and Min fields is optional, it only affect if the Kind is // FormInputKindNumber. -// type FormInput struct { Label string `json:"label"` Hint string `json:"hint"` diff --git a/http_target.go b/http_target.go index 99a6965..b209c0d 100644 --- a/http_target.go +++ b/http_target.go @@ -15,22 +15,16 @@ import ( vegeta "github.com/tsenart/vegeta/v12/lib" ) -// // HttpConvertParams is a handler that will be called inside the Run handler // to convert the Params values to type that will be send as request. -// type HttpConvertParams func(target *HttpTarget) (interface{}, error) -// // HttpRunHandler define the function type that will be called when client // send request to run the HTTP target. -// type HttpRunHandler func(rr *RunRequest) (runres *RunResponse, err error) -// // HttpAttackHandler define the function type that will be called when client // send request to attack HTTP target. -// type HttpAttackHandler func(rr *RunRequest) vegeta.Targeter // HttpPreAttackHandler define the function type that will be called before diff --git a/key_form_input.go b/key_form_input.go index c4f988d..2a8789a 100644 --- a/key_form_input.go +++ b/key_form_input.go @@ -10,15 +10,11 @@ import ( "github.com/shuLhan/share/lib/math/big" ) -// // KeyFormInput is the simplified type for getting and setting HTTP headers // and request parameters (either in query or in the parameter body). -// type KeyFormInput map[string]FormInput -// // ToHttpHeader convert the KeyFormInputs to the standard http.Header. -// func (kfi KeyFormInput) ToHttpHeader() (headers http.Header) { headers = http.Header{} if kfi == nil || len(kfi) == 0 { @@ -30,9 +26,7 @@ func (kfi KeyFormInput) ToHttpHeader() (headers http.Header) { return headers } -// // ToJsonObject convert the KeyFormInput into JSON object. -// func (kfi KeyFormInput) ToJsonObject() (data map[string]interface{}) { data = make(map[string]interface{}, len(kfi)) for k, fi := range kfi { @@ -46,10 +40,8 @@ func (kfi KeyFormInput) ToJsonObject() (data map[string]interface{}) { return data } -// // ToMultipartFormData convert the KeyFormInput into map of string and raw // bytes. -// func (kfi KeyFormInput) ToMultipartFormData() (data map[string][]byte) { data = make(map[string][]byte, len(kfi)) if kfi == nil || len(kfi) == 0 { @@ -61,9 +53,7 @@ func (kfi KeyFormInput) ToMultipartFormData() (data map[string][]byte) { return data } -// // ToUrlValues convert the KeyFormInput to the standard url.Values. -// func (kfi KeyFormInput) ToUrlValues() (vals url.Values) { vals = url.Values{} if kfi == nil || len(kfi) == 0 { diff --git a/run_request.go b/run_request.go index 36a2bca..ac875df 100644 --- a/run_request.go +++ b/run_request.go @@ -11,9 +11,7 @@ import ( vegeta "github.com/tsenart/vegeta/v12/lib" ) -// // RunRequest define the request to run HTTP or WebSocket target. -// type RunRequest struct { result *AttackResult @@ -24,10 +22,8 @@ type RunRequest struct { Locker sync.Mutex `json:"-"` } -// // generateRunRequest merge the run request with original target and HTTP // target into new RunRequest. -// func generateRunRequest( env *Environment, req *RunRequest, @@ -65,10 +61,8 @@ func generateRunRequest( return outrr } -// // generateWebSocketTarget merge the run request with original target and // WebSocket target into new RunRequest -// func generateWebSocketTarget( env *Environment, req *RunRequest, diff --git a/target.go b/target.go index 92cc98a..a69ee37 100644 --- a/target.go +++ b/target.go @@ -9,9 +9,7 @@ import ( libhttp "github.com/shuLhan/share/lib/http" ) -// // Target contains group of HttpTarget that can be tested by Trunks. -// type Target struct { // HttpClient that can be used for running HttpTarget. HttpClient *libhttp.Client `json:"-"` diff --git a/trunks.go b/trunks.go index 152be44..1e9bd44 100644 --- a/trunks.go +++ b/trunks.go @@ -42,10 +42,8 @@ const ( apiAttackResult = "/_trunks/api/attack/result" ) -// // Trunks is the HTTP server with web user interface and APIs for running and // load testing the registered HTTP endpoints. -// type Trunks struct { Env *Environment Httpd *libhttp.Server @@ -59,9 +57,7 @@ type Trunks struct { navLinks []*NavLink } -// // New create and initialize new Trunks service. -// func New(env *Environment) (trunks *Trunks, err error) { var ( logp = "trunks.New" @@ -92,9 +88,7 @@ func New(env *Environment) (trunks *Trunks, err error) { return trunks, nil } -// // AttackHttp start attacking the HTTP target defined in req. -// func (trunks *Trunks) AttackHttp(req *RunRequest) (err error) { logp := "AttackHttp" @@ -133,10 +127,8 @@ func (trunks *Trunks) AttackHttp(req *RunRequest) (err error) { return nil } -// // AttackHttpCancel cancel any running attack. // It will return an error if no attack is running. -// func (trunks *Trunks) AttackHttpCancel() (rr *RunRequest, err error) { rr = trunks.Env.getRunningAttack() if rr == nil { @@ -153,9 +145,7 @@ func (trunks *Trunks) AttackHttpCancel() (rr *RunRequest, err error) { return rr, nil } -// // RegisterNavLink register custom navigation link. -// func (trunks *Trunks) RegisterNavLink(nav *NavLink) (err error) { if nav == nil { return @@ -186,10 +176,8 @@ func (trunks *Trunks) RegisterTarget(target *Target) (err error) { return nil } -// // RunHttp send the HTTP request to the HTTP target defined in RunRequest with // optional Headers and Parameters. -// func (trunks *Trunks) RunHttp(req *RunRequest) (res *RunResponse, err error) { origTarget := trunks.getTargetByID(req.Target.ID) if origTarget == nil { @@ -215,10 +203,8 @@ func (trunks *Trunks) RunHttp(req *RunRequest) (res *RunResponse, err error) { return res, nil } -// // Start the Trunks HTTP server that provide user interface for running and // load testing registered Targets. -// func (trunks *Trunks) Start() (err error) { mlog.Outf("trunks: scanning previous attack results...\n") trunks.scanResultsDir() @@ -246,9 +232,7 @@ func (trunks *Trunks) Start() (err error) { return err } -// // Stop the Trunks HTTP server. -// func (trunks *Trunks) Stop() { logp := "trunks.Stop" mlog.Outf("=== Stopping the Trunks service ...\n") @@ -400,13 +384,11 @@ func (trunks *Trunks) runHttpTarget(rr *RunRequest) (res *RunResponse, err error return res, nil } -// // scanResultsDir scan the environment's ResultsDir for the past attack // results and add it to each target based on ID on file name. // // Due to size of file can be big (maybe more than 5000 records), this // function only parse the file name and append it to Results field. -// func (trunks *Trunks) scanResultsDir() { logp := "scanResultsDir" diff --git a/websocket_target.go b/websocket_target.go index ef2c53c..c275ec3 100644 --- a/websocket_target.go +++ b/websocket_target.go @@ -5,15 +5,11 @@ package trunks import "fmt" -// // WebSocketRunHandler define a function type that will be called to run the // WebSocket target. -// type WebSocketRunHandler func(rr *RunRequest) (interface{}, error) -// // WebSocketTarget define the target to test WebSocket service. -// type WebSocketTarget struct { Headers KeyFormInput -- cgit v1.3