Documentation
¶
Overview ¶
Package api provides an http-rpc handler which provides the entire http request over rpc
Copyright 2014 mqant Author. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
Constants ¶
const (
Handler = "api"
)
Variables ¶
var DefaultRoute = func(app module.App, r *http.Request) (*Service, error) { if r.URL.Path == "" { return nil, errors.New("path is nil") } handers := strings.Split(r.URL.Path, "/") if len(handers) < 2 { return nil, errors.New("path is not /[server]/path") } server := handers[1] if server == "" { return nil, errors.New("server is nil") } session, err := app.GetRouteServer(server, selector.WithStrategy(func(services []*registry.Service) selector.Next { var nodes []*registry.Node for _, service := range services { for _, node := range service.Nodes { nodes = append(nodes, node) } } var mtx sync.Mutex return func() (*registry.Node, error) { mtx.Lock() defer mtx.Unlock() if len(nodes) == 0 { return nil, fmt.Errorf("no node") } index := rand.Intn(int(len(nodes))) return nodes[index], nil } }), ) if err != nil { return nil, errors.New(err.Error()) } return &Service{SrvSession: session, Hander: r.URL.Path}, err }
Functions ¶
Types ¶
type ApiHandler ¶ added in v1.3.1
func (*ApiHandler) ServeHTTP ¶ added in v1.3.1
func (a *ApiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
API handler is the default handler which takes api.Request and returns api.Response
type Options ¶
func NewOptions ¶
type Service ¶
type Service struct { // hander Hander string // node SrvSession module.ServerSession }
Service represents an API service