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.
20
"github.com/julienschmidt/httprouter"
24
// HandlerFunc defines the handler of app function
25
HandlerFunc func(*Context)
27
// ServerOption server option applier
28
// Order: if args in body ignore others options,
29
// else uri > query > form > postfrom
30
ServerOption interface {
33
serverOptions struct {
42
funcServerOption struct {
43
f func(*serverOptions)
47
func (so *serverOptions) copy() *serverOptions {
48
return &serverOptions{
49
argsBody: so.argsBody,
51
argsQuery: so.argsQuery,
52
argsForm: so.argsForm,
53
argsPostForm: so.argsPostForm,
55
metaCapacity: so.metaCapacity,
59
func (so *serverOptions) hasArgs() bool {
60
return so.argsBody || so.argsURI || so.argsQuery || so.argsForm || so.argsPostForm
63
func (fo *funcServerOption) apply(f *serverOptions) {
67
func newFuncServerOption(f func(*serverOptions)) *funcServerOption {
68
return &funcServerOption{
73
// OptArgsBody argument in request body
74
func OptArgsBody() ServerOption {
75
return newFuncServerOption(func(o *serverOptions) {
80
// OptArgsURI argument in uri
81
func OptArgsURI() ServerOption {
82
return newFuncServerOption(func(o *serverOptions) {
87
// OptArgsQuery argument in query string
88
func OptArgsQuery() ServerOption {
89
return newFuncServerOption(func(o *serverOptions) {
94
// OptArgsForm argument in form
95
func OptArgsForm() ServerOption {
96
return newFuncServerOption(func(o *serverOptions) {
101
// OptArgsPostForm argument in post form
102
func OptArgsPostForm() ServerOption {
103
return newFuncServerOption(func(o *serverOptions) {
104
o.argsPostForm = true
108
// OptMetaCapacity initial meta capacity
109
func OptMetaCapacity(capacity int) ServerOption {
110
return newFuncServerOption(func(o *serverOptions) {
112
o.metaCapacity = capacity
117
// makeHandler make handle of httprouter
118
func makeHandler(handlers []HandlerFunc, opts ...ServerOption) httprouter.Handle {
119
opt := new(serverOptions)
120
for _, o := range opts {
124
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
132
Meta: make(map[string]interface{}, opt.metaCapacity),
139
c.RespondStatus(http.StatusOK)