micro/web/options.go

260 lines
5.2 KiB
Go
Raw Normal View History

2019-06-03 19:30:43 +01:00
package web
import (
"context"
"crypto/tls"
"net/http"
"time"
"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/registry"
2019-06-03 19:30:43 +01:00
)
//Options for web
2019-06-03 19:30:43 +01:00
type Options struct {
Name string
Version string
Id string
Metadata map[string]string
Address string
Advertise string
Action func(*cli.Context)
Flags []cli.Flag
RegisterTTL time.Duration
RegisterInterval time.Duration
// RegisterCheck runs a check function before registering the service
RegisterCheck func(context.Context) error
2019-06-03 19:30:43 +01:00
Server *http.Server
Handler http.Handler
// Alternative Options
Context context.Context
Registry registry.Registry
Service micro.Service
Secure bool
TLSConfig *tls.Config
BeforeStart []func() error
BeforeStop []func() error
AfterStart []func() error
AfterStop []func() error
// Static directory
StaticDir string
Signal bool
2019-06-03 19:30:43 +01:00
}
func newOptions(opts ...Option) Options {
opt := Options{
Name: DefaultName,
Version: DefaultVersion,
Id: DefaultId,
Address: DefaultAddress,
RegisterTTL: DefaultRegisterTTL,
RegisterInterval: DefaultRegisterInterval,
StaticDir: DefaultStaticDir,
Service: micro.NewService(),
Context: context.TODO(),
Signal: true,
2019-06-03 19:30:43 +01:00
}
for _, o := range opts {
o(&opt)
}
if opt.RegisterCheck == nil {
opt.RegisterCheck = DefaultRegisterCheck
}
2019-06-03 19:30:43 +01:00
return opt
}
// Name of Web
2019-06-03 19:30:43 +01:00
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}
2019-11-20 12:43:43 +00:00
// Icon specifies an icon url to load in the UI
func Icon(ico string) Option {
return func(o *Options) {
if o.Metadata == nil {
o.Metadata = make(map[string]string)
}
o.Metadata["icon"] = ico
}
}
//Id for Unique server id
2019-06-03 19:30:43 +01:00
func Id(id string) Option {
return func(o *Options) {
o.Id = id
}
}
// Version of the service
func Version(v string) Option {
return func(o *Options) {
o.Version = v
}
}
// Metadata associated with the service
func Metadata(md map[string]string) Option {
return func(o *Options) {
o.Metadata = md
}
}
// Address to bind to - host:port
func Address(a string) Option {
return func(o *Options) {
o.Address = a
}
}
//Advertise The address to advertise for discovery - host:port
2019-06-03 19:30:43 +01:00
func Advertise(a string) Option {
return func(o *Options) {
o.Advertise = a
}
}
// Context specifies a context for the service.
// Can be used to signal shutdown of the service.
// Can be used for extra option values.
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
// Registry used for discovery
2019-06-03 19:30:43 +01:00
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
}
}
//RegisterTTL Register the service with a TTL
2019-06-03 19:30:43 +01:00
func RegisterTTL(t time.Duration) Option {
return func(o *Options) {
o.RegisterTTL = t
}
}
//RegisterInterval Register the service with at interval
2019-06-03 19:30:43 +01:00
func RegisterInterval(t time.Duration) Option {
return func(o *Options) {
o.RegisterInterval = t
}
}
//Handler for custom handler
2019-06-03 19:30:43 +01:00
func Handler(h http.Handler) Option {
return func(o *Options) {
o.Handler = h
}
}
//Server for custom Server
2019-06-03 19:30:43 +01:00
func Server(srv *http.Server) Option {
return func(o *Options) {
o.Server = srv
}
}
// MicroService sets the micro.Service used internally
func MicroService(s micro.Service) Option {
return func(o *Options) {
o.Service = s
}
}
// Flags sets the command flags.
func Flags(flags ...cli.Flag) Option {
return func(o *Options) {
o.Flags = append(o.Flags, flags...)
}
}
// Action sets the command action.
func Action(a func(*cli.Context)) Option {
return func(o *Options) {
o.Action = a
}
}
// BeforeStart is executed before the server starts.
func BeforeStart(fn func() error) Option {
return func(o *Options) {
o.BeforeStart = append(o.BeforeStart, fn)
}
}
// BeforeStop is executed before the server stops.
func BeforeStop(fn func() error) Option {
return func(o *Options) {
o.BeforeStop = append(o.BeforeStop, fn)
}
}
// AfterStart is executed after server start.
func AfterStart(fn func() error) Option {
return func(o *Options) {
o.AfterStart = append(o.AfterStart, fn)
}
}
// AfterStop is executed after server stop.
func AfterStop(fn func() error) Option {
return func(o *Options) {
o.AfterStop = append(o.AfterStop, fn)
}
}
// Secure Use secure communication. If TLSConfig is not specified we use InsecureSkipVerify and generate a self signed cert
func Secure(b bool) Option {
return func(o *Options) {
o.Secure = b
}
}
// TLSConfig to be used for the transport.
func TLSConfig(t *tls.Config) Option {
return func(o *Options) {
o.TLSConfig = t
}
}
// StaticDir sets the static file directory. This defaults to ./html
func StaticDir(d string) Option {
return func(o *Options) {
o.StaticDir = d
}
}
// RegisterCheck run func before registry service
func RegisterCheck(fn func(context.Context) error) Option {
return func(o *Options) {
o.RegisterCheck = fn
}
}
// HandleSignal toggles automatic installation of the signal handler that
// traps TERM, INT, and QUIT. Users of this feature to disable the signal
// handler, should control liveness of the service through the context.
func HandleSignal(b bool) Option {
return func(o *Options) {
o.Signal = b
}
}