1
// Copyright 2022 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
21
"github.com/cubefs/cubefs/blobstore/common/rpc"
22
"github.com/cubefs/cubefs/blobstore/common/trace"
26
trace.RequestIDKey = "rid"
27
trace.PrefixBaggage = "baggage-"
28
trace.FieldKeyTraceID = "traceid"
29
trace.FieldKeySpanID = "spanid"
32
type metaValue struct {
36
func interceptorWholeRequest(c *rpc.Context) {
37
span := trace.SpanFromContextSafe(c.Request.Context())
39
c.Set("var", &metaValue{0})
42
metaVal, _ := c.Get("var")
43
val := metaVal.(*metaValue)
44
span.Infof("Request %s: done Val=%d, spent %s", c.Request.URL.Path, val.val, time.Since(start).String())
47
func interceptorNothing1(c *rpc.Context) {
48
span := trace.SpanFromContextSafe(c.Request.Context())
50
metaVal, _ := c.Get("var")
51
val := metaVal.(*metaValue)
53
span.Infof("Request %s: interceptor nothing-1 Val=%d", c.Request.URL.Path, val.val)
56
func interceptorNothing2(c *rpc.Context) {
57
span := trace.SpanFromContextSafe(c.Request.Context())
59
metaVal, _ := c.Get("var")
60
val := metaVal.(*metaValue)
62
span.Debugf("Request %s: interceptor nothing-2 Val=%d", c.Request.URL.Path, val.val)
65
// NewAppHandler returns app server handler, use rpc.DefaultRouter
66
func NewAppHandler(cfg AppConfig) *rpc.Router {
67
// must be register args with tag
68
rpc.RegisterArgsParser(&ArgsUpload{}, "flag")
69
rpc.RegisterArgsParser(&ArgsDelete{})
70
rpc.RegisterArgsParser(&ArgsDownload{}, "form", "formx")
71
rpc.RegisterArgsParser(&ArgsExist{})
72
rpc.RegisterArgsParser(&ArgsURIOptional{}, "json")
76
rpc.Use(interceptorWholeRequest) // first interceptor
77
rpc.Use(interceptorNothing1, interceptorNothing2) // second, then third
79
// args key/val in c.Param
80
rpc.PUT("/upload/:name/:size/:mode/:desc", app.Upload, rpc.OptArgsURI())
81
// parse args in handler by c.ArgsBody
82
rpc.POST("/update", app.Update)
83
rpc.DELETE("/delete", app.Delete, rpc.OptArgsQuery())
84
rpc.POST("/download", app.Download, rpc.OptArgsForm())
85
rpc.GET("/stream", app.Stream)
86
// param priority is uri > query > form > postfrom by default
87
// but you can define you own rpc.Parser on args struct
88
rpc.HEAD("/exist/:name", app.Exist, rpc.OptArgsURI(), rpc.OptArgsQuery())
89
rpc.GET("/stat/:name", app.Stat, rpc.OptArgsURI(), rpc.OptArgsQuery())
90
// initialized 8 room for key/val map of c.Meta
91
rpc.GET("/list", app.List, rpc.OptMetaCapacity(8))
93
// argument in uri with option
94
rpc.GET("/args/:require/:option", app.OptionalArgs, rpc.OptArgsURI())
95
// argument in uri without option
96
rpc.GET("/args/:require", app.OptionalArgs, rpc.OptArgsURI())
98
return rpc.DefaultRouter
101
// NewFileHandler returns file server handler
102
func NewFileHandler() *rpc.Router {
103
rpc.RegisterArgsParser(&ArgsWrite{}, "flag")
107
router.Handle(http.MethodPut, "/write/:size", filer.Write, rpc.OptArgsURI())
108
router.Handle(http.MethodGet, "/read/:size", filer.Read, rpc.OptArgsURI())
112
// NewMetaHandler returns meta server handler
113
func NewMetaHandler() *rpc.Router {
116
router.Handle(http.MethodPut, "/write/:size", filer.Write, rpc.OptArgsURI())
117
router.Handle(http.MethodGet, "/read/:size", filer.Read, rpc.OptArgsURI())