1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/web/options.go

279 lines
5.4 KiB
Go
Raw Normal View History

2021-07-06 12:12:19 +02:00
package web
import (
"context"
"crypto/tls"
"net/http"
"time"
"github.com/urfave/cli/v2"
2024-06-04 22:40:43 +02:00
"go-micro.dev/v5"
"go-micro.dev/v5/logger"
"go-micro.dev/v5/registry"
2021-07-06 12:12:19 +02:00
)
2022-09-30 16:27:07 +02:00
// Options for web.
2021-07-06 12:12:19 +02:00
type Options struct {
2023-04-26 02:16:34 +02:00
Handler http.Handler
2021-07-06 12:12:19 +02:00
2023-04-26 02:16:34 +02:00
Logger logger.Logger
2021-07-06 12:12:19 +02:00
2023-04-26 02:16:34 +02:00
Service micro.Service
Registry registry.Registry
// Alternative Options
Context context.Context
Action func(*cli.Context)
Metadata map[string]string
TLSConfig *tls.Config
Server *http.Server
2021-07-06 12:12:19 +02:00
// RegisterCheck runs a check function before registering the service
RegisterCheck func(context.Context) error
2023-04-26 02:16:34 +02:00
Version string
2021-07-06 12:12:19 +02:00
2023-04-26 02:16:34 +02:00
// Static directory
StaticDir string
2021-07-06 12:12:19 +02:00
2023-04-26 02:16:34 +02:00
Advertise string
Address string
Name string
Id string
Flags []cli.Flag
2021-07-06 12:12:19 +02:00
BeforeStart []func() error
BeforeStop []func() error
AfterStart []func() error
AfterStop []func() error
2023-04-26 02:16:34 +02:00
RegisterInterval time.Duration
RegisterTTL time.Duration
Secure bool
2021-07-06 12:12:19 +02:00
Signal bool
}
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,
Logger: logger.DefaultLogger,
2021-07-06 12:12:19 +02:00
}
for _, o := range opts {
o(&opt)
}
if opt.RegisterCheck == nil {
opt.RegisterCheck = DefaultRegisterCheck
}
return opt
}
2022-09-30 16:27:07 +02:00
// Name of Web.
2021-07-06 12:12:19 +02:00
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}
2022-09-30 16:27:07 +02:00
// Icon specifies an icon url to load in the UI.
2021-07-06 12:12:19 +02:00
func Icon(ico string) Option {
return func(o *Options) {
if o.Metadata == nil {
o.Metadata = make(map[string]string)
}
2022-09-30 20:32:55 +02:00
2021-07-06 12:12:19 +02:00
o.Metadata["icon"] = ico
}
}
2022-09-30 16:27:07 +02:00
// Id for Unique server id.
2021-07-06 12:12:19 +02:00
func Id(id string) Option {
return func(o *Options) {
o.Id = id
}
}
2022-09-30 16:27:07 +02:00
// Version of the service.
2021-07-06 12:12:19 +02:00
func Version(v string) Option {
return func(o *Options) {
o.Version = v
}
}
2022-09-30 16:27:07 +02:00
// Metadata associated with the service.
2021-07-06 12:12:19 +02:00
func Metadata(md map[string]string) Option {
return func(o *Options) {
o.Metadata = md
}
}
2022-09-30 16:27:07 +02:00
// Address to bind to - host:port.
2021-07-06 12:12:19 +02:00
func Address(a string) Option {
return func(o *Options) {
o.Address = a
}
}
2022-09-30 16:27:07 +02:00
// Advertise The address to advertise for discovery - host:port.
2021-07-06 12:12:19 +02: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
}
}
2022-09-30 16:27:07 +02:00
// Registry used for discovery.
2021-07-06 12:12:19 +02:00
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
}
}
2022-09-30 16:27:07 +02:00
// RegisterTTL Register the service with a TTL.
2021-07-06 12:12:19 +02:00
func RegisterTTL(t time.Duration) Option {
return func(o *Options) {
o.RegisterTTL = t
}
}
2022-09-30 16:27:07 +02:00
// RegisterInterval Register the service with at interval.
2021-07-06 12:12:19 +02:00
func RegisterInterval(t time.Duration) Option {
return func(o *Options) {
o.RegisterInterval = t
}
}
2022-09-30 16:27:07 +02:00
// Handler for custom handler.
2021-07-06 12:12:19 +02:00
func Handler(h http.Handler) Option {
return func(o *Options) {
o.Handler = h
}
}
2022-09-30 16:27:07 +02:00
// Server for custom Server.
2021-07-06 12:12:19 +02:00
func Server(srv *http.Server) Option {
return func(o *Options) {
o.Server = srv
}
}
2022-09-30 16:27:07 +02:00
// MicroService sets the micro.Service used internally.
2021-07-06 12:12:19 +02:00
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)
}
}
2022-09-30 20:32:55 +02:00
// Secure Use secure communication.
// If TLSConfig is not specified we use InsecureSkipVerify and generate a self signed cert.
2021-07-06 12:12:19 +02:00
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
}
}
2022-09-30 16:27:07 +02:00
// StaticDir sets the static file directory. This defaults to ./html.
2021-07-06 12:12:19 +02:00
func StaticDir(d string) Option {
return func(o *Options) {
o.StaticDir = d
}
}
2022-09-30 16:27:07 +02:00
// RegisterCheck run func before registry service.
2021-07-06 12:12:19 +02:00
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
}
}
2022-09-30 16:27:07 +02:00
// Logger sets the underline logger.
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}