-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
85 lines (70 loc) · 1.97 KB
/
controller.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
77
78
79
80
81
82
83
84
85
package controllers
import (
"encoding/base64"
"io/ioutil"
"github.com/golang/protobuf/proto"
"github.com/gorilla/securecookie"
"github.com/kataras/iris"
"github.com/kataras/iris/sessions"
)
var (
cookieName = "mycustomsessionid"
// AES only supports key sizes of 16, 24 or 32 bytes.
// You either need to provide exactly that amount or you derive the key from what you type in.
hashKey = []byte("the-big-and-secret-fash-key-here")
blockKey = []byte("lot-secret-of-characters-big-too")
secureCookie = securecookie.New(hashKey, blockKey)
sess = sessions.New(sessions.Config{
Cookie: cookieName,
Encode: secureCookie.Encode,
Decode: secureCookie.Decode,
Expires: -1,
})
)
type controller struct {
}
func (c *controller) ReadProto(ctx iris.Context, pb proto.Message) error {
body, err := ioutil.ReadAll(ctx.Request().Body)
if err != nil {
return err
}
data, err := base64.StdEncoding.DecodeString(string(body))
if err != nil {
return err
}
return proto.Unmarshal(data, pb)
}
func (c *controller) WriteProto(ctx iris.Context, pb proto.Message) {
result, err := proto.Marshal(pb)
if err != nil {
ctx.Text(err.Error())
return
}
ctx.Text(base64.StdEncoding.EncodeToString(result))
}
var (
// HomeController 首页
HomeController *homeController
// UserController 用户
UserController *userController
// GoodsController 商品
GoodsController *goodsController
// CartController 购物车
CartController *cartController
// AddressController 收货地址
AddressController *addressController
// OrderController 订单
OrderController *orderController
// RegionController 行政区域
RegionController *regionController
)
// InitControllers 初始化
func InitControllers() {
HomeController = &homeController{}
UserController = &userController{}
GoodsController = &goodsController{}
CartController = &cartController{}
OrderController = &orderController{}
AddressController = &addressController{}
RegionController = ®ionController{}
}