Skip to content

Commit f195af1

Browse files
authored
Revert "Feat/auth (#72)" (#73)
1 parent b132a53 commit f195af1

File tree

11 files changed

+767
-68
lines changed

11 files changed

+767
-68
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ SRC_DIR=$(GOPATH)/src
1515

1616
.PHONY: proto
1717
proto:
18-
#protoc-go-inject-tag -I ./proto -I ${GOPATH}/src --go_out=plugins=grpc: proto/${W}/${V}/*;
19-
find proto/ -name '*.proto' -exec protoc --proto_path=$(PROTO_PATH) $(PROTO_FLAGS) --go_out=plugins=grpc:. {} \;
18+
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/*.proto
2019

2120

2221
.PHONY: lint

core/server/task/options.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package task
88
*/
99

1010
import (
11-
"sync"
12-
1311
"github.com/robfig/cron/v3"
1412
)
1513

@@ -23,26 +21,30 @@ type schedule struct {
2321
}
2422

2523
type options struct {
26-
task *cron.Cron
27-
schedules map[string]schedule
28-
mux sync.Mutex
24+
task *cron.Cron
25+
storage Storage
2926
}
3027

3128
// WithSchedule set schedule
3229
func WithSchedule(key string, spec string, job cron.Job) Option {
3330
return func(o *options) {
34-
o.mux.Lock()
35-
o.schedules[key] = schedule{
36-
spec: spec,
37-
job: job,
38-
}
39-
o.mux.Unlock()
31+
_ = o.storage.Set(key, 0, spec, job)
32+
}
33+
}
34+
35+
// WithStorage set storage
36+
func WithStorage(s Storage) Option {
37+
return func(o *options) {
38+
o.storage = s
4039
}
40+
4141
}
4242

4343
func setDefaultOption() options {
4444
return options{
45-
task: cron.New(cron.WithSeconds(), cron.WithChain()),
46-
schedules: make(map[string]schedule),
45+
task: cron.New(cron.WithSeconds(), cron.WithChain()),
46+
storage: &defaultStorage{
47+
schedules: make(map[string]*schedule),
48+
},
4749
}
4850
}

core/server/task/server.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,42 @@ func New(opts ...Option) *Server {
3232

3333
// GetJob get job
3434
func GetJob(key string) (string, cron.Job, bool) {
35-
task.opts.mux.Lock()
36-
defer task.opts.mux.Unlock()
37-
s, ok := task.opts.schedules[key]
35+
_, spec, job, ok, _ := task.opts.storage.Get(key)
3836
if !ok {
3937
return "", nil, false
4038
}
41-
return s.spec, s.job, true
39+
return spec, job, true
40+
}
41+
42+
// Entry get entry
43+
func Entry(entryID cron.EntryID) cron.Entry {
44+
return task.opts.task.Entry(entryID)
45+
4246
}
4347

4448
// UpdateJob update or create job
4549
func UpdateJob(key string, spec string, job cron.Job) error {
46-
task.opts.mux.Lock()
47-
defer task.opts.mux.Unlock()
48-
s, ok := task.opts.schedules[key]
50+
var err error
51+
entryID, _, _, ok, _ := task.opts.storage.Get(key)
4952
if ok {
50-
task.opts.task.Remove(s.entryID)
53+
task.opts.task.Remove(entryID)
5154
}
52-
entryID, err := task.opts.task.AddJob(spec, job)
55+
entryID, err = task.opts.task.AddJob(spec, job)
5356
if err != nil {
5457
slog.Error("task add job error", slog.Any("err", err))
5558
return err
5659
}
57-
task.opts.schedules[key] = schedule{
58-
spec: spec,
59-
job: job,
60-
entryID: entryID,
61-
}
62-
return nil
60+
return task.opts.storage.Update(key, entryID)
6361
}
6462

6563
// RemoveJob remove job
6664
func RemoveJob(key string) error {
67-
task.opts.mux.Lock()
68-
defer task.opts.mux.Unlock()
69-
s, ok := task.opts.schedules[key]
65+
entryID, _, _, ok, _ := task.opts.storage.Get(key)
7066
if !ok {
7167
return nil
7268
}
73-
task.opts.task.Remove(s.entryID)
74-
delete(task.opts.schedules, key)
75-
return nil
69+
task.opts.task.Remove(entryID)
70+
return task.opts.storage.Remove(key)
7671
}
7772

7873
// Options set options
@@ -91,13 +86,22 @@ func (e *Server) String() string {
9186
func (e *Server) Start(ctx context.Context) error {
9287
var err error
9388
e.ctx = ctx
94-
for i, s := range e.opts.schedules {
95-
s.entryID, err = e.opts.task.AddJob(e.opts.schedules[i].spec, e.opts.schedules[i].job)
89+
keys, _ := e.opts.storage.ListKeys()
90+
for i := range keys {
91+
_, spec, job, ok, _ := e.opts.storage.Get(keys[i])
92+
if !ok {
93+
continue
94+
}
95+
entryID, err := e.opts.task.AddJob(spec, job)
9696
if err != nil {
9797
slog.ErrorContext(ctx, "task add job error", slog.Any("err", err))
9898
return err
9999
}
100-
e.opts.schedules[i] = s
100+
err = e.opts.storage.Update(keys[i], entryID)
101+
if err != nil {
102+
slog.ErrorContext(ctx, "task update job error", slog.Any("err", err))
103+
return err
104+
}
101105
}
102106
go func() {
103107
e.opts.task.Run()

core/server/task/storage.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package task
2+
3+
import (
4+
"sync"
5+
6+
"github.com/robfig/cron/v3"
7+
)
8+
9+
/*
10+
* @Author: lwnmengjing<lwnmengjing@qq.com>
11+
* @Date: 2023/12/5 16:56:16
12+
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
13+
* @Last Modified time: 2023/12/5 16:56:16
14+
*/
15+
16+
// Storage storage interface
17+
type Storage interface {
18+
Get(key string) (entryID cron.EntryID, spec string, job cron.Job, exist bool, err error)
19+
Set(key string, entryID cron.EntryID, spec string, job cron.Job) error
20+
Update(key string, entryID cron.EntryID) error
21+
Remove(key string) error
22+
ListKeys() ([]string, error)
23+
}
24+
25+
type defaultStorage struct {
26+
schedules map[string]*schedule
27+
mux sync.Mutex
28+
}
29+
30+
// Get schedule
31+
func (s *defaultStorage) Get(key string) (entryID cron.EntryID, spec string, job cron.Job, exist bool, err error) {
32+
if s.schedules == nil {
33+
return
34+
}
35+
item, ok := s.schedules[key]
36+
if !ok {
37+
return
38+
}
39+
entryID = item.entryID
40+
spec = item.spec
41+
job = item.job
42+
exist = true
43+
return
44+
}
45+
46+
// Set schedule
47+
func (s *defaultStorage) Set(key string, entryID cron.EntryID, spec string, job cron.Job) error {
48+
s.mux.Lock()
49+
defer s.mux.Unlock()
50+
if s.schedules == nil {
51+
s.schedules = make(map[string]*schedule)
52+
}
53+
s.schedules[key] = &schedule{
54+
spec: spec,
55+
entryID: entryID,
56+
job: job,
57+
}
58+
return nil
59+
}
60+
61+
// Update schedule
62+
func (s *defaultStorage) Update(key string, entryID cron.EntryID) error {
63+
if s.schedules == nil {
64+
s.schedules = make(map[string]*schedule)
65+
return nil
66+
}
67+
item, ok := s.schedules[key]
68+
if !ok {
69+
return nil
70+
}
71+
item.entryID = entryID
72+
return nil
73+
}
74+
75+
func (s *defaultStorage) Remove(key string) error {
76+
if s.schedules == nil {
77+
return nil
78+
}
79+
delete(s.schedules, key)
80+
return nil
81+
}
82+
83+
// ListKeys list keys
84+
func (s *defaultStorage) ListKeys() ([]string, error) {
85+
keys := make([]string, 0, len(s.schedules))
86+
for k := range s.schedules {
87+
keys = append(keys, k)
88+
}
89+
return keys, nil
90+
}

pkg/response/actions/get_gorm.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ func NewGetGorm(m schema.Tabler, key string) *Get {
3333
func (e *Get) getGorm(c *gin.Context, key string) {
3434
api := response.Make(c)
3535
m := pkg.TablerDeepCopy(e.ModelGorm)
36-
preloads := c.QueryArray("preloads[]")
37-
query := gormdb.DB.Model(m).Where("id = ?", c.Param(key))
38-
for _, preload := range preloads {
39-
query = query.Preload(preload)
40-
}
41-
err := query.First(m).Error
36+
err := gormdb.DB.First(m, "id = ?", c.Param(key)).Error
4237
if err != nil {
4338
if errors.Is(err, gorm.ErrRecordNotFound) {
4439
api.Err(http.StatusNotFound)

pkg/response/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ func (e *API) AddError(err error) *API {
104104
} else if err != nil {
105105
e.Error = fmt.Errorf("%v; %w", e.Error, err)
106106
}
107+
if e.Error != nil {
108+
e.Log = e.Log.With("error", e.Error)
109+
}
107110
return e
108111
}
109112

pkg/response/model.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ func (e *response) OK(c *gin.Context, data interface{}) {
9292
res := Default.Clone()
9393
res.SetList(data)
9494
res.SetTraceID(pkg.GenerateMsgIDFromContext(c))
95-
status := http.StatusOK
9695
switch c.Request.Method {
9796
case http.MethodDelete:
98-
status = http.StatusNoContent
97+
res.SetCode(http.StatusNoContent)
98+
c.AbortWithStatusJSON(http.StatusNoContent, data)
99+
return
99100
case http.MethodPost:
100-
status = http.StatusCreated
101-
}
102-
res.SetCode(status)
103-
if data == nil {
104-
c.AbortWithStatus(status)
101+
res.SetCode(http.StatusCreated)
102+
c.AbortWithStatusJSON(http.StatusCreated, data)
105103
return
104+
default:
105+
res.SetCode(http.StatusOK)
106+
c.AbortWithStatusJSON(http.StatusOK, data)
106107
}
107-
c.AbortWithStatusJSON(status, data)
108108
}
109109

110110
// PageOK page ok

pkg/search/gorms/query.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ func parseSQL(driver string, searchTag *resolveSearchTag, condition Condition, q
7474
if driver == Postgres {
7575
iStr = "i"
7676
}
77+
if searchTag.Table != "" {
78+
searchTag.Table = fmt.Sprintf("`%s`.", searchTag.Table)
79+
}
7780
switch searchTag.Type {
7881
case "left":
7982
//左关联
8083
join := condition.SetJoinOn(searchTag.Type, fmt.Sprintf(
81-
"left join %s on %s.%s = %s.%s",
84+
"left join `%s` on `%s`.`%s` = %s.`%s`",
8285
searchTag.Join,
8386
searchTag.Join,
8487
searchTag.On[0],
@@ -87,37 +90,37 @@ func parseSQL(driver string, searchTag *resolveSearchTag, condition Condition, q
8790
))
8891
ResolveSearchQuery(driver, qValue.Field(i).Interface(), join)
8992
case "exact", "iexact":
90-
condition.SetWhere(fmt.Sprintf("%s.%s = ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
93+
condition.SetWhere(fmt.Sprintf("%s`%s` = ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
9194
case "contains":
92-
condition.SetWhere(fmt.Sprintf("%s.%s like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String() + "%"})
95+
condition.SetWhere(fmt.Sprintf("%s`%s` like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String() + "%"})
9396
case "icontains":
94-
condition.SetWhere(fmt.Sprintf("%s.%s %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{"%" + qValue.Field(i).String() + "%"})
97+
condition.SetWhere(fmt.Sprintf("%s`%s` %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{"%" + qValue.Field(i).String() + "%"})
9598
case "gt":
96-
condition.SetWhere(fmt.Sprintf("%s.%s > ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
99+
condition.SetWhere(fmt.Sprintf("%s`%s` > ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
97100
case "gte":
98-
condition.SetWhere(fmt.Sprintf("%s.%s >= ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
101+
condition.SetWhere(fmt.Sprintf("%s`%s` >= ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
99102
case "lt":
100-
condition.SetWhere(fmt.Sprintf("%s.%s < ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
103+
condition.SetWhere(fmt.Sprintf("%s`%s` < ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
101104
case "lte":
102-
condition.SetWhere(fmt.Sprintf("%s.%s <= ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
105+
condition.SetWhere(fmt.Sprintf("%s`%s` <= ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
103106
case "startswith":
104-
condition.SetWhere(fmt.Sprintf("%s.%s like ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).String() + "%"})
107+
condition.SetWhere(fmt.Sprintf("%s`%s` like ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).String() + "%"})
105108
case "istartswith":
106-
condition.SetWhere(fmt.Sprintf("%s.%s %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{qValue.Field(i).String() + "%"})
109+
condition.SetWhere(fmt.Sprintf("%s`%s` %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{qValue.Field(i).String() + "%"})
107110
case "endswith":
108-
condition.SetWhere(fmt.Sprintf("%s.%s like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String()})
111+
condition.SetWhere(fmt.Sprintf("%s`%s` like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String()})
109112
case "iendswith":
110-
condition.SetWhere(fmt.Sprintf("%s.%s %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{"%" + qValue.Field(i).String()})
113+
condition.SetWhere(fmt.Sprintf("%s`%s` %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{"%" + qValue.Field(i).String()})
111114
case "in":
112-
condition.SetWhere(fmt.Sprintf("`%s`.`%s` in (?)", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
115+
condition.SetWhere(fmt.Sprintf("%s`%s` in (?)", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
113116
case "isnull":
114117
if !(qValue.Field(i).IsZero() && qValue.Field(i).IsNil()) {
115-
condition.SetWhere(fmt.Sprintf("%s.%s` isnull", searchTag.Table, searchTag.Column), make([]interface{}, 0))
118+
condition.SetWhere(fmt.Sprintf("%s`%s` isnull", searchTag.Table, searchTag.Column), make([]interface{}, 0))
116119
}
117120
case "order":
118121
switch strings.ToLower(qValue.Field(i).String()) {
119122
case "desc", "asc":
120-
condition.SetOrder(fmt.Sprintf("%s.%s %s", searchTag.Table, searchTag.Column, qValue.Field(i).String()))
123+
condition.SetOrder(fmt.Sprintf("%s`%s` %s", searchTag.Table, searchTag.Column, qValue.Field(i).String()))
121124
}
122125
}
123126
}

0 commit comments

Comments
 (0)