This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
76 lines (59 loc) · 1.55 KB
/
context.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gosrv
import "cyberpull.com/gotk/v2/errors"
type Context interface {
ParseContent(v any) (err error)
Update(v any, codes ...int) (n int, err error)
UpdateAll(v any, codes ...int)
Output(v any, code ...int) (data Output)
Success(v any) (data Output)
Error(v any, code ...int) (data Output)
}
// ======================
type pContext struct {
server *pServer
client *pClientConn
request *pRequest
}
func (ctx *pContext) ParseContent(v any) (err error) {
return ctx.request.ParseContent(v)
}
func (ctx *pContext) Update(v any, codes ...int) (n int, err error) {
data, err := newUpdate(ctx.request.Method, ctx.request.Channel, v, codes...)
if err != nil {
return
}
return writeUpdate(ctx.client, data)
}
func (ctx *pContext) UpdateAll(v any, codes ...int) {
data, err := newUpdate(ctx.request.Method, ctx.request.Channel, v, codes...)
if err != nil {
return
}
go func() {
for _, instance := range ctx.server.instances {
writeUpdate(instance.client, data)
}
}()
}
func (ctx *pContext) Output(v any, codes ...int) (data Output) {
code := one(200, codes)
return &pOutput{
Code: code,
Content: v,
}
}
func (ctx *pContext) Success(v any) (data Output) {
return ctx.Output(v, 200)
}
func (ctx *pContext) Error(v any, code ...int) (data Output) {
err := errors.From(v, code...)
return ctx.Output(err.Error(), err.Code())
}
// ======================
func newContext(instance *serverClientInstance, request *pRequest) *pContext {
return &pContext{
server: instance.server,
client: instance.client,
request: request,
}
}