-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrator.go
More file actions
152 lines (136 loc) · 4.33 KB
/
integrator.go
File metadata and controls
152 lines (136 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package admingin
import (
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
admin "github.com/ovnicraft/go-advanced-admin"
)
// Integrator adapts the admin panel to a gin.RouterGroup.
type Integrator struct {
group *gin.RouterGroup
}
// NewIntegrator creates a new Integrator for the given gin.RouterGroup.
func NewIntegrator(group *gin.RouterGroup) *Integrator {
return &Integrator{group: group}
}
// HandleRoute registers a route with the given method, path, and handler function.
// It adapts the admin.HandlerFunc to a gin.HandlerFunc.
func (i *Integrator) HandleRoute(method, path string, handler admin.HandlerFunc) {
i.group.Handle(method, path, func(c *gin.Context) {
code, body := handler(c)
switch int(code) {
case http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther,
http.StatusTemporaryRedirect, http.StatusPermanentRedirect:
c.Redirect(int(code), body)
return
}
c.Data(int(code), "text/html; charset=utf-8", []byte(body))
})
}
// HandleJSONRoute registers a route that returns JSON responses.
func (i *Integrator) HandleJSONRoute(method, path string, handler admin.JSONHandlerFunc) {
i.group.Handle(method, path, func(c *gin.Context) {
if err := handler(c); err != nil {
_ = i.SetJSONResponse(c, http.StatusInternalServerError, admin.NewErrorResponse([]string{err.Error()}))
}
})
}
// ServeAssets serves static assets under the specified prefix using the provided renderer.
func (i *Integrator) ServeAssets(prefix string, renderer admin.TemplateRenderer) {
p := "/" + strings.TrimPrefix(prefix, "/")
grp := i.group.Group(p)
grp.HEAD("/*filepath", func(c *gin.Context) {
path := strings.TrimPrefix(c.Param("filepath"), "/")
if _, err := renderer.GetAsset(path); err != nil {
c.Status(http.StatusNotFound)
return
}
c.Status(http.StatusOK)
})
grp.GET("/*filepath", func(c *gin.Context) {
path := strings.TrimPrefix(c.Param("filepath"), "/")
data, err := renderer.GetAsset(path)
if err != nil {
c.Status(http.StatusNotFound)
return
}
c.Data(http.StatusOK, detectContentType(path), data)
})
}
// GetQueryParam retrieves the value of a query parameter from the context.
func (i *Integrator) GetQueryParam(ctx interface{}, name string) string {
if c, ok := ctx.(*gin.Context); ok {
return c.Query(name)
}
return ""
}
// GetPathParam retrieves the value of a path parameter from the context.
func (i *Integrator) GetPathParam(ctx interface{}, name string) string {
if c, ok := ctx.(*gin.Context); ok {
return c.Param(name)
}
return ""
}
// GetRequestMethod retrieves the HTTP method of the request from the context.
func (i *Integrator) GetRequestMethod(ctx interface{}) string {
if c, ok := ctx.(*gin.Context); ok {
return c.Request.Method
}
return ""
}
// GetFormData retrieves form data from the context.
func (i *Integrator) GetFormData(ctx interface{}) map[string][]string {
if c, ok := ctx.(*gin.Context); ok {
if err := c.Request.ParseForm(); err != nil {
return nil
}
return c.Request.Form
}
return nil
}
// SetJSONResponse sets a JSON response with the given status code and data.
func (i *Integrator) SetJSONResponse(ctx interface{}, statusCode int, data interface{}) error {
c, ok := ctx.(*gin.Context)
if !ok {
return fmt.Errorf("invalid context type")
}
c.JSON(statusCode, data)
return nil
}
// GetJSONBody retrieves JSON body data from the request context.
func (i *Integrator) GetJSONBody(ctx interface{}) (map[string]interface{}, error) {
c, ok := ctx.(*gin.Context)
if !ok {
return nil, fmt.Errorf("invalid context type")
}
var body map[string]interface{}
if err := c.ShouldBindJSON(&body); err != nil {
return nil, err
}
return body, nil
}
func detectContentType(path string) string {
switch {
case strings.HasSuffix(path, ".css"):
return "text/css; charset=utf-8"
case strings.HasSuffix(path, ".js"):
return "application/javascript"
case strings.HasSuffix(path, ".png"):
return "image/png"
case strings.HasSuffix(path, ".jpg"), strings.HasSuffix(path, ".jpeg"):
return "image/jpeg"
case strings.HasSuffix(path, ".svg"):
return "image/svg+xml"
case strings.HasSuffix(path, ".woff2"):
return "font/woff2"
case strings.HasSuffix(path, ".woff"):
return "font/woff"
case strings.HasSuffix(path, ".ttf"):
return "font/ttf"
case strings.HasSuffix(path, ".map"):
return "application/json"
default:
return "application/octet-stream"
}
}