-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_http_rule.go
53 lines (47 loc) · 2.13 KB
/
service_http_rule.go
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
package dao
import (
"github.com/e421083458/gorm"
"github.com/gin-gonic/gin"
"github.com/yguilai/go-gateway/public"
)
type HttpRule struct {
ID int64 `json:"id" gorm:"primary_key"`
ServiceID int64 `json:"service_id" gorm:"column:service_id" description:"服务id"`
RuleType int `json:"rule_type" gorm:"column:rule_type" description:"匹配类型 domain=域名, url_prefix=url前缀"`
Rule string `json:"rule" gorm:"column:rule" description:"type=domain表示域名,type=url_prefix时表示url前缀"`
NeedHttps int `json:"need_https" gorm:"column:need_https" description:"type=支持https 1=支持"`
NeedWebsocket int `json:"need_websocket" gorm:"column:need_websocket" description:"启用websocket 1=启用"`
NeedStripUri int `json:"need_strip_uri" gorm:"column:need_strip_uri" description:"启用strip_uri 1=启用"`
UrlRewrite string `json:"url_rewrite" gorm:"column:url_rewrite" description:"url重写功能,每行一个 "`
HeaderTransfor string `json:"header_transfor" gorm:"column:header_transfor" description:"header转换支持增加(add)、删除(del)、修改(edit) 格式: add headname headvalue "`
}
func (t *HttpRule) TableName() string {
return "gateway_service_http_rule"
}
func (t *HttpRule) Find(c *gin.Context, tx *gorm.DB, search *HttpRule) (*HttpRule, error) {
model := &HttpRule{}
err := tx.SetCtx(public.GetGinTraceContext(c)).Where(search).Find(model).Error
return model, err
}
func (t *HttpRule) Save(c *gin.Context, tx *gorm.DB) error {
if err := tx.SetCtx(public.GetGinTraceContext(c)).Save(t).Error; err != nil {
return err
}
return nil
}
func (t *HttpRule) ListByServiceID(c *gin.Context, tx *gorm.DB, serviceID int64) ([]HttpRule, int64, error) {
var list []HttpRule
var count int64
query := tx.SetCtx(public.GetGinTraceContext(c))
query = query.Table(t.TableName()).Select("*")
query = query.Where("service_id=?", serviceID)
err := query.Order("id desc").Find(&list).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, 0, err
}
errCount := query.Count(&count).Error
if errCount != nil {
return nil, 0, err
}
return list, count, nil
}