You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@casbin.apache.org by hs...@apache.org on 2026/08/17 04:39:52 UTC
(casbin-gateway) branch master updated: ci: add golangci-lint workflow
This is an automated email from the ASF dual-hosted git repository.
hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-gateway.git
The following commit(s) were added to refs/heads/master by this push:
new d1eae26 ci: add golangci-lint workflow
d1eae26 is described below
commit d1eae26f443dad989d94c676d24d8a2545d898dd
Author: Yang Luo <hs...@gmail.com>
AuthorDate: Mon Aug 17 12:39:46 2026 +0800
ci: add golangci-lint workflow
---
.github/workflows/golangci-lint.yml | 31 +++++++++++++++++++++++++++++++
.golangci.yml | 18 ++++++++++++++++++
rule/rule.go | 10 +++++-----
service/proxy.go | 11 +++++------
util/supervisor.go | 32 ++++++++++++++++----------------
5 files changed, 75 insertions(+), 27 deletions(-)
diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml
new file mode 100644
index 0000000..79039f2
--- /dev/null
+++ b/.github/workflows/golangci-lint.yml
@@ -0,0 +1,31 @@
+name: golangci-lint
+
+on:
+ push:
+ branches:
+ - master
+ pull_request:
+ branches:
+ - master
+
+permissions:
+ contents: read
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
+jobs:
+ golangci:
+ name: Lint
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Set up Go
+ uses: actions/setup-go@v5
+ with:
+ go-version: '1.21'
+
+ - name: golangci-lint
+ uses: golangci/golangci-lint-action@v8
diff --git a/.golangci.yml b/.golangci.yml
new file mode 100644
index 0000000..ac5dd08
--- /dev/null
+++ b/.golangci.yml
@@ -0,0 +1,18 @@
+version: "2"
+
+linters:
+ default: none
+ enable:
+ - govet
+ - misspell
+ settings:
+ govet:
+ disable:
+ # Reports every stdlib call that the newer toolchain golangci-lint is
+ # built with cannot inline into a "go 1.20" file. Not actionable here.
+ - inline
+
+formatters:
+ enable:
+ - gofmt
+ - goimports
diff --git a/rule/rule.go b/rule/rule.go
index 427a781..c8c705d 100644
--- a/rule/rule.go
+++ b/rule/rule.go
@@ -62,13 +62,13 @@ func CheckRules(ruleIds []string, r *http.Request) (*RuleResult, error) {
if err != nil {
return nil, err
}
-
+
if result != nil {
// Use rule's action if no action specified by the rule check
if result.Action == "" {
result.Action = rule.Action
}
-
+
// Determine status code
if result.StatusCode == 0 {
if rule.StatusCode != 0 {
@@ -89,7 +89,7 @@ func CheckRules(ruleIds []string, r *http.Request) (*RuleResult, error) {
}
}
}
-
+
// Update reason if rule has custom reason
if result.Action == "Block" || result.Action == "Drop" {
if rule.IsVerbose {
@@ -101,11 +101,11 @@ func CheckRules(ruleIds []string, r *http.Request) (*RuleResult, error) {
result.Reason = fmt.Sprintf("hit rule %s: %s", ruleIds[i], result.Reason)
}
}
-
+
return result, nil
}
}
-
+
// Default action if no rule matched
return &RuleResult{
Action: "Allow",
diff --git a/service/proxy.go b/service/proxy.go
index 5f9bb21..f43bfed 100644
--- a/service/proxy.go
+++ b/service/proxy.go
@@ -35,7 +35,6 @@ func forwardHandler(targetUrl string, writer http.ResponseWriter, request *http.
if nil != err {
panic(err)
- return
}
proxy := httputil.NewSingleHostReverseProxy(target)
@@ -70,10 +69,10 @@ func forwardHandler(targetUrl string, writer http.ResponseWriter, request *http.
for _, cookie := range cookies {
// Check if Secure attribute is already present (case-insensitive)
cookieLower := strings.ToLower(cookie)
- hasSecure := strings.Contains(cookieLower, ";secure;") ||
- strings.Contains(cookieLower, "; secure;") ||
- strings.HasSuffix(cookieLower, ";secure") ||
- strings.HasSuffix(cookieLower, "; secure")
+ hasSecure := strings.Contains(cookieLower, ";secure;") ||
+ strings.Contains(cookieLower, "; secure;") ||
+ strings.HasSuffix(cookieLower, ";secure") ||
+ strings.HasSuffix(cookieLower, "; secure")
if !hasSecure {
cookie = cookie + "; Secure"
}
@@ -85,7 +84,7 @@ func forwardHandler(targetUrl string, writer http.ResponseWriter, request *http.
// Fix CORS issue: Remove CORS header combinations that allow credential theft from any origin
allowOrigin := resp.Header.Get("Access-Control-Allow-Origin")
allowCredentials := resp.Header.Get("Access-Control-Allow-Credentials")
-
+
// Remove CORS headers when the combination is present:
// 1. Access-Control-Allow-Credentials: true with Access-Control-Allow-Origin: *
// This is actually blocked by browsers but we sanitize it anyway
diff --git a/util/supervisor.go b/util/supervisor.go
index 898de49..c58c514 100644
--- a/util/supervisor.go
+++ b/util/supervisor.go
@@ -48,7 +48,7 @@ func InitSelfGuard() {
// Already supervised, just return and continue normal execution
return
}
-
+
// Start as supervisor
err := runSupervisor()
if err != nil {
@@ -62,9 +62,9 @@ func InitSelfGuard() {
// runSupervisor starts the supervisor that monitors and restarts the main process
func runSupervisor() error {
fmt.Println("Starting Casbin Gateway with auto-recovery mechanism...")
-
+
restartTimes := []time.Time{}
-
+
for {
// Clean up old restart times outside the window
now := time.Now()
@@ -75,46 +75,46 @@ func runSupervisor() error {
}
}
restartTimes = validRestarts
-
+
// Check if we've exceeded max restarts
if len(restartTimes) >= MaxRestarts {
return fmt.Errorf("exceeded maximum restart limit (%d restarts in %v), stopping supervisor", MaxRestarts, RestartWindow)
}
-
+
// Start the child process
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
-
+
// Set environment variable to indicate supervised process
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", EnvSupervisorKey))
-
+
// Start the process
if err := cmd.Start(); err != nil {
fmt.Printf("Failed to start process: %v\n", err)
return err
}
-
+
processStartTime := time.Now()
fmt.Printf("Started supervised process with PID: %d\n", cmd.Process.Pid)
-
+
// Setup signal handling to forward signals to child
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
-
+
// Wait for process completion or signal
doneChan := make(chan error, 1)
go func() {
doneChan <- cmd.Wait()
}()
-
+
select {
case err := <-doneChan:
// Process exited
exitTime := time.Now()
uptime := exitTime.Sub(processStartTime)
-
+
if err != nil {
// A configuration failure repeats on every restart, so pass the
// child's exit code up instead of burning through the restart
@@ -127,11 +127,11 @@ func runSupervisor() error {
// Record this restart
restartTimes = append(restartTimes, time.Now())
-
- fmt.Printf("Waiting %v before restarting... (restart %d/%d)\n",
+
+ fmt.Printf("Waiting %v before restarting... (restart %d/%d)\n",
RestartDelay, len(restartTimes), MaxRestarts)
time.Sleep(RestartDelay)
-
+
// Continue to restart
continue
} else {
@@ -139,7 +139,7 @@ func runSupervisor() error {
fmt.Println("Process exited cleanly")
return nil
}
-
+
case sig := <-sigChan:
// Received shutdown signal, forward to child
fmt.Printf("Received signal %v, forwarding to child process...\n", sig)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@casbin.apache.org
For additional commands, e-mail: commits-help@casbin.apache.org