2019-06-03 18:44:43 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2020-04-07 19:29:26 +01:00
|
|
|
"net/http"
|
2019-10-09 16:42:05 +01:00
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/api/resolver"
|
|
|
|
"github.com/unistack-org/micro/v3/api/server/acme"
|
2020-11-04 00:38:12 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2019-06-03 18:44:43 +01:00
|
|
|
)
|
|
|
|
|
2020-11-02 13:25:29 +03:00
|
|
|
// Option func
|
2019-06-03 18:44:43 +01:00
|
|
|
type Option func(o *Options)
|
|
|
|
|
2020-11-02 13:25:29 +03:00
|
|
|
// Options for api server
|
2019-06-03 18:44:43 +01:00
|
|
|
type Options struct {
|
2020-04-07 19:29:26 +01:00
|
|
|
EnableACME bool
|
|
|
|
EnableCORS bool
|
|
|
|
ACMEProvider acme.Provider
|
|
|
|
EnableTLS bool
|
|
|
|
ACMEHosts []string
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
Resolver resolver.Resolver
|
|
|
|
Wrappers []Wrapper
|
2020-11-04 00:38:12 +03:00
|
|
|
Logger logger.Logger
|
2020-04-07 19:29:26 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// NewOptions returns new Options
|
2020-10-16 09:38:57 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
2020-11-04 00:38:12 +03:00
|
|
|
options := Options{
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
}
|
2020-10-16 09:38:57 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:29:26 +01:00
|
|
|
type Wrapper func(h http.Handler) http.Handler
|
|
|
|
|
2020-08-10 22:38:54 +01:00
|
|
|
func WrapHandler(w ...Wrapper) Option {
|
2020-04-07 19:29:26 +01:00
|
|
|
return func(o *Options) {
|
2020-08-10 22:38:54 +01:00
|
|
|
o.Wrappers = append(o.Wrappers, w...)
|
2020-04-07 19:29:26 +01:00
|
|
|
}
|
2019-10-09 16:42:05 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:40:53 +00:00
|
|
|
func EnableCORS(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.EnableCORS = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:42:05 +01:00
|
|
|
func EnableACME(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.EnableACME = b
|
|
|
|
}
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ACMEHosts(hosts ...string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.ACMEHosts = hosts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:42:05 +01:00
|
|
|
func ACMEProvider(p acme.Provider) Option {
|
2019-06-03 18:44:43 +01:00
|
|
|
return func(o *Options) {
|
2019-10-09 16:42:05 +01:00
|
|
|
o.ACMEProvider = p
|
2019-06-03 18:44:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func EnableTLS(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.EnableTLS = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TLSConfig(t *tls.Config) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TLSConfig = t
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 17:44:48 +01:00
|
|
|
|
2020-04-07 16:24:51 +01:00
|
|
|
func Resolver(r resolver.Resolver) Option {
|
2020-04-02 17:44:48 +01:00
|
|
|
return func(o *Options) {
|
2020-04-07 16:24:51 +01:00
|
|
|
o.Resolver = r
|
2020-04-02 17:44:48 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-04 00:38:12 +03:00
|
|
|
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|