Skip to content

Commit

Permalink
Merge pull request #108 from PanGan21/106-add-linter-rules
Browse files Browse the repository at this point in the history
Add golangci config file and fix linter issues
  • Loading branch information
nguyer authored Sep 19, 2023
2 parents 2346ff4 + d86d6ba commit 92a9508
Show file tree
Hide file tree
Showing 69 changed files with 1,493 additions and 867 deletions.
60 changes: 60 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
run:
tests: false
skip-dirs:
- "mocks"
issues:
exclude-rules:
- path: internal/events/test_helper.go
text: "unused"
linters-settings:
golint: {}
gocritic:
enabled-checks: []
disabled-checks:
- regexpMust
goheader:
values:
regexp:
COMPANY: .*
template: |-
Copyright © {{ YEAR }} {{ COMPANY }}
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
linters:
disable-all: false
disable:
- structcheck
enable:
- dogsled
- errcheck
- goconst
- gocritic
- gocyclo
- gofmt
- goheader
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unused
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ mocks: mockery ${GOFILES}
${MOCKERY} --case underscore --dir internal/fabric/dep --name CAClient --output mocks/fabric/dep --outpkg mockfabricdep
${MOCKERY} --case underscore --dir internal/kvstore --name KVStore --output mocks/kvstore --outpkg mockkvstore
${MOCKERY} --case underscore --dir internal/kvstore --name KVIterator --output mocks/kvstore --outpkg mockkvstore
${MOCKERY} --case underscore --dir internal/rest/async --name AsyncDispatcher --output mocks/rest/async --outpkg mockasync
${MOCKERY} --case underscore --dir internal/rest/identity --name IdentityClient --output mocks/rest/identity --outpkg mockidentity
${MOCKERY} --case underscore --dir internal/rest/receipt --name ReceiptStore --output mocks/rest/receipt --outpkg mockreceipt
${MOCKERY} --case underscore --dir internal/rest/async --name Dispatcher --output mocks/rest/async --outpkg mockasync
${MOCKERY} --case underscore --dir internal/rest/identity --name Client --output mocks/rest/identity --outpkg mockidentity
${MOCKERY} --case underscore --dir internal/rest/receipt --name Store --output mocks/rest/receipt --outpkg mockreceipt
${MOCKERY} --case underscore --dir internal/rest/receipt/api --name ReceiptStorePersistence --output mocks/rest/receipt/api --outpkg mockreceiptapi
${MOCKERY} --case underscore --dir internal/rest/sync --name SyncDispatcher --output mocks/rest/sync --outpkg mocksync
${MOCKERY} --case underscore --dir internal/tx --name TxProcessor --output mocks/tx --outpkg mocktx
${MOCKERY} --case underscore --dir internal/rest/sync --name Dispatcher --output mocks/rest/sync --outpkg mocksync
${MOCKERY} --case underscore --dir internal/tx --name Processor --output mocks/tx --outpkg mocktx
${MOCKERY} --case underscore --dir internal/ws --name WebSocketServer --output mocks/ws --outpkg mockws
${MOCKERY} --case underscore --dir internal/ws --name WebSocketChannels --output mocks/ws --outpkg mockws
36 changes: 26 additions & 10 deletions cmd/debugrouter.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand All @@ -10,19 +26,19 @@ import (
log "github.com/sirupsen/logrus"
)

type debugRouter struct {
type DebugRouter struct {
router *httprouter.Router
}

func NewDebugRouter() *debugRouter {
func NewDebugRouter() *DebugRouter {
r := httprouter.New()
cors.Default().Handler(r)
return &debugRouter{
return &DebugRouter{
router: r,
}
}

func (d *debugRouter) addRoutes() {
func (d *DebugRouter) addRoutes() {
d.router.GET("/debug/pprof/cmdline", d.cmdline)
d.router.GET("/debug/pprof/profile", d.profile)
d.router.GET("/debug/pprof/symbol", d.symbol)
Expand All @@ -31,35 +47,35 @@ func (d *debugRouter) addRoutes() {
d.router.GET("/debug/pprof/", d.index)
}

func (d *debugRouter) cmdline(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
func (d *DebugRouter) cmdline(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)
pprof.Cmdline(res, req)
}

func (d *debugRouter) profile(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
func (d *DebugRouter) profile(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)
pprof.Profile(res, req)
}

func (d *debugRouter) symbol(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
func (d *DebugRouter) symbol(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)

pprof.Symbol(res, req)
}

func (d *debugRouter) trace(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
func (d *DebugRouter) trace(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)

pprof.Trace(res, req)
}

func (d *debugRouter) index(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
func (d *DebugRouter) index(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)

pprof.Index(res, req)
}

func (d *debugRouter) goroutines(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
func (d *DebugRouter) goroutines(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
log.Infof("--> %s %s", req.Method, req.URL)

_ = runtimepprof.Lookup("goroutine").WriteTo(res, 1)
Expand Down
14 changes: 6 additions & 8 deletions cmd/fabconnect.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2021 Kaleido
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//
// http://www.apache.org/licenses/LICENSE-2.0

//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -23,16 +23,14 @@ import (
"strings"
"time"

"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"

"github.com/hyperledger/firefly-fabconnect/internal/conf"
"github.com/hyperledger/firefly-fabconnect/internal/rest"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
prefixed "github.com/x-cray/logrus-prefixed-formatter"

_ "net/http/pprof"
)

func initLogging(debugLevel int) {
Expand Down Expand Up @@ -68,7 +66,7 @@ var rootConfig = &cmdConfig{}

func newRootCmd() (*cobra.Command, *conf.RESTGatewayConf) {
restGatewayConf := &conf.RESTGatewayConf{}
var restGateway *rest.RESTGateway
var restGateway *rest.Gateway

rootCmd := &cobra.Command{
Use: "fabconnect",
Expand Down Expand Up @@ -151,7 +149,7 @@ func initConfig() {
}
}

func startServer(restGatewayConf *conf.RESTGatewayConf, restGateway *rest.RESTGateway) error {
func startServer(restGatewayConf *conf.RESTGatewayConf, restGateway *rest.Gateway) error {

if rootConfig.PrintYAML {
a, err := marshalToYAML(rootConfig)
Expand Down
38 changes: 19 additions & 19 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright 2021 Kaleido
// Copyright © 2023 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//
// http://www.apache.org/licenses/LICENSE-2.0

//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -19,7 +19,7 @@ package auth
import (
"context"

"github.com/hyperledger/firefly-fabconnect/internal/errors"
internalErrors "github.com/hyperledger/firefly-fabconnect/internal/errors"
"github.com/hyperledger/firefly-fabconnect/pkg/plugins"
)

Expand Down Expand Up @@ -77,60 +77,60 @@ func GetAccessToken(ctx context.Context) string {
return ""
}

// AuthRPC authorize an RPC call
func AuthRPC(ctx context.Context, method string, args ...interface{}) error {
// RPC authorize an RPC call
func RPC(ctx context.Context, method string, args ...interface{}) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
return errors.Errorf(errors.SecurityModuleNoAuthContext)
return internalErrors.Errorf(internalErrors.SecurityModuleNoAuthContext)
}
return securityModule.AuthRPC(authCtx, method, args...)
}
return nil
}

// AuthRPCSubscribe authorize a subscribe RPC call
func AuthRPCSubscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) error {
// RPCSubscribe authorize a subscribe RPC call
func RPCSubscribe(ctx context.Context, namespace string, channel interface{}, args ...interface{}) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
return errors.Errorf(errors.SecurityModuleNoAuthContext)
return internalErrors.Errorf(internalErrors.SecurityModuleNoAuthContext)
}
return securityModule.AuthRPCSubscribe(authCtx, namespace, channel, args...)
}
return nil
}

// AuthEventStreams authorize the whole of event streams
func AuthEventStreams(ctx context.Context) error {
// EventStreams authorize the whole of event streams
func EventStreams(ctx context.Context) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
return errors.Errorf(errors.SecurityModuleNoAuthContext)
return internalErrors.Errorf(internalErrors.SecurityModuleNoAuthContext)
}
return securityModule.AuthEventStreams(authCtx)
}
return nil
}

// AuthListAsyncReplies authorize the listing or searching of all replies
func AuthListAsyncReplies(ctx context.Context) error {
// ListAsyncReplies authorize the listing or searching of all replies
func ListAsyncReplies(ctx context.Context) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
return errors.Errorf(errors.SecurityModuleNoAuthContext)
return internalErrors.Errorf(internalErrors.SecurityModuleNoAuthContext)
}
return securityModule.AuthListAsyncReplies(authCtx)
}
return nil
}

// AuthReadAsyncReplyByUUID authorize the query of an invidual reply by UUID
func AuthReadAsyncReplyByUUID(ctx context.Context) error {
// ReadAsyncReplyByUUID authorize the query of an invidual reply by UUID
func ReadAsyncReplyByUUID(ctx context.Context) error {
if securityModule != nil && !IsSystemContext(ctx) {
authCtx := GetAuthContext(ctx)
if authCtx == nil {
return errors.Errorf(errors.SecurityModuleNoAuthContext)
return internalErrors.Errorf(internalErrors.SecurityModuleNoAuthContext)
}
return securityModule.AuthReadAsyncReplyByUUID(authCtx)
}
Expand Down
Loading

0 comments on commit 92a9508

Please sign in to comment.