Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dependencies update + more test codes + sample middleware code #26

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
# Allow up to 10 open pull requests for dependencies
open-pull-requests-limit: 10

- package-ecosystem: github-actions
directory: /
schedule:
interval: daily
76 changes: 76 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches:
- main
pull_request:
# The branches below must be a subset of the branches above
branches:
- main
schedule:
- cron: "19 7 * * 2"

permissions:
contents: read

jobs:
CodeQL:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ["go"]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
23 changes: 23 additions & 0 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Dependency Review Action
#
# This Action will scan dependency manifest files that change as part of a Pull Request,
# surfacing known-vulnerable versions of the packages declared or updated in the PR.
# Once installed, if the workflow run is marked as required,
# PRs introducing known-vulnerable packages will be blocked from merging.
#
# Source repository: https://github.com/actions/dependency-review-action
name: "Dependency Review"
on: [pull_request]

permissions:
contents: read

jobs:
dependency-review:
runs-on: ubuntu-latest
steps:
- name: "Checkout Repository"
uses: actions/checkout@v4

- name: "Dependency Review"
uses: actions/dependency-review-action@v4
63 changes: 63 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Go

on:
push:
branches:
- main
pull_request:
branches:
- main

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
permissions:
actions: read
contents: read

steps:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.22.5
- run: go version

- name: Checkout repository
uses: actions/checkout@v4

- name: Examine source code for Linux AMD
run: GOOS=linux GOARCH=amd64 go vet -v ./...

- name: Examine source code for MacOS AMD
run: GOOS=darwin GOARCH=amd64 go vet -v ./...

- name: Examine source code for Windows AMD
run: GOOS=windows GOARCH=amd64 go vet -v ./...

- name: Examine source code for Linux ARM
run: GOOS=linux GOARCH=arm64 go vet -v ./...

- name: Examine source code for MacOS ARM
run: GOOS=darwin GOARCH=arm64 go vet -v ./...

- name: Examine source code for Windows ARM
run: GOOS=windows GOARCH=arm64 go vet -v ./...

- name: Gosec security scanner
uses: securego/gosec@master
with:
args: ./...

- name: Test
if: github.event_name == 'push'
run: go test -v -cover -coverprofile=coverage.txt -covermode=atomic ./...

- name: Upload coverage reports to Codecov
if: github.event_name == 'push'
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: pilinux/logrus
44 changes: 44 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- main
pull_request:
branches:
- main
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest

steps:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.22.5
- run: go version

- name: Checkout repository
uses: actions/checkout@v4

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.59.1

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
args: --timeout 5m --verbose

# Optional: show only new issues if it's a pull request. The default value is `false`.
only-new-issues: true
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Hooks for [logrus](https://github.com/Sirupsen/logrus)

_Note:_ This is a fork of [onrik/logrus](https://github.com/onrik/logrus)

Due to the slow response of the original author, I have decided to fork the repo and maintain it myself.

## Example

```go
Expand All @@ -8,8 +12,8 @@ package main
import (
"fmt"

"github.com/onrik/logrus/filename"
"github.com/onrik/logrus/sentry"
"github.com/pilinux/logrus/filename"
"github.com/pilinux/logrus/sentry"
log "github.com/sirupsen/logrus"
)

Expand Down
2 changes: 1 addition & 1 deletion echo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main

import (
"github.com/labstack/echo/v4"
echolog "github.com/onrik/logrus/echo"
echolog "github.com/pilinux/logrus/echo"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's redundant.

"github.com/sirupsen/logrus"
)

Expand Down
47 changes: 47 additions & 0 deletions example/sentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package example

// github.com/pilinux/gorest
// The MIT License (MIT)
// Copyright (c) 2023 pilinux

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/pilinux/logrus/sentry"
log "github.com/sirupsen/logrus"
)

// SentryCapture - capture errors and forward to sentry.io
func SentryCapture(sentryDsn string) gin.HandlerFunc {
return func(c *gin.Context) {
// Automatic recovery from panic
defer func() {
if r := recover(); r != nil {
log.Error("panic msg: middleware -> sentry panicked")
}
}()

sentryHook, err := sentry.NewHook(sentry.Options{
Dsn: sentryDsn,
Debug: true,
Environment: "development",
Release: "1.0.0",
})
if err != nil {
// middleware -> sentry NewHook failed
c.AbortWithStatusJSON(http.StatusInternalServerError, "internal server error")
return
}
sentryHook.AddTag("method", c.Request.Method)
sentryHook.AddTag("path", c.Request.URL.Path)
sentryHook.AddTag("host", c.Request.Host)
sentryHook.AddTag("remote.addr", c.Request.RemoteAddr)
sentryHook.AddTag("user.agent", c.Request.UserAgent())
defer sentryHook.Flush()

log.AddHook(sentryHook)

c.Next()
}
}
49 changes: 49 additions & 0 deletions example/sentry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package example

import (
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

func TestSentryCapture(t *testing.T) {
// set up a gin router and handler
gin.SetMode(gin.TestMode)
router := gin.New()
err := router.SetTrustedProxies(nil)
if err != nil {
t.Errorf("failed to set trusted proxies to nil")
}
router.TrustedPlatform = "X-Real-Ip"

// register middleware with valid sentry dsn
sentryDSN := os.Getenv("TEST_SENTRY_DSN")
router.Use(SentryCapture(sentryDSN))

// define test route
router.GET("/", func(c *gin.Context) {
// send log to sentry for testing
log.Info("testing sentry integration")
c.Status(http.StatusOK)
})

// perform request and get response
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Errorf("failed to create an HTTP request")
return
}
req.Header.Set("User-Agent", "Test-User-Agent")
res := httptest.NewRecorder()

router.ServeHTTP(res, req)

// check response
if res.Code != http.StatusOK {
t.Errorf("expected response code %v, got '%v'", http.StatusOK, res.Code)
}
}
2 changes: 1 addition & 1 deletion gin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main

import (
"github.com/gin-gonic/gin"
ginlog "github.com/onrik/logrus/gin"
ginlog "github.com/pilinux/logrus/gin"
"github.com/sirupsen/logrus"
)

Expand Down
Loading