cubefs

Форк
0
142 строки · 3.1 Кб
1
// Copyright 2022 The CubeFS Authors.
2
//
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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
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.
14

15
package rpc
16

17
import (
18
	"net/http"
19

20
	"github.com/julienschmidt/httprouter"
21
)
22

23
type (
24
	// HandlerFunc defines the handler of app function
25
	HandlerFunc func(*Context)
26

27
	// ServerOption server option applier
28
	// Order: if args in body ignore others options,
29
	//        else uri > query > form > postfrom
30
	ServerOption interface {
31
		apply(*serverOptions)
32
	}
33
	serverOptions struct {
34
		argsBody     bool
35
		argsURI      bool
36
		argsQuery    bool
37
		argsForm     bool
38
		argsPostForm bool
39

40
		metaCapacity int
41
	}
42
	funcServerOption struct {
43
		f func(*serverOptions)
44
	}
45
)
46

47
func (so *serverOptions) copy() *serverOptions {
48
	return &serverOptions{
49
		argsBody:     so.argsBody,
50
		argsURI:      so.argsURI,
51
		argsQuery:    so.argsQuery,
52
		argsForm:     so.argsForm,
53
		argsPostForm: so.argsPostForm,
54

55
		metaCapacity: so.metaCapacity,
56
	}
57
}
58

59
func (so *serverOptions) hasArgs() bool {
60
	return so.argsBody || so.argsURI || so.argsQuery || so.argsForm || so.argsPostForm
61
}
62

63
func (fo *funcServerOption) apply(f *serverOptions) {
64
	fo.f(f)
65
}
66

67
func newFuncServerOption(f func(*serverOptions)) *funcServerOption {
68
	return &funcServerOption{
69
		f: f,
70
	}
71
}
72

73
// OptArgsBody argument in request body
74
func OptArgsBody() ServerOption {
75
	return newFuncServerOption(func(o *serverOptions) {
76
		o.argsBody = true
77
	})
78
}
79

80
// OptArgsURI argument in uri
81
func OptArgsURI() ServerOption {
82
	return newFuncServerOption(func(o *serverOptions) {
83
		o.argsURI = true
84
	})
85
}
86

87
// OptArgsQuery argument in query string
88
func OptArgsQuery() ServerOption {
89
	return newFuncServerOption(func(o *serverOptions) {
90
		o.argsQuery = true
91
	})
92
}
93

94
// OptArgsForm argument in form
95
func OptArgsForm() ServerOption {
96
	return newFuncServerOption(func(o *serverOptions) {
97
		o.argsForm = true
98
	})
99
}
100

101
// OptArgsPostForm argument in post form
102
func OptArgsPostForm() ServerOption {
103
	return newFuncServerOption(func(o *serverOptions) {
104
		o.argsPostForm = true
105
	})
106
}
107

108
// OptMetaCapacity initial meta capacity
109
func OptMetaCapacity(capacity int) ServerOption {
110
	return newFuncServerOption(func(o *serverOptions) {
111
		if capacity >= 0 {
112
			o.metaCapacity = capacity
113
		}
114
	})
115
}
116

117
// makeHandler make handle of httprouter
118
func makeHandler(handlers []HandlerFunc, opts ...ServerOption) httprouter.Handle {
119
	opt := new(serverOptions)
120
	for _, o := range opts {
121
		o.apply(opt)
122
	}
123

124
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
125
		c := &Context{
126
			opts:  opt,
127
			Param: ps,
128

129
			Request: r,
130
			Writer:  w,
131

132
			Meta: make(map[string]interface{}, opt.metaCapacity),
133

134
			index:    -1,
135
			handlers: handlers,
136
		}
137
		c.Next()
138
		if !c.wroteHeader {
139
			c.RespondStatus(http.StatusOK)
140
		}
141
	}
142
}
143

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.