123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package micro
- import (
- "context"
- "strings"
- "git.nspix.com/golang/micro/registry"
- )
- type (
- Options struct {
- Zone string //注册域
- Name string //名称
- Version string //版本号
- EnableHttp bool //启用HTTP功能
- EnableRPC bool //启用RPC功能
- EnableInternalListener bool //启用内置网络监听服务
- DisableRegister bool //禁用注册
- registry registry.Registry //注册仓库
- Server Server //加载的服务
- Port int //绑定端口
- Address string //绑定地址
- EnableHttpPProf bool //启用HTTP调试工具
- EnableStats bool //启用数据统计
- EnableLogPrefix bool //启用日志前缀
- Context context.Context
- shortName string
- }
- Option func(o *Options)
- )
- func (o *Options) ShortName() string {
- if o.shortName != "" {
- return o.shortName
- }
- if pos := strings.LastIndex(o.Name, "/"); pos != -1 {
- o.shortName = o.Name[pos+1:]
- } else {
- o.shortName = o.Name
- }
- return o.shortName
- }
- func WithName(name string, version string) Option {
- return func(o *Options) {
- o.Name = name
- o.Version = version
- }
- }
- func WithPort(port int) Option {
- return func(o *Options) {
- o.Port = port
- }
- }
- func WithStats() Option {
- return func(o *Options) {
- o.EnableStats = true
- }
- }
- func WithHttpDebug() Option {
- return func(o *Options) {
- o.EnableHttpPProf = true
- }
- }
- func WithRegistry(r registry.Registry) Option {
- return func(o *Options) {
- o.registry = r
- }
- }
- func WithServer(s Server) Option {
- return func(o *Options) {
- o.Server = s
- }
- }
- func WithoutHttp() Option {
- return func(o *Options) {
- o.EnableHttp = false
- }
- }
- func WithoutRegister() Option {
- return func(o *Options) {
- o.DisableRegister = true
- }
- }
- func WithoutLogPrefix() Option {
- return func(o *Options) {
- o.EnableLogPrefix = false
- }
- }
- func WithoutRPC() Option {
- return func(o *Options) {
- o.EnableRPC = false
- }
- }
- func NewOptions() *Options {
- opts := &Options{
- Zone: "default",
- Version: "1.0.1",
- EnableHttp: true,
- EnableRPC: true,
- EnableInternalListener: true,
- EnableLogPrefix: true,
- Context: context.Background(),
- registry: registry.DefaultRegistry,
- }
- return opts
- }
|