mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-09 20:29:16 +08:00
Compare commits
7 Commits
64450154f0
...
f9714dc8dd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f9714dc8dd | ||
|
|
c3d5a28ed6 | ||
|
|
acc55e049e | ||
|
|
0c0e99d253 | ||
|
|
dceb61e6e7 | ||
|
|
5e5ff3ace4 | ||
|
|
d905a35c21 |
16
.github/workflows/gin.yml
vendored
16
.github/workflows/gin.yml
vendored
@ -81,19 +81,3 @@ jobs:
|
||||
uses: codecov/codecov-action@v5
|
||||
with:
|
||||
flags: ${{ matrix.os }},go-${{ matrix.go }},${{ matrix.test-tags }}
|
||||
|
||||
vulnerability-scanning:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Run Trivy vulnerability scanner in repo mode
|
||||
uses: aquasecurity/trivy-action@0.33.1
|
||||
with:
|
||||
scan-type: "fs"
|
||||
ignore-unfixed: true
|
||||
format: "table"
|
||||
exit-code: "1"
|
||||
severity: "CRITICAL,HIGH,MEDIUM"
|
||||
|
||||
57
.github/workflows/trivy-scan.yml
vendored
Normal file
57
.github/workflows/trivy-scan.yml
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
name: Trivy Security Scan
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
schedule:
|
||||
# Run every 3 months (quarterly) on the 1st day at 00:00 UTC
|
||||
# Months: January (1), April (4), July (7), October (10)
|
||||
- cron: '0 0 1 1,4,7,10 *'
|
||||
workflow_dispatch: # Allow manual trigger
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
security-events: write # Required for uploading SARIF results
|
||||
|
||||
jobs:
|
||||
trivy-scan:
|
||||
name: Trivy Security Scan
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Run Trivy vulnerability scanner (source code)
|
||||
uses: aquasecurity/trivy-action@0.33.1
|
||||
with:
|
||||
scan-type: 'fs'
|
||||
scan-ref: '.'
|
||||
scanners: 'vuln,secret,misconfig'
|
||||
format: 'sarif'
|
||||
output: 'trivy-results.sarif'
|
||||
severity: 'CRITICAL,HIGH,MEDIUM'
|
||||
ignore-unfixed: true
|
||||
|
||||
- name: Upload Trivy results to GitHub Security tab
|
||||
uses: github/codeql-action/upload-sarif@v4
|
||||
if: always()
|
||||
with:
|
||||
sarif_file: 'trivy-results.sarif'
|
||||
|
||||
- name: Run Trivy scanner (table output for logs)
|
||||
uses: aquasecurity/trivy-action@0.33.1
|
||||
if: always()
|
||||
with:
|
||||
scan-type: 'fs'
|
||||
scan-ref: '.'
|
||||
scanners: 'vuln,secret,misconfig'
|
||||
format: 'table'
|
||||
severity: 'CRITICAL,HIGH,MEDIUM'
|
||||
ignore-unfixed: true
|
||||
exit-code: '1'
|
||||
11
README.md
11
README.md
@ -3,6 +3,7 @@
|
||||
<img align="right" width="159px" src="https://raw.githubusercontent.com/gin-gonic/logo/master/color.png">
|
||||
|
||||
[](https://github.com/gin-gonic/gin/actions/workflows/gin.yml)
|
||||
[](https://github.com/gin-gonic/gin/actions/workflows/trivy-scan.yml)
|
||||
[](https://codecov.io/gh/gin-gonic/gin)
|
||||
[](https://goreportcard.com/report/github.com/gin-gonic/gin)
|
||||
[](https://pkg.go.dev/github.com/gin-gonic/gin?tab=doc)
|
||||
@ -62,6 +63,7 @@ Here's a complete example that demonstrates Gin's simplicity:
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -70,7 +72,7 @@ import (
|
||||
func main() {
|
||||
// Create a Gin router with default middleware (logger and recovery)
|
||||
r := gin.Default()
|
||||
|
||||
|
||||
// Define a simple GET endpoint
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
// Return JSON response
|
||||
@ -78,10 +80,12 @@ func main() {
|
||||
"message": "pong",
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
// Start server on port 8080 (default)
|
||||
// Server will listen on 0.0.0.0:8080 (localhost:8080 on Windows)
|
||||
r.Run()
|
||||
if err := r.Run(); err != nil {
|
||||
log.Fatalf("failed to run server: %v", err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -190,7 +194,6 @@ Gin has a rich ecosystem of middleware for common web development needs. Explore
|
||||
- CORS, Rate limiting, Compression
|
||||
- Logging, Metrics, Tracing
|
||||
- Static file serving, Template engines
|
||||
|
||||
- **[gin-gonic/contrib](https://github.com/gin-gonic/contrib)** - Additional community middleware
|
||||
|
||||
## 🏢 Production Usage
|
||||
|
||||
@ -184,8 +184,15 @@ type BindUnmarshaler interface {
|
||||
// If the value implements the BindUnmarshaler interface, it will be used to set the value, we will return `true`
|
||||
// to skip the default value setting.
|
||||
func trySetCustom(val string, value reflect.Value) (isSet bool, err error) {
|
||||
switch v := value.Addr().Interface().(type) {
|
||||
if value.Kind() != reflect.Ptr {
|
||||
value = value.Addr()
|
||||
}
|
||||
switch value.Interface().(type) {
|
||||
case BindUnmarshaler:
|
||||
if !value.Elem().IsValid() {
|
||||
value.Set(reflect.New(value.Type().Elem()))
|
||||
}
|
||||
v := value.Interface().(BindUnmarshaler)
|
||||
return true, v.UnmarshalParam(val)
|
||||
}
|
||||
return false, nil
|
||||
@ -456,6 +463,13 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
|
||||
|
||||
func setArray(vals []string, value reflect.Value, field reflect.StructField) error {
|
||||
for i, s := range vals {
|
||||
if ok, err := trySetCustom(s, value.Index(i)); ok {
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
err := setWithProperType(s, value.Index(i), field)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@ -550,6 +550,54 @@ func TestMappingCustomPointerStructTypeWithURITag(t *testing.T) {
|
||||
assert.Equal(t, "happiness", s.FileData.Name)
|
||||
}
|
||||
|
||||
func TestMappingCustomStructTypeSliceWithFormTag(t *testing.T) {
|
||||
var s struct {
|
||||
FileData []customUnmarshalParamType `form:"data"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"data": {`file:/foo:happiness`, `http:/bar:sadness`}}, "form")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "file", s.FileData[0].Protocol)
|
||||
assert.Equal(t, "/foo", s.FileData[0].Path)
|
||||
assert.Equal(t, "happiness", s.FileData[0].Name)
|
||||
|
||||
assert.Equal(t, "http", s.FileData[1].Protocol)
|
||||
assert.Equal(t, "/bar", s.FileData[1].Path)
|
||||
assert.Equal(t, "sadness", s.FileData[1].Name)
|
||||
}
|
||||
|
||||
func TestMappingCustomStructTypeSliceWithURITag(t *testing.T) {
|
||||
var s struct {
|
||||
FileData []customUnmarshalParamType `uri:"data"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"data": {`file:/foo:happiness`, `http:/bar:sadness`}}, "uri")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "file", s.FileData[0].Protocol)
|
||||
assert.Equal(t, "/foo", s.FileData[0].Path)
|
||||
assert.Equal(t, "happiness", s.FileData[0].Name)
|
||||
|
||||
assert.Equal(t, "http", s.FileData[1].Protocol)
|
||||
assert.Equal(t, "/bar", s.FileData[1].Path)
|
||||
assert.Equal(t, "sadness", s.FileData[1].Name)
|
||||
}
|
||||
|
||||
func TestMappingCustomPointerStructTypeSliceWithFormTag(t *testing.T) {
|
||||
var s struct {
|
||||
FileData []*customUnmarshalParamType `form:"data"`
|
||||
}
|
||||
err := mappingByPtr(&s, formSource{"data": {`file:/foo:happiness`, `http:/bar:sadness`}}, "form")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "file", s.FileData[0].Protocol)
|
||||
assert.Equal(t, "/foo", s.FileData[0].Path)
|
||||
assert.Equal(t, "happiness", s.FileData[0].Name)
|
||||
|
||||
assert.Equal(t, "http", s.FileData[1].Protocol)
|
||||
assert.Equal(t, "/bar", s.FileData[1].Path)
|
||||
assert.Equal(t, "sadness", s.FileData[1].Name)
|
||||
}
|
||||
|
||||
type customPath []string
|
||||
|
||||
func (p *customPath) UnmarshalParam(param string) error {
|
||||
|
||||
22
context.go
22
context.go
@ -39,6 +39,7 @@ const (
|
||||
MIMEYAML = binding.MIMEYAML
|
||||
MIMEYAML2 = binding.MIMEYAML2
|
||||
MIMETOML = binding.MIMETOML
|
||||
MIMEPROTOBUF = binding.MIMEPROTOBUF
|
||||
)
|
||||
|
||||
// BodyBytesKey indicates a default body bytes key.
|
||||
@ -1280,14 +1281,15 @@ func (c *Context) Stream(step func(w io.Writer) bool) bool {
|
||||
|
||||
// Negotiate contains all negotiations data.
|
||||
type Negotiate struct {
|
||||
Offered []string
|
||||
HTMLName string
|
||||
HTMLData any
|
||||
JSONData any
|
||||
XMLData any
|
||||
YAMLData any
|
||||
Data any
|
||||
TOMLData any
|
||||
Offered []string
|
||||
HTMLName string
|
||||
HTMLData any
|
||||
JSONData any
|
||||
XMLData any
|
||||
YAMLData any
|
||||
Data any
|
||||
TOMLData any
|
||||
PROTOBUFData any
|
||||
}
|
||||
|
||||
// Negotiate calls different Render according to acceptable Accept format.
|
||||
@ -1313,6 +1315,10 @@ func (c *Context) Negotiate(code int, config Negotiate) {
|
||||
data := chooseData(config.TOMLData, config.Data)
|
||||
c.TOML(code, data)
|
||||
|
||||
case binding.MIMEPROTOBUF:
|
||||
data := chooseData(config.PROTOBUFData, config.Data)
|
||||
c.ProtoBuf(code, data)
|
||||
|
||||
default:
|
||||
c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) //nolint: errcheck
|
||||
}
|
||||
|
||||
@ -1628,6 +1628,32 @@ func TestContextNegotiationWithHTML(t *testing.T) {
|
||||
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
func TestContextNegotiationWithPROTOBUF(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
|
||||
reps := []int64{int64(1), int64(2)}
|
||||
label := "test"
|
||||
data := &testdata.Test{
|
||||
Label: &label,
|
||||
Reps: reps,
|
||||
}
|
||||
|
||||
c.Negotiate(http.StatusCreated, Negotiate{
|
||||
Offered: []string{MIMEPROTOBUF, MIMEJSON, MIMEXML},
|
||||
Data: data,
|
||||
})
|
||||
|
||||
// Marshal original data for comparison
|
||||
protoData, err := proto.Marshal(data)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, http.StatusCreated, w.Code)
|
||||
assert.Equal(t, string(protoData), w.Body.String())
|
||||
assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
func TestContextNegotiationNotSupport(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user