github.com/micro/go-micro/v2@v2.9.1/web/options.go (about)

     1  package web
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"net/http"
     7  	"time"
     8  
     9  	"github.com/micro/cli/v2"
    10  	"github.com/micro/go-micro/v2"
    11  	"github.com/micro/go-micro/v2/registry"
    12  )
    13  
    14  //Options for web
    15  type Options struct {
    16  	Name      string
    17  	Version   string
    18  	Id        string
    19  	Metadata  map[string]string
    20  	Address   string
    21  	Advertise string
    22  
    23  	Action func(*cli.Context)
    24  	Flags  []cli.Flag
    25  
    26  	RegisterTTL      time.Duration
    27  	RegisterInterval time.Duration
    28  
    29  	// RegisterCheck runs a check function before registering the service
    30  	RegisterCheck func(context.Context) error
    31  
    32  	Server  *http.Server
    33  	Handler http.Handler
    34  
    35  	// Alternative Options
    36  	Context context.Context
    37  
    38  	Registry registry.Registry
    39  	Service  micro.Service
    40  
    41  	Secure      bool
    42  	TLSConfig   *tls.Config
    43  	BeforeStart []func() error
    44  	BeforeStop  []func() error
    45  	AfterStart  []func() error
    46  	AfterStop   []func() error
    47  
    48  	// Static directory
    49  	StaticDir string
    50  
    51  	Signal bool
    52  }
    53  
    54  func newOptions(opts ...Option) Options {
    55  	opt := Options{
    56  		Name:             DefaultName,
    57  		Version:          DefaultVersion,
    58  		Id:               DefaultId,
    59  		Address:          DefaultAddress,
    60  		RegisterTTL:      DefaultRegisterTTL,
    61  		RegisterInterval: DefaultRegisterInterval,
    62  		StaticDir:        DefaultStaticDir,
    63  		Service:          micro.NewService(),
    64  		Context:          context.TODO(),
    65  		Signal:           true,
    66  	}
    67  
    68  	for _, o := range opts {
    69  		o(&opt)
    70  	}
    71  
    72  	if opt.RegisterCheck == nil {
    73  		opt.RegisterCheck = DefaultRegisterCheck
    74  	}
    75  
    76  	return opt
    77  }
    78  
    79  // Name of Web
    80  func Name(n string) Option {
    81  	return func(o *Options) {
    82  		o.Name = n
    83  	}
    84  }
    85  
    86  // Icon specifies an icon url to load in the UI
    87  func Icon(ico string) Option {
    88  	return func(o *Options) {
    89  		if o.Metadata == nil {
    90  			o.Metadata = make(map[string]string)
    91  		}
    92  		o.Metadata["icon"] = ico
    93  	}
    94  }
    95  
    96  //Id for Unique server id
    97  func Id(id string) Option {
    98  	return func(o *Options) {
    99  		o.Id = id
   100  	}
   101  }
   102  
   103  // Version of the service
   104  func Version(v string) Option {
   105  	return func(o *Options) {
   106  		o.Version = v
   107  	}
   108  }
   109  
   110  // Metadata associated with the service
   111  func Metadata(md map[string]string) Option {
   112  	return func(o *Options) {
   113  		o.Metadata = md
   114  	}
   115  }
   116  
   117  // Address to bind to - host:port
   118  func Address(a string) Option {
   119  	return func(o *Options) {
   120  		o.Address = a
   121  	}
   122  }
   123  
   124  //Advertise The address to advertise for discovery - host:port
   125  func Advertise(a string) Option {
   126  	return func(o *Options) {
   127  		o.Advertise = a
   128  	}
   129  }
   130  
   131  // Context specifies a context for the service.
   132  // Can be used to signal shutdown of the service.
   133  // Can be used for extra option values.
   134  func Context(ctx context.Context) Option {
   135  	return func(o *Options) {
   136  		o.Context = ctx
   137  	}
   138  }
   139  
   140  // Registry used for discovery
   141  func Registry(r registry.Registry) Option {
   142  	return func(o *Options) {
   143  		o.Registry = r
   144  	}
   145  }
   146  
   147  //RegisterTTL Register the service with a TTL
   148  func RegisterTTL(t time.Duration) Option {
   149  	return func(o *Options) {
   150  		o.RegisterTTL = t
   151  	}
   152  }
   153  
   154  //RegisterInterval Register the service with at interval
   155  func RegisterInterval(t time.Duration) Option {
   156  	return func(o *Options) {
   157  		o.RegisterInterval = t
   158  	}
   159  }
   160  
   161  //Handler for custom handler
   162  func Handler(h http.Handler) Option {
   163  	return func(o *Options) {
   164  		o.Handler = h
   165  	}
   166  }
   167  
   168  //Server for custom Server
   169  func Server(srv *http.Server) Option {
   170  	return func(o *Options) {
   171  		o.Server = srv
   172  	}
   173  }
   174  
   175  // MicroService sets the micro.Service used internally
   176  func MicroService(s micro.Service) Option {
   177  	return func(o *Options) {
   178  		o.Service = s
   179  	}
   180  }
   181  
   182  // Flags sets the command flags.
   183  func Flags(flags ...cli.Flag) Option {
   184  	return func(o *Options) {
   185  		o.Flags = append(o.Flags, flags...)
   186  	}
   187  }
   188  
   189  // Action sets the command action.
   190  func Action(a func(*cli.Context)) Option {
   191  	return func(o *Options) {
   192  		o.Action = a
   193  	}
   194  }
   195  
   196  // BeforeStart is executed before the server starts.
   197  func BeforeStart(fn func() error) Option {
   198  	return func(o *Options) {
   199  		o.BeforeStart = append(o.BeforeStart, fn)
   200  	}
   201  }
   202  
   203  // BeforeStop is executed before the server stops.
   204  func BeforeStop(fn func() error) Option {
   205  	return func(o *Options) {
   206  		o.BeforeStop = append(o.BeforeStop, fn)
   207  	}
   208  }
   209  
   210  // AfterStart is executed after server start.
   211  func AfterStart(fn func() error) Option {
   212  	return func(o *Options) {
   213  		o.AfterStart = append(o.AfterStart, fn)
   214  	}
   215  }
   216  
   217  // AfterStop is executed after server stop.
   218  func AfterStop(fn func() error) Option {
   219  	return func(o *Options) {
   220  		o.AfterStop = append(o.AfterStop, fn)
   221  	}
   222  }
   223  
   224  // Secure Use secure communication. If TLSConfig is not specified we use InsecureSkipVerify and generate a self signed cert
   225  func Secure(b bool) Option {
   226  	return func(o *Options) {
   227  		o.Secure = b
   228  	}
   229  }
   230  
   231  // TLSConfig to be used for the transport.
   232  func TLSConfig(t *tls.Config) Option {
   233  	return func(o *Options) {
   234  		o.TLSConfig = t
   235  	}
   236  }
   237  
   238  // StaticDir sets the static file directory. This defaults to ./html
   239  func StaticDir(d string) Option {
   240  	return func(o *Options) {
   241  		o.StaticDir = d
   242  	}
   243  }
   244  
   245  // RegisterCheck run func before registry service
   246  func RegisterCheck(fn func(context.Context) error) Option {
   247  	return func(o *Options) {
   248  		o.RegisterCheck = fn
   249  	}
   250  }
   251  
   252  // HandleSignal toggles automatic installation of the signal handler that
   253  // traps TERM, INT, and QUIT.  Users of this feature to disable the signal
   254  // handler, should control liveness of the service through the context.
   255  func HandleSignal(b bool) Option {
   256  	return func(o *Options) {
   257  		o.Signal = b
   258  	}
   259  }