2016-06-30 22:21:57 +03:00
|
|
|
// Package http implements a go-micro.Server
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-03-01 20:23:22 +03:00
|
|
|
"context"
|
2020-12-15 11:54:15 +03:00
|
|
|
"crypto/tls"
|
2018-12-19 10:33:23 +03:00
|
|
|
"fmt"
|
2016-06-30 22:21:57 +03:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-02-05 18:31:51 +03:00
|
|
|
"reflect"
|
2021-04-13 11:32:56 +03:00
|
|
|
"regexp"
|
2018-12-19 10:33:23 +03:00
|
|
|
"sort"
|
2021-02-05 18:31:51 +03:00
|
|
|
"strings"
|
2016-06-30 22:21:57 +03:00
|
|
|
"sync"
|
2019-02-01 12:11:25 +03:00
|
|
|
"time"
|
2016-06-30 22:21:57 +03:00
|
|
|
|
2020-10-10 00:38:35 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-01-29 14:32:32 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-10-10 00:38:35 +03:00
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2021-02-05 18:31:51 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/router"
|
2020-12-15 11:54:15 +03:00
|
|
|
"golang.org/x/net/netutil"
|
2016-06-30 22:21:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type httpServer struct {
|
2020-10-10 00:38:35 +03:00
|
|
|
sync.RWMutex
|
2020-11-05 01:55:38 +03:00
|
|
|
opts server.Options
|
2021-03-09 14:19:59 +03:00
|
|
|
handlers map[string]server.Handler
|
2020-11-05 01:55:38 +03:00
|
|
|
hd server.Handler
|
|
|
|
exit chan chan error
|
|
|
|
subscribers map[*httpSubscriber][]broker.Subscriber
|
2018-12-19 13:22:27 +03:00
|
|
|
// used for first registration
|
|
|
|
registered bool
|
2021-01-29 14:32:32 +03:00
|
|
|
// register service instance
|
2021-04-13 23:42:33 +03:00
|
|
|
rsvc *register.Service
|
|
|
|
init bool
|
|
|
|
errorHandler func(context.Context, server.Handler, http.ResponseWriter, *http.Request, error, int)
|
|
|
|
pathHandlers map[*regexp.Regexp]http.HandlerFunc
|
|
|
|
contentTypeHandlers map[string]http.HandlerFunc
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
2020-11-25 23:44:32 +03:00
|
|
|
func (h *httpServer) newCodec(ct string) (codec.Codec, error) {
|
2021-04-16 17:14:27 +03:00
|
|
|
h.RLock()
|
|
|
|
defer h.RUnlock()
|
|
|
|
|
|
|
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
|
|
|
ct = ct[:idx]
|
|
|
|
}
|
|
|
|
|
2020-11-25 23:44:32 +03:00
|
|
|
if cf, ok := h.opts.Codecs[ct]; ok {
|
2018-12-19 10:33:23 +03:00
|
|
|
return cf, nil
|
|
|
|
}
|
2020-11-25 23:44:32 +03:00
|
|
|
return nil, codec.ErrUnknownContentType
|
2018-12-19 10:33:23 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
func (h *httpServer) Options() server.Options {
|
|
|
|
h.Lock()
|
|
|
|
opts := h.opts
|
|
|
|
h.Unlock()
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) Init(opts ...server.Option) error {
|
2021-03-24 15:22:20 +03:00
|
|
|
if len(opts) == 0 && h.init {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
h.Lock()
|
2021-04-13 11:32:56 +03:00
|
|
|
defer h.Unlock()
|
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&h.opts)
|
|
|
|
}
|
2021-03-09 14:19:59 +03:00
|
|
|
if fn, ok := h.opts.Context.Value(errorHandlerKey{}).(func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int)); ok && fn != nil {
|
|
|
|
h.errorHandler = fn
|
|
|
|
}
|
2021-03-24 15:22:20 +03:00
|
|
|
if h.handlers == nil {
|
|
|
|
h.handlers = make(map[string]server.Handler)
|
|
|
|
}
|
2021-04-13 11:32:56 +03:00
|
|
|
if h.pathHandlers == nil {
|
|
|
|
h.pathHandlers = make(map[*regexp.Regexp]http.HandlerFunc)
|
|
|
|
}
|
2021-04-13 23:42:33 +03:00
|
|
|
if h.contentTypeHandlers == nil {
|
|
|
|
h.contentTypeHandlers = make(map[string]http.HandlerFunc)
|
|
|
|
}
|
2021-04-13 11:32:56 +03:00
|
|
|
if phs, ok := h.opts.Context.Value(pathHandlerKey{}).(*pathHandlerVal); ok && phs.h != nil {
|
|
|
|
for pp, ph := range phs.h {
|
|
|
|
exp, err := regexp.Compile(pp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.pathHandlers[exp] = ph
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 23:42:33 +03:00
|
|
|
if phs, ok := h.opts.Context.Value(contentTypeHandlerKey{}).(*contentTypeHandlerVal); ok && phs.h != nil {
|
|
|
|
for pp, ph := range phs.h {
|
|
|
|
h.contentTypeHandlers[pp] = ph
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:22:20 +03:00
|
|
|
if err := h.opts.Register.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Broker.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Tracer.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Auth.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Logger.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Meter.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.opts.Transport.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.init = true
|
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) Handle(handler server.Handler) error {
|
|
|
|
h.Lock()
|
2021-03-09 14:19:59 +03:00
|
|
|
if hdlr, ok := handler.(*httpHandler); ok {
|
|
|
|
if _, ok := hdlr.hd.(http.Handler); ok {
|
|
|
|
h.hd = handler
|
|
|
|
} else {
|
2021-03-24 15:22:20 +03:00
|
|
|
if h.handlers == nil {
|
|
|
|
h.handlers = make(map[string]server.Handler)
|
|
|
|
}
|
2021-03-09 14:19:59 +03:00
|
|
|
h.handlers[handler.Name()] = handler
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
h.hd = handler
|
|
|
|
}
|
2016-06-30 22:21:57 +03:00
|
|
|
h.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
|
2020-11-18 13:08:50 +03:00
|
|
|
options := server.NewHandlerOptions(opts...)
|
2016-06-30 22:21:57 +03:00
|
|
|
|
2021-04-20 12:53:29 +03:00
|
|
|
eps := make([]*register.Endpoint, 0, len(options.Metadata))
|
|
|
|
for name, metadata := range options.Metadata {
|
|
|
|
eps = append(eps, ®ister.Endpoint{
|
|
|
|
Name: name,
|
|
|
|
Metadata: metadata,
|
|
|
|
})
|
2017-04-03 17:03:46 +03:00
|
|
|
}
|
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
hdlr := &httpHandler{
|
|
|
|
eps: eps,
|
|
|
|
hd: handler,
|
|
|
|
opts: options,
|
|
|
|
sopts: h.opts,
|
|
|
|
}
|
|
|
|
|
|
|
|
tp := reflect.TypeOf(handler)
|
|
|
|
|
|
|
|
/*
|
|
|
|
for m := 0; m < tp.NumMethod(); m++ {
|
|
|
|
if e := register.ExtractEndpoint(tp.Method(m)); e != nil {
|
|
|
|
e.Name = name + "." + e.Name
|
|
|
|
|
|
|
|
for k, v := range options.Metadata[e.Name] {
|
|
|
|
e.Metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
eps = append(eps, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
hdlr.handlers = make(map[string][]patHandler)
|
|
|
|
for hn, md := range options.Metadata {
|
|
|
|
cmp, err := rutil.Parse(md["Path"])
|
|
|
|
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
h.opts.Logger.Errorf(h.opts.Context, "parsing path pattern err: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tpl := cmp.Compile()
|
|
|
|
pat, err := rutil.NewPattern(tpl.Version, tpl.OpCodes, tpl.Pool, tpl.Verb)
|
|
|
|
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
h.opts.Logger.Errorf(h.opts.Context, "creating new pattern err: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var method reflect.Method
|
|
|
|
mname := hn[strings.Index(hn, ".")+1:]
|
|
|
|
for m := 0; m < tp.NumMethod(); m++ {
|
|
|
|
mn := tp.Method(m)
|
|
|
|
if mn.Name != mname {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
method = mn
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if method.Name == "" && h.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
h.opts.Logger.Errorf(h.opts.Context, "nil method for %s", mname)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
mtype, err := prepareEndpoint(method)
|
|
|
|
if err != nil && h.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
h.opts.Logger.Errorf(h.opts.Context, "%v", err)
|
|
|
|
continue
|
|
|
|
} else if mtype == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rcvr := reflect.ValueOf(handler)
|
|
|
|
name := reflect.Indirect(rcvr).Type().Name()
|
|
|
|
|
|
|
|
pth := patHandler{pat: pat, mtype: mtype, name: name, rcvr: rcvr}
|
|
|
|
hdlr.name = name
|
|
|
|
hdlr.handlers[md["Method"]] = append(hdlr.handlers[md["Method"]], pth)
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
|
|
|
|
return hdlr
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) NewSubscriber(topic string, handler interface{}, opts ...server.SubscriberOption) server.Subscriber {
|
2018-12-19 10:33:23 +03:00
|
|
|
return newSubscriber(topic, handler, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) Subscribe(sb server.Subscriber) error {
|
2018-12-19 12:47:03 +03:00
|
|
|
sub, ok := sb.(*httpSubscriber)
|
2018-12-19 10:33:23 +03:00
|
|
|
if !ok {
|
2018-12-19 12:47:03 +03:00
|
|
|
return fmt.Errorf("invalid subscriber: expected *httpSubscriber")
|
2018-12-19 10:33:23 +03:00
|
|
|
}
|
|
|
|
if len(sub.handlers) == 0 {
|
|
|
|
return fmt.Errorf("invalid subscriber: no handler functions")
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
2020-10-10 00:38:35 +03:00
|
|
|
if err := server.ValidateSubscriber(sb); err != nil {
|
2018-12-19 10:33:23 +03:00
|
|
|
return err
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
2018-12-19 10:33:23 +03:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
_, ok = h.subscribers[sub]
|
|
|
|
if ok {
|
|
|
|
return fmt.Errorf("subscriber %v already exists", h)
|
|
|
|
}
|
|
|
|
h.subscribers[sub] = nil
|
|
|
|
return nil
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) Register() error {
|
2021-03-09 14:19:59 +03:00
|
|
|
var eps []*register.Endpoint
|
2020-11-05 01:55:38 +03:00
|
|
|
h.RLock()
|
2021-03-09 14:19:59 +03:00
|
|
|
for _, hdlr := range h.handlers {
|
|
|
|
eps = append(eps, hdlr.Endpoints()...)
|
|
|
|
}
|
2020-11-05 01:55:38 +03:00
|
|
|
rsvc := h.rsvc
|
|
|
|
config := h.opts
|
|
|
|
h.RUnlock()
|
|
|
|
|
|
|
|
// if service already filled, reuse it and return early
|
|
|
|
if rsvc != nil {
|
|
|
|
if err := server.DefaultRegisterFunc(rsvc, config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-30 22:21:57 +03:00
|
|
|
|
2021-01-29 14:32:32 +03:00
|
|
|
service, err := server.NewRegisterService(h)
|
2020-10-10 00:38:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-16 16:20:19 +03:00
|
|
|
service.Nodes[0].Metadata["protocol"] = "http"
|
2017-04-03 17:03:46 +03:00
|
|
|
service.Endpoints = eps
|
2016-06-30 22:21:57 +03:00
|
|
|
|
2018-12-19 10:33:23 +03:00
|
|
|
h.Lock()
|
2018-12-19 12:47:03 +03:00
|
|
|
var subscriberList []*httpSubscriber
|
2018-12-19 10:33:23 +03:00
|
|
|
for e := range h.subscribers {
|
|
|
|
// Only advertise non internal subscribers
|
2021-04-20 12:53:29 +03:00
|
|
|
subscriberList = append(subscriberList, e)
|
2018-12-19 10:33:23 +03:00
|
|
|
}
|
|
|
|
sort.Slice(subscriberList, func(i, j int) bool {
|
|
|
|
return subscriberList[i].topic > subscriberList[j].topic
|
|
|
|
})
|
2020-11-05 01:55:38 +03:00
|
|
|
|
2018-12-19 10:33:23 +03:00
|
|
|
for _, e := range subscriberList {
|
|
|
|
service.Endpoints = append(service.Endpoints, e.Endpoints()...)
|
|
|
|
}
|
|
|
|
h.Unlock()
|
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
h.RLock()
|
|
|
|
registered := h.registered
|
|
|
|
h.RUnlock()
|
2016-06-30 22:21:57 +03:00
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
if !registered {
|
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2021-01-29 14:32:32 +03:00
|
|
|
config.Logger.Infof(config.Context, "Register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].Id)
|
2020-11-05 01:55:38 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-09 13:31:22 +03:00
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
// register the service
|
|
|
|
if err := server.DefaultRegisterFunc(service, config); err != nil {
|
2018-12-19 13:22:27 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
// already registered? don't need to register subscribers
|
|
|
|
if registered {
|
2018-12-19 13:22:27 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
2020-10-16 16:20:19 +03:00
|
|
|
|
2020-10-10 00:38:35 +03:00
|
|
|
for sb := range h.subscribers {
|
2020-11-05 01:55:38 +03:00
|
|
|
handler := h.createSubHandler(sb, config)
|
|
|
|
var opts []broker.SubscribeOption
|
2018-12-19 13:22:27 +03:00
|
|
|
if queue := sb.Options().Queue; len(queue) > 0 {
|
2020-11-05 01:55:38 +03:00
|
|
|
opts = append(opts, broker.SubscribeGroup(queue))
|
2018-12-19 13:22:27 +03:00
|
|
|
}
|
2020-10-10 00:38:35 +03:00
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
subCtx := config.Context
|
2020-10-16 16:20:19 +03:00
|
|
|
if cx := sb.Options().Context; cx != nil {
|
|
|
|
subCtx = cx
|
|
|
|
}
|
2020-11-05 01:55:38 +03:00
|
|
|
opts = append(opts, broker.SubscribeContext(subCtx))
|
|
|
|
opts = append(opts, broker.SubscribeAutoAck(sb.Options().AutoAck))
|
2020-10-16 16:20:19 +03:00
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
sub, err := config.Broker.Subscribe(subCtx, sb.Topic(), handler, opts...)
|
2018-12-19 13:22:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
h.subscribers[sb] = []broker.Subscriber{sub}
|
|
|
|
}
|
2020-11-05 01:55:38 +03:00
|
|
|
|
|
|
|
h.registered = true
|
|
|
|
h.rsvc = service
|
|
|
|
|
2018-12-19 13:22:27 +03:00
|
|
|
return nil
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) Deregister() error {
|
2020-11-05 01:55:38 +03:00
|
|
|
h.RLock()
|
|
|
|
config := h.opts
|
|
|
|
h.RUnlock()
|
2020-10-10 00:38:35 +03:00
|
|
|
|
2021-01-29 14:32:32 +03:00
|
|
|
service, err := server.NewRegisterService(h)
|
2020-10-10 00:38:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-09 13:31:22 +03:00
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Infof(config.Context, "Deregistering node: %s", service.Nodes[0].Id)
|
2020-11-05 01:55:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.DefaultDeregisterFunc(service, config); err != nil {
|
2018-12-19 12:56:52 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
2020-11-05 01:55:38 +03:00
|
|
|
h.rsvc = nil
|
|
|
|
|
2018-12-19 13:22:27 +03:00
|
|
|
if !h.registered {
|
|
|
|
h.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-05 01:55:38 +03:00
|
|
|
|
2018-12-19 13:22:27 +03:00
|
|
|
h.registered = false
|
|
|
|
|
2020-10-16 16:20:19 +03:00
|
|
|
subCtx := h.opts.Context
|
2018-12-19 12:56:52 +03:00
|
|
|
for sb, subs := range h.subscribers {
|
2020-10-16 16:46:39 +03:00
|
|
|
if cx := sb.Options().Context; cx != nil {
|
|
|
|
subCtx = cx
|
|
|
|
}
|
|
|
|
|
2018-12-19 12:56:52 +03:00
|
|
|
for _, sub := range subs {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Infof(config.Context, "Unsubscribing from topic: %s", sub.Topic())
|
2020-10-16 16:20:19 +03:00
|
|
|
if err := sub.Unsubscribe(subCtx); err != nil {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "failed to unsubscribe topic: %s, error: %v", sb.Topic(), err)
|
2020-10-16 16:20:19 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-12-19 12:56:52 +03:00
|
|
|
}
|
|
|
|
h.subscribers[sb] = nil
|
|
|
|
}
|
|
|
|
h.Unlock()
|
|
|
|
return nil
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) Start() error {
|
2020-11-05 01:55:38 +03:00
|
|
|
h.RLock()
|
|
|
|
config := h.opts
|
|
|
|
h.RUnlock()
|
2016-06-30 22:21:57 +03:00
|
|
|
|
2020-12-15 11:54:15 +03:00
|
|
|
// micro: config.Transport.Listen(config.Address)
|
|
|
|
var ts net.Listener
|
|
|
|
|
|
|
|
if l := config.Listener; l != nil {
|
|
|
|
ts = l
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// check the tls config for secure connect
|
|
|
|
if tc := config.TLSConfig; tc != nil {
|
|
|
|
ts, err = tls.Listen("tcp", config.Address, tc)
|
|
|
|
// otherwise just plain tcp listener
|
|
|
|
} else {
|
|
|
|
ts, err = net.Listen("tcp", config.Address)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.MaxConn > 0 {
|
|
|
|
ts = netutil.LimitListener(ts, config.MaxConn)
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
2020-11-05 01:55:38 +03:00
|
|
|
if config.Logger.V(logger.InfoLevel) {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Infof(config.Context, "Listening on %s", ts.Addr().String())
|
2020-11-05 01:55:38 +03:00
|
|
|
}
|
2018-08-09 13:31:22 +03:00
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
h.Lock()
|
2020-12-15 11:54:15 +03:00
|
|
|
h.opts.Address = ts.Addr().String()
|
2016-06-30 22:21:57 +03:00
|
|
|
h.Unlock()
|
|
|
|
|
2021-03-09 14:19:59 +03:00
|
|
|
var handler http.Handler
|
2021-03-09 23:54:46 +03:00
|
|
|
var srvFunc func(net.Listener) error
|
|
|
|
|
|
|
|
if h.opts.Context != nil {
|
|
|
|
if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil {
|
|
|
|
if hs.Handler == nil && h.hd != nil {
|
|
|
|
if hdlr, ok := h.hd.Handler().(http.Handler); ok {
|
|
|
|
hs.Handler = hdlr
|
|
|
|
handler = hs.Handler
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
handler = hs.Handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if handler == nil && h.hd == nil {
|
2021-03-09 14:19:59 +03:00
|
|
|
handler = h
|
2021-03-09 23:54:46 +03:00
|
|
|
} else if handler == nil && h.hd != nil {
|
|
|
|
if hdlr, ok := h.hd.Handler().(http.Handler); ok {
|
|
|
|
handler = hdlr
|
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
2021-03-09 23:54:46 +03:00
|
|
|
if handler == nil {
|
|
|
|
return fmt.Errorf("cant process with nil handler")
|
|
|
|
}
|
2016-06-30 22:21:57 +03:00
|
|
|
|
2020-12-15 11:54:15 +03:00
|
|
|
if err := config.Broker.Connect(h.opts.Context); err != nil {
|
2019-02-27 07:28:03 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-15 11:54:15 +03:00
|
|
|
if err := config.RegisterCheck(h.opts.Context); err != nil {
|
2020-11-05 01:55:38 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.Id, err)
|
2020-10-10 00:38:35 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = h.Register(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-27 07:28:03 +03:00
|
|
|
}
|
2019-01-24 16:58:56 +03:00
|
|
|
|
2021-02-07 13:27:28 +03:00
|
|
|
fn := handler
|
2021-02-07 19:03:38 +03:00
|
|
|
|
2021-02-07 13:27:28 +03:00
|
|
|
if h.opts.Context != nil {
|
|
|
|
if mwf, ok := h.opts.Context.Value(middlewareKey{}).([]func(http.Handler) http.Handler); ok && len(mwf) > 0 {
|
|
|
|
// wrap the handler func
|
|
|
|
for i := len(mwf); i > 0; i-- {
|
|
|
|
fn = mwf[i-1](fn)
|
|
|
|
}
|
|
|
|
}
|
2021-02-07 19:03:38 +03:00
|
|
|
if hs, ok := h.opts.Context.Value(serverKey{}).(*http.Server); ok && hs != nil {
|
|
|
|
hs.Handler = fn
|
|
|
|
srvFunc = hs.Serve
|
|
|
|
}
|
2021-02-07 13:27:28 +03:00
|
|
|
}
|
|
|
|
|
2021-02-07 19:03:38 +03:00
|
|
|
if srvFunc != nil {
|
|
|
|
go srvFunc(ts)
|
|
|
|
} else {
|
|
|
|
go http.Serve(ts, fn)
|
|
|
|
}
|
2016-06-30 22:21:57 +03:00
|
|
|
|
|
|
|
go func() {
|
2019-02-01 12:11:25 +03:00
|
|
|
t := new(time.Ticker)
|
|
|
|
|
|
|
|
// only process if it exists
|
2020-11-05 01:55:38 +03:00
|
|
|
if config.RegisterInterval > time.Duration(0) {
|
2019-02-01 12:11:25 +03:00
|
|
|
// new ticker
|
2020-11-05 01:55:38 +03:00
|
|
|
t = time.NewTicker(config.RegisterInterval)
|
2019-02-01 12:11:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return error chan
|
|
|
|
var ch chan error
|
|
|
|
|
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// register self on interval
|
|
|
|
case <-t.C:
|
2020-10-10 00:38:35 +03:00
|
|
|
h.RLock()
|
|
|
|
registered := h.registered
|
|
|
|
h.RUnlock()
|
2020-11-05 01:55:38 +03:00
|
|
|
rerr := config.RegisterCheck(h.opts.Context)
|
2020-10-10 00:38:35 +03:00
|
|
|
if rerr != nil && registered {
|
2020-11-05 01:55:38 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s, deregister it", config.Name, config.Id, rerr)
|
2020-10-10 00:38:35 +03:00
|
|
|
}
|
|
|
|
// deregister self in case of error
|
|
|
|
if err := h.Deregister(); err != nil {
|
2020-11-05 01:55:38 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s deregister error: %s", config.Name, config.Id, err)
|
2020-10-10 00:38:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if rerr != nil && !registered {
|
2020-11-05 01:55:38 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s register check error: %s", config.Name, config.Id, rerr)
|
2020-10-10 00:38:35 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := h.Register(); err != nil {
|
2020-11-05 01:55:38 +03:00
|
|
|
if config.Logger.V(logger.ErrorLevel) {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server %s-%s register error: %s", config.Name, config.Id, err)
|
2020-10-10 00:38:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 12:11:25 +03:00
|
|
|
if err := h.Register(); err != nil {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server register error: %s", err)
|
2019-02-01 12:11:25 +03:00
|
|
|
}
|
|
|
|
// wait for exit
|
|
|
|
case ch = <-h.exit:
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 11:54:15 +03:00
|
|
|
ch <- ts.Close()
|
2018-12-20 06:31:36 +03:00
|
|
|
|
2019-01-24 16:58:56 +03:00
|
|
|
// deregister
|
2020-12-15 11:54:15 +03:00
|
|
|
if err := h.Deregister(); err != nil {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Server deregister error: %s", err)
|
2020-12-15 11:54:15 +03:00
|
|
|
}
|
2019-01-24 16:58:56 +03:00
|
|
|
|
2020-12-15 11:54:15 +03:00
|
|
|
if err := config.Broker.Disconnect(config.Context); err != nil {
|
2021-01-16 00:28:10 +03:00
|
|
|
config.Logger.Errorf(config.Context, "Broker disconnect error: %s", err)
|
2020-12-15 11:54:15 +03:00
|
|
|
}
|
2016-06-30 22:21:57 +03:00
|
|
|
}()
|
|
|
|
|
2019-02-27 07:28:03 +03:00
|
|
|
return nil
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) Stop() error {
|
|
|
|
ch := make(chan error)
|
|
|
|
h.exit <- ch
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpServer) String() string {
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:32:32 +03:00
|
|
|
func (h *httpServer) Name() string {
|
|
|
|
return h.opts.Name
|
|
|
|
}
|
|
|
|
|
2020-10-10 00:38:35 +03:00
|
|
|
func NewServer(opts ...server.Option) server.Server {
|
|
|
|
options := server.NewOptions(opts...)
|
2016-06-30 22:21:57 +03:00
|
|
|
return &httpServer{
|
2021-03-31 00:49:55 +03:00
|
|
|
opts: options,
|
|
|
|
exit: make(chan chan error),
|
|
|
|
subscribers: make(map[*httpSubscriber][]broker.Subscriber),
|
|
|
|
errorHandler: DefaultErrorHandler,
|
2021-04-13 11:32:56 +03:00
|
|
|
pathHandlers: make(map[*regexp.Regexp]http.HandlerFunc),
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
}
|