From 36518cb90280dc380bf083c52d49c2674f798c65 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Tue, 17 Jun 2025 22:03:11 -0400 Subject: [PATCH 1/4] add linting configuration and update logging context key type --- .gitea/workflows/lint.yml | 39 +++++++++++++++++++++++++++++++++++++++ .golangci.yml | 17 +++++++++++++++++ logger/log.go | 5 ++++- main.go | 1 - 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 .gitea/workflows/lint.yml create mode 100644 .golangci.yml diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml new file mode 100644 index 0000000..bf3e17f --- /dev/null +++ b/.gitea/workflows/lint.yml @@ -0,0 +1,39 @@ +on: + push: + branches: + - "*" + +name: ๐Ÿงช โœจ Unit Tests Workflow + +jobs: + test-go: + env: + GO_VERSION: '1.24.3' + NODE_TLS_REJECT_UNAUTHORIZED: 0 + name: ๐Ÿ” ๐Ÿน Go Tests + runs-on: ubuntu-latest + steps: + + - name: ๐Ÿ›ก๏ธ ๐Ÿ”’ Add Siteworx CA Certificates + run: | + curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt + update-ca-certificates + + - name: โš™๏ธ ๐Ÿน Set up Go Environment + uses: actions/setup-go@v2 + with: + go-version: ${{ env.GO_VERSION }} + cache: true + + - name: ๐Ÿ“– ๐Ÿ” Checkout Repository Code + uses: actions/checkout@v2 + with: + fetch-depth: 1 + + - name: ๐Ÿ“ฆ ๐Ÿ“ฅ Install Dependencies + run: | + go mod download + + - name: โœ… ๐Ÿ” Run Go Lint + run: | + golangci-lint run ./... \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..39d7345 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,17 @@ +version: "2" +linters: + default: standard + enable: + - whitespace + - tagalign + - reassign + - bodyclose + - contextcheck + - containedctx + - godot + - usestdlibvars + +formatters: + settings: + gofmt: + simplify: true \ No newline at end of file diff --git a/logger/log.go b/logger/log.go index c923082..5118902 100644 --- a/logger/log.go +++ b/logger/log.go @@ -5,7 +5,10 @@ import ( log "github.com/sirupsen/logrus" ) -const contextKey = "logger" +type contextKeyType string + +const contextKey contextKeyType = "logger +const contextKey contextKeyType = "logger" type Interface interface { Info(format string, args ...interface{}) diff --git a/main.go b/main.go index 40b3a88..7817124 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,6 @@ import ( const Version = "0.1" func main() { - rootCmd := &cobra.Command{ Use: "Go-Template", Short: "Go-Template is a simple template for Go applications", -- 2.49.1 From 7efe11f845332b52c869a718b107291f5485190a Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Tue, 17 Jun 2025 22:05:36 -0400 Subject: [PATCH 2/4] update linting action to use golangci-lint-action@v8.0.0 --- .gitea/workflows/lint.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitea/workflows/lint.yml b/.gitea/workflows/lint.yml index bf3e17f..ec0016b 100644 --- a/.gitea/workflows/lint.yml +++ b/.gitea/workflows/lint.yml @@ -35,5 +35,4 @@ jobs: go mod download - name: โœ… ๐Ÿ” Run Go Lint - run: | - golangci-lint run ./... \ No newline at end of file + uses: golangci/golangci-lint-action@v8.0.0 \ No newline at end of file -- 2.49.1 From ef073eadc2e61255d7aadc2bde52dc921fd2a11f Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Tue, 17 Jun 2025 22:12:43 -0400 Subject: [PATCH 3/4] enhance linting rules and update HTTP status codes in responses --- .golangci.yml | 10 ++++++++++ http_handlers/index/index.go | 7 ++++--- logger/log.go | 4 ++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 39d7345..45d32d5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -10,6 +10,16 @@ linters: - containedctx - godot - usestdlibvars + - funlen + - gochecknoglobals + - tagalign + - sqlclosecheck + - rowserrcheck + - recvcheck + - reassign + - predeclared + - maintidx + - mnd formatters: settings: diff --git a/http_handlers/index/index.go b/http_handlers/index/index.go index f3fab97..ab95ed8 100644 --- a/http_handlers/index/index.go +++ b/http_handlers/index/index.go @@ -4,6 +4,7 @@ import ( "gitea.siteworxpro.com/Siteworxpro/Go-Template/http_handlers/errors" "gitea.siteworxpro.com/Siteworxpro/Go-Template/logger" "github.com/labstack/echo/v4" + "net/http" ) func Register(g *echo.Group) { @@ -20,7 +21,7 @@ type index struct{} func (i *index) Get(c echo.Context) error { c.Get("logger").(logger.Interface).Info("Index handler called") - return c.JSON(200, map[string]string{"message": "Hello, World!"}) + return c.JSON(http.StatusOK, map[string]string{"message": "Hello, World!"}) } func (i *index) Post(c echo.Context) error { @@ -28,11 +29,11 @@ func (i *index) Post(c echo.Context) error { } func (i *index) Put(c echo.Context) error { - return c.JSON(200, map[string]string{"message": "Hello, World!"}) + return c.JSON(http.StatusOK, map[string]string{"message": "Hello, World!"}) } func (i *index) Delete(c echo.Context) error { - return c.JSON(200, map[string]string{"message": "Hello, World!"}) + return c.JSON(http.StatusOK, map[string]string{"message": "Hello, World!"}) } func (i *index) Patch(c echo.Context) error { diff --git a/logger/log.go b/logger/log.go index 5118902..c781b4f 100644 --- a/logger/log.go +++ b/logger/log.go @@ -22,7 +22,7 @@ type Logger struct { logger *log.Logger } -func FromContext(ctx context.Context) Interface { +func FromContext(ctx context.Context) *Logger { logger, ok := ctx.Value(contextKey).(*Logger) if !ok { return nil @@ -31,7 +31,7 @@ func FromContext(ctx context.Context) Interface { return logger } -func NewLogger(level log.Level) Interface { +func NewLogger(level log.Level) *Logger { l := log.New() l.SetFormatter(&log.JSONFormatter{}) l.SetLevel(level) -- 2.49.1 From bf25a693409a535f633517c342313e8206c96509 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Tue, 17 Jun 2025 22:14:16 -0400 Subject: [PATCH 4/4] fix: correct context key constant definition in log.go --- logger/log.go | 1 - 1 file changed, 1 deletion(-) diff --git a/logger/log.go b/logger/log.go index c781b4f..e5518a2 100644 --- a/logger/log.go +++ b/logger/log.go @@ -7,7 +7,6 @@ import ( type contextKeyType string -const contextKey contextKeyType = "logger const contextKey contextKeyType = "logger" type Interface interface { -- 2.49.1