-
Notifications
You must be signed in to change notification settings - Fork 20
/
config.go
59 lines (49 loc) · 1.25 KB
/
config.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
package simplehttp
import (
"context"
"io"
"net/http"
"runtime"
"strings"
"unsafe"
tps "github.com/Asutorufa/yuhaiin/internal/http/templates"
"github.com/Asutorufa/yuhaiin/pkg/protos/config"
grpcconfig "github.com/Asutorufa/yuhaiin/pkg/protos/config/grpc"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/emptypb"
)
type configHandler struct {
emptyHTTP
cf grpcconfig.ConfigDaoServer
}
func (cc *configHandler) Get(w http.ResponseWriter, r *http.Request) error {
c, err := cc.cf.Load(context.TODO(), &emptypb.Empty{})
if err != nil {
return err
}
data, err := protojson.MarshalOptions{Indent: " ", EmitUnpopulated: true}.Marshal(c)
if err != nil {
return err
}
return TPS.BodyExecute(w, map[string]any{
"Config": *(*string)(unsafe.Pointer(&data)),
"GOOS": strings.ToLower(runtime.GOOS),
}, tps.CONFIG)
}
func (c *configHandler) Post(w http.ResponseWriter, r *http.Request) error {
data, err := io.ReadAll(r.Body)
if err != nil {
return err
}
config := &config.Setting{}
err = protojson.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(data, config)
if err != nil {
return err
}
_, err = c.cf.Save(context.TODO(), config)
if err != nil {
return err
}
w.Write(nil)
return nil
}