2019-06-03 18:44:43 +01:00
|
|
|
// Package http provides a http server with features; acme, cors, etc
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2020-03-07 11:06:57 +00:00
|
|
|
"github.com/micro/go-micro/v2/api/server/auth"
|
|
|
|
|
2019-06-03 18:44:43 +01:00
|
|
|
"github.com/gorilla/handlers"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/api/server"
|
2020-03-04 11:40:53 +00:00
|
|
|
"github.com/micro/go-micro/v2/api/server/cors"
|
2020-03-11 20:55:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type httpServer struct {
|
|
|
|
mux *http.ServeMux
|
|
|
|
opts server.Options
|
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
address string
|
|
|
|
exit chan chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(address string) server.Server {
|
|
|
|
return &httpServer{
|
|
|
|
opts: server.Options{},
|
|
|
|
mux: http.NewServeMux(),
|
|
|
|
address: address,
|
|
|
|
exit: make(chan chan error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServer) Address() string {
|
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
return s.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServer) Init(opts ...server.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServer) Handle(path string, handler http.Handler) {
|
2020-03-04 11:40:53 +00:00
|
|
|
h := handlers.CombinedLoggingHandler(os.Stdout, handler)
|
2020-03-07 11:06:57 +00:00
|
|
|
h = auth.CombinedAuthHandler(handler)
|
2020-03-04 11:40:53 +00:00
|
|
|
|
|
|
|
if s.opts.EnableCORS {
|
|
|
|
h = cors.CombinedCORSHandler(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mux.Handle(path, h)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServer) Start() error {
|
|
|
|
var l net.Listener
|
|
|
|
var err error
|
|
|
|
|
2019-10-11 16:52:57 +01:00
|
|
|
if s.opts.EnableACME && s.opts.ACMEProvider != nil {
|
2019-06-03 18:44:43 +01:00
|
|
|
// should we check the address to make sure its using :443?
|
2020-02-15 15:10:26 +00:00
|
|
|
l, err = s.opts.ACMEProvider.Listen(s.opts.ACMEHosts...)
|
2019-06-03 18:44:43 +01:00
|
|
|
} else if s.opts.EnableTLS && s.opts.TLSConfig != nil {
|
|
|
|
l, err = tls.Listen("tcp", s.address, s.opts.TLSConfig)
|
|
|
|
} else {
|
|
|
|
// otherwise plain listen
|
|
|
|
l, err = net.Listen("tcp", s.address)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.InfoLevel, logger.DefaultLogger) {
|
|
|
|
logger.Infof("HTTP API Listening on %s", l.Addr().String())
|
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
|
|
|
|
s.mtx.Lock()
|
|
|
|
s.address = l.Addr().String()
|
|
|
|
s.mtx.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := http.Serve(l, s.mux); err != nil {
|
|
|
|
// temporary fix
|
2020-03-11 20:55:39 +03:00
|
|
|
//logger.Fatal(err)
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
ch := <-s.exit
|
|
|
|
ch <- l.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServer) Stop() error {
|
|
|
|
ch := make(chan error)
|
|
|
|
s.exit <- ch
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *httpServer) String() string {
|
|
|
|
return "http"
|
|
|
|
}
|