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