feat:完善产品详情
This commit is contained in:
@ -1,32 +1,71 @@
|
||||
package es
|
||||
|
||||
import "github.com/elastic/go-elasticsearch/v7"
|
||||
import (
|
||||
"github.com/elastic/go-elasticsearch/v7"
|
||||
)
|
||||
|
||||
type Es struct{ *EsConfig }
|
||||
type Instance struct {
|
||||
client *elasticsearch.Client
|
||||
|
||||
type EsConfig struct {
|
||||
Address []string
|
||||
address []string
|
||||
username, password string
|
||||
}
|
||||
|
||||
type EsServer func(*EsConfig) *Es
|
||||
type Option func(*Instance)
|
||||
|
||||
var esClient = new(elasticsearch.Client)
|
||||
var (
|
||||
client *elasticsearch.Client
|
||||
)
|
||||
|
||||
func (this *Es) Run() {
|
||||
func WithEsAddress(address []string) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.address = address
|
||||
}
|
||||
}
|
||||
|
||||
func WithEsUsername(username string) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.username = username
|
||||
}
|
||||
}
|
||||
|
||||
func WithEsPassword(password string) Option {
|
||||
return func(instance *Instance) {
|
||||
instance.password = password
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Instance) Init() *Instance {
|
||||
obj := elasticsearch.Config{
|
||||
Addresses: this.Address,
|
||||
Username: "",
|
||||
Password: "",
|
||||
Addresses: this.address,
|
||||
Username: this.username,
|
||||
Password: this.password,
|
||||
}
|
||||
var err error
|
||||
client, err := elasticsearch.NewClient(obj)
|
||||
|
||||
if esClient, err = elasticsearch.NewClient(obj); err != nil {
|
||||
panic("Elasticsearch Error " + err.Error())
|
||||
if err != nil {
|
||||
panic("Elasticsearch New Error " + err.Error())
|
||||
}
|
||||
|
||||
if _, err = client.Ping(); err != nil {
|
||||
panic("Elasticsearch Ping Error " + err.Error())
|
||||
|
||||
}
|
||||
this.client = client
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
func NewEs() EsServer {
|
||||
return func(config *EsConfig) *Es {
|
||||
return &Es{config}
|
||||
}
|
||||
func (this *Instance) Local() *Instance {
|
||||
client = this.client
|
||||
return this
|
||||
}
|
||||
|
||||
func NewInstance(option ...Option) *Instance {
|
||||
instance := new(Instance)
|
||||
|
||||
for _, v := range option {
|
||||
v(instance)
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
Reference in New Issue
Block a user