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