From 2288c8f5f224ad6baf43467c7fbe138f9167b761 Mon Sep 17 00:00:00 2001 From: ClareChu <1062186165@qq.com> Date: Fri, 7 Feb 2020 16:21:09 +0800 Subject: [PATCH 1/2] support es --- examples/es/config/application-local.yml | 7 ++ examples/es/config/application.yml | 16 ++++ examples/es/config/i18n/en-US.ini | 3 + examples/es/config/i18n/zh-CN.ini | 3 + examples/es/config/keygen.sh | 19 ++++ examples/es/config/ssl/app.rsa | 15 ++++ examples/es/config/ssl/app.rsa.pub | 6 ++ examples/es/controller/user.go | 77 ++++++++++++++++ examples/es/controller/user_test.go | 103 +++++++++++++++++++++ examples/es/entity/user.go | 32 +++++++ examples/es/entity/user_test.go | 25 ++++++ examples/es/main.go | 33 +++++++ examples/es/main_test.go | 21 +++++ examples/es/service/mocks/UserService.go | 94 ++++++++++++++++++++ examples/es/service/user.go | 108 +++++++++++++++++++++++ starter/es/autoconfigure.go | 1 + starter/es/autoconfigure_test.go | 22 +++++ starter/es/client.go | 1 + starter/es/client_test.go | 31 +++++++ 19 files changed, 617 insertions(+) create mode 100644 examples/es/config/application-local.yml create mode 100644 examples/es/config/application.yml create mode 100644 examples/es/config/i18n/en-US.ini create mode 100644 examples/es/config/i18n/zh-CN.ini create mode 100755 examples/es/config/keygen.sh create mode 100644 examples/es/config/ssl/app.rsa create mode 100644 examples/es/config/ssl/app.rsa.pub create mode 100644 examples/es/controller/user.go create mode 100644 examples/es/controller/user_test.go create mode 100644 examples/es/entity/user.go create mode 100644 examples/es/entity/user_test.go create mode 100644 examples/es/main.go create mode 100644 examples/es/main_test.go create mode 100644 examples/es/service/mocks/UserService.go create mode 100644 examples/es/service/user.go create mode 100644 starter/es/autoconfigure.go create mode 100644 starter/es/autoconfigure_test.go create mode 100644 starter/es/client.go create mode 100644 starter/es/client_test.go diff --git a/examples/es/config/application-local.yml b/examples/es/config/application-local.yml new file mode 100644 index 0000000..aaf6101 --- /dev/null +++ b/examples/es/config/application-local.yml @@ -0,0 +1,7 @@ +# config file for testing + +server: + port: 8080 + +logging: + level: info diff --git a/examples/es/config/application.yml b/examples/es/config/application.yml new file mode 100644 index 0000000..cd92c01 --- /dev/null +++ b/examples/es/config/application.yml @@ -0,0 +1,16 @@ +# config file for testing +# filename should be application.yml + +app: + project: examples + name: gorm-demo + profiles: + include: + - actuator + - locale + - logging + - gorm + +logging: + level: info + diff --git a/examples/es/config/i18n/en-US.ini b/examples/es/config/i18n/en-US.ini new file mode 100644 index 0000000..6a29eef --- /dev/null +++ b/examples/es/config/i18n/en-US.ini @@ -0,0 +1,3 @@ +success = Success +failed = Failed +hello = Hello \ No newline at end of file diff --git a/examples/es/config/i18n/zh-CN.ini b/examples/es/config/i18n/zh-CN.ini new file mode 100644 index 0000000..67856a1 --- /dev/null +++ b/examples/es/config/i18n/zh-CN.ini @@ -0,0 +1,3 @@ +success = 成功 +failed = 失败 +hello = 您好 diff --git a/examples/es/config/keygen.sh b/examples/es/config/keygen.sh new file mode 100755 index 0000000..e67ebf2 --- /dev/null +++ b/examples/es/config/keygen.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# Copyright 2018 John Deng (hi.devops.io@gmail.com). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +mkdir -p ssl + +openssl genrsa -out ssl/app.rsa 1024 +openssl rsa -in ssl/app.rsa -pubout > ssl/app.rsa.pub \ No newline at end of file diff --git a/examples/es/config/ssl/app.rsa b/examples/es/config/ssl/app.rsa new file mode 100644 index 0000000..2fbbd3d --- /dev/null +++ b/examples/es/config/ssl/app.rsa @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQDBaFochwjb/PFXA9EOmynT+SQr54wh9HMx5VFj0bNUKegMS02W +4DSsneRVaY5Bo+8sDoNPx2Luzp7pPRNbukP4pjgVWAASVWnGeBaGRaYbo/d9SV/R +OdKwmV1DJM3PtOo82oNnI26FJeJ/oTl+5UJ4sd7J8UbR0DCu40venKLlOwIDAQAB +AoGAOFuJDpEGstvlE38INyJdsOgDc0u4mNhbjOmOqvCbOeYwOQzkYheZIceslnr4 +ugXkFslv2Bp4qK/KvJpQfsUrMtGNr1FKfwLzD09++GU89ltufgn8Z6iYJ+app0qa +hYKAoGQkvPP4ks+k3nBYRESL01y9mKZ/JSvWfKj8wHOu1pkCQQD/Pdom+5r2q150 +QUkw4huDthHBB96LMJiVLSNkPoHXPwXqSoa8CXNquCQA43F1gjtCz37rZDEyFYWH +IYT4TcG1AkEAwft3WsON1ND0p8Z+iUCe/+ySajBvH4ICJ6Muj8D4FdydRvBhNJT0 +qmZYZI19Bjk8qYHV7UoUhQQcfBB8TVQhLwJBAOE10CEJbEguGR9243+wYhqrMasi +qGgkLt2hgFLZHHOx9iQuMggERsRpSctG5DuM94ER2KkWNJo+hx0epFb7NOkCQEnq +1c9LRyd6PFAWTi9k5XGmJyC5tg5ujv2lq99xa9eAypm2jYPo4tZvy2z9kU+mzdvV +srwKB4msSGJqMcORtFcCQQDJ1nXPTCgptvny+FhkFGAy3UNLdGXKeAM9UHhJW+/Z +RJvfo1hEyCPZZbPrMrPzqkJ8pBd2VRwoevlJreZDekvd +-----END RSA PRIVATE KEY----- diff --git a/examples/es/config/ssl/app.rsa.pub b/examples/es/config/ssl/app.rsa.pub new file mode 100644 index 0000000..6dd0c22 --- /dev/null +++ b/examples/es/config/ssl/app.rsa.pub @@ -0,0 +1,6 @@ +-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBaFochwjb/PFXA9EOmynT+SQr +54wh9HMx5VFj0bNUKegMS02W4DSsneRVaY5Bo+8sDoNPx2Luzp7pPRNbukP4pjgV +WAASVWnGeBaGRaYbo/d9SV/ROdKwmV1DJM3PtOo82oNnI26FJeJ/oTl+5UJ4sd7J +8UbR0DCu40venKLlOwIDAQAB +-----END PUBLIC KEY----- diff --git a/examples/es/controller/user.go b/examples/es/controller/user.go new file mode 100644 index 0000000..0a95ffb --- /dev/null +++ b/examples/es/controller/user.go @@ -0,0 +1,77 @@ +// Copyright 2018 John Deng (hi.devops.io@gmail.com). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package controller + +import ( + "hidevops.io/hiboot-data/examples/gorm/entity" + "hidevops.io/hiboot-data/examples/gorm/service" + "hidevops.io/hiboot/pkg/app" + "hidevops.io/hiboot/pkg/at" + "hidevops.io/hiboot/pkg/model" + "net/http" +) + +// RestController +type userController struct { + at.RestController + + userService service.UserService +} + +func init() { + app.Register(newUserController) +} + +// newUserController inject userService automatically +func newUserController(userService service.UserService) *userController { + return &userController{ + userService: userService, + } +} + +// Post POST /user +func (c *userController) Post(request *entity.User) (model.Response, error) { + err := c.userService.AddUser(request) + response := new(model.BaseResponse) + response.SetData(request) + return response, err +} + +// GetById GET /id/{id} +func (c *userController) GetById(id uint64) (response model.Response, err error) { + user, err := c.userService.GetUser(id) + response = new(model.BaseResponse) + if err != nil { + response.SetCode(http.StatusNotFound) + } else { + response.SetData(user) + } + return +} + +// GetById GET /id/{id} +func (c *userController) GetAll() (response model.Response, err error) { + users, err := c.userService.GetAll() + response = new(model.BaseResponse) + response.SetData(users) + return +} + +// DeleteById DELETE /id/{id} +func (c *userController) DeleteById(id uint64) (response model.Response, err error) { + err = c.userService.DeleteUser(id) + response = new(model.BaseResponse) + return +} diff --git a/examples/es/controller/user_test.go b/examples/es/controller/user_test.go new file mode 100644 index 0000000..298fb52 --- /dev/null +++ b/examples/es/controller/user_test.go @@ -0,0 +1,103 @@ +// Copyright 2018 John Deng (hi.devops.io@gmail.com). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package controller + +import ( + "errors" + "github.com/stretchr/testify/assert" + "hidevops.io/hiboot-data/examples/gorm/entity" + "hidevops.io/hiboot-data/examples/gorm/service/mocks" + "hidevops.io/hiboot/pkg/app/web" + "hidevops.io/hiboot/pkg/log" + "hidevops.io/hiboot/pkg/utils/idgen" + "net/http" + "testing" +) + +func init() { + log.SetLevel(log.DebugLevel) +} + +func TestCrdRequest(t *testing.T) { + + mockUserService := new(mocks.UserService) + userController := newUserController(mockUserService) + testApp := web.NewTestApp(userController).Run(t) + + id, err := idgen.Next() + assert.Equal(t, nil, err) + + testUser := &entity.User{ + Id: id, + Name: "Bill Gates", + Username: "billg", + Password: "3948tdaD", + Email: "bill.gates@microsoft.com", + Age: 60, + Gender: 1, + } + + // first, call mocks.UserService.AddUser + mockUserService.On("AddUser", testUser).Return(nil) + // then run the test that will call UserService.AddUser + t.Run("should add user with POST request", func(t *testing.T) { + // First, let's Post User + testApp.Post("/user"). + WithJSON(testUser). + Expect().Status(http.StatusOK) + }) + + mockUserService.On("GetUser", id).Return(testUser, nil) + t.Run("should get user with GET request", func(t *testing.T) { + // Then Get User + // e.g. GET /user/id/123456 + testApp.Get("/user/id/{id}"). + WithPath("id", id). + Expect().Status(http.StatusOK) + }) + + mockUserService.On("GetAll").Return(&[]entity.User{*testUser}, nil) + t.Run("should get user with GET request", func(t *testing.T) { + // Then Get User + // e.g. GET /user/id/123456 + testApp.Get("/user/all"). + Expect().Status(http.StatusOK) + }) + + // assert that the expectations were met + mockUserService.AssertExpectations(t) + + unknownId, err := idgen.Next() + assert.Equal(t, nil, err) + mockUserService.On("GetUser", unknownId).Return((*entity.User)(nil), errors.New("not found")) + + t.Run("should return 404 if trying to find a record that does not exist", func(t *testing.T) { + // Then Get User + testApp.Get("/user/id/{id}"). + WithPath("id", unknownId). + Expect().Status(http.StatusNotFound) + }) + + // assert that the expectations were met + mockUserService.AssertExpectations(t) + + mockUserService.On("DeleteUser", id).Return(nil) + t.Run("should delete the record with DELETE request", func(t *testing.T) { + // Finally Delete User + testApp.Delete("/user/id/{id}"). + WithPath("id", id). + Expect().Status(http.StatusOK) + }) +} diff --git a/examples/es/entity/user.go b/examples/es/entity/user.go new file mode 100644 index 0000000..b8067eb --- /dev/null +++ b/examples/es/entity/user.go @@ -0,0 +1,32 @@ +// Copyright 2018 John Deng (hi.devops.io@gmail.com). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package entity + +import "hidevops.io/hiboot/pkg/model" + +type User struct { + model.RequestBody + Id uint64 `json:"id"` + Name string `json:"name" validate:"required"` + Username string `json:"username" validate:"required"` + Password string `json:"password" validate:"required"` + Email string `json:"email" validate:"required,email"` + Age uint `json:"age" validate:"gte=0,lte=130"` + Gender uint `json:"gender" validate:"gte=0,lte=2"` +} + +func (u *User) TableName() string { + return "user" +} diff --git a/examples/es/entity/user_test.go b/examples/es/entity/user_test.go new file mode 100644 index 0000000..72196a4 --- /dev/null +++ b/examples/es/entity/user_test.go @@ -0,0 +1,25 @@ +// Copyright 2018 John Deng (hi.devops.io@gmail.com). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package entity + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestUser(t *testing.T) { + u := new(User) + assert.Equal(t, "user", u.TableName()) +} diff --git a/examples/es/main.go b/examples/es/main.go new file mode 100644 index 0000000..2e45523 --- /dev/null +++ b/examples/es/main.go @@ -0,0 +1,33 @@ +// Copyright 2018 John Deng (hi.devops.io@gmail.com). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + _ "hidevops.io/hiboot-data/examples/gorm/controller" + "hidevops.io/hiboot/pkg/app" + "hidevops.io/hiboot/pkg/app/web" + "hidevops.io/hiboot/pkg/starter/actuator" + "hidevops.io/hiboot/pkg/starter/locale" + "hidevops.io/hiboot/pkg/starter/logging" +) + +func main() { + web.NewApplication(). + SetProperty(app.ProfilesInclude, + actuator.Profile, + locale.Profile, + logging.Profile). + Run() +} diff --git a/examples/es/main_test.go b/examples/es/main_test.go new file mode 100644 index 0000000..7129c7f --- /dev/null +++ b/examples/es/main_test.go @@ -0,0 +1,21 @@ +// Copyright 2018 John Deng (hi.devops.io@gmail.com). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import "testing" + +func TestRunMain(t *testing.T) { + go main() +} diff --git a/examples/es/service/mocks/UserService.go b/examples/es/service/mocks/UserService.go new file mode 100644 index 0000000..ade4869 --- /dev/null +++ b/examples/es/service/mocks/UserService.go @@ -0,0 +1,94 @@ +// Code generated by mockery v1.0.0. DO NOT EDIT. + +package mocks + +import entity "hidevops.io/hiboot-data/examples/es/entity" +import mock "github.com/stretchr/testify/mock" + +// UserService is an autogenerated mock type for the UserService type +type UserService struct { + mock.Mock +} + +// AddUser provides a mock function with given fields: user +func (_m *UserService) AddUser(user *entity.User) (*entity.User, error) { + ret := _m.Called(user) + + var r0 *entity.User + if rf, ok := ret.Get(0).(func(*entity.User) *entity.User); ok { + r0 = rf(user) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*entity.User) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(*entity.User) error); ok { + r1 = rf(user) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// DeleteUser provides a mock function with given fields: id +func (_m *UserService) DeleteUser(id string) error { + ret := _m.Called(id) + + var r0 error + if rf, ok := ret.Get(0).(func(string) error); ok { + r0 = rf(id) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// GetAll provides a mock function with given fields: +func (_m *UserService) GetAll() (*[]entity.User, error) { + ret := _m.Called() + + var r0 *[]entity.User + if rf, ok := ret.Get(0).(func() *[]entity.User); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*[]entity.User) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetUser provides a mock function with given fields: id +func (_m *UserService) GetUser(id string) (*entity.User, error) { + ret := _m.Called(id) + + var r0 *entity.User + if rf, ok := ret.Get(0).(func(string) *entity.User); ok { + r0 = rf(id) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*entity.User) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(id) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} diff --git a/examples/es/service/user.go b/examples/es/service/user.go new file mode 100644 index 0000000..009ced9 --- /dev/null +++ b/examples/es/service/user.go @@ -0,0 +1,108 @@ +// Copyright 2018 John Deng (hi.devops.io@gmail.com). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package service + +import ( + "context" + "encoding/json" + "fmt" + "github.com/olivere/elastic/v6" + "hidevops.io/hiboot-data/examples/es/entity" + "hidevops.io/hiboot-data/starter/es" + "hidevops.io/hiboot/pkg/app" + "hidevops.io/hiboot/pkg/utils/idgen" + "log" +) + +type UserService interface { + AddUser(user *entity.User) (newUser *entity.User, err error) + GetUser(id string) (user *entity.User, err error) + GetAll() (user *[]entity.User, err error) + DeleteUser(id string) (err error) +} + +type userServiceImpl struct { + client *es.Client +} + +func init() { + // register UserServiceImpl + app.Register(newUserService) +} + +// will inject gorm.Repository that configured in hidevops.io/hiboot-data/starter/gorm +func newUserService(client *es.Client) UserService { + return &userServiceImpl{ + client: client, + } +} + +func (s *userServiceImpl) AddUser(user *entity.User) (newUser *entity.User, err error) { + if user.Id == "" { + id, err := idgen.NextString() + if err != nil { + return nil, err + } + user.Id = id + } + _, err = s.client.Index().Index("test").Type("test").Id(user.Id).BodyJson(user).Do(context.Background()) + newUser = user + return +} + +func (s *userServiceImpl) GetUser(id string) (user *entity.User, err error) { + esResponse, err := s.client.Get().Index("test").Type("test").Id(id).Do(context.Background()) + if err != nil { + // Handle Error + return + } + err = json.Unmarshal(*esResponse.Source, &user) + return +} + +func (s *userServiceImpl) GetAll() (users *[]entity.User, err error) { + var users1 []entity.User + //err = s.repository.Find(users).Error() + ctx := context.Background() + query := elastic.NewBoolQuery() + res, err := s.client.Search().Index("test").Type("test").Size(10).Query(query).Do(ctx) + if err != nil { + log.Println("err:", err) + return + } + + if res.TotalHits() > 0 { + log.Printf("Found a total of %d indice\n", res.TotalHits()) + for _, hit := range res.Hits.Hits { + var user entity.User + err := json.Unmarshal(*hit.Source, &user) + if err != nil { + log.Printf("%v", err) + return nil, err + } + fmt.Printf("user: %v", user) + users1 = append(users1, user) + } + } else { + log.Println("没有查询到数据") + } + users = &users1 + return +} + +func (s *userServiceImpl) DeleteUser(id string) (err error) { + _, err = s.client.Delete().Index("test").Type("test").Id(id).Do(context.Background()) + return +} diff --git a/starter/es/autoconfigure.go b/starter/es/autoconfigure.go new file mode 100644 index 0000000..910142c --- /dev/null +++ b/starter/es/autoconfigure.go @@ -0,0 +1 @@ +package es diff --git a/starter/es/autoconfigure_test.go b/starter/es/autoconfigure_test.go new file mode 100644 index 0000000..a43b58f --- /dev/null +++ b/starter/es/autoconfigure_test.go @@ -0,0 +1,22 @@ +package amqp + +import ( + "github.com/magiconair/properties/assert" + "hidevops.io/hiboot/pkg/log" + "testing" +) + +func TestAmqp(t *testing.T) { + log.SetLevel(log.DebugLevel) + conf := newConfiguration(&Properties{ + Username: "user", + Password: "password", + Host: "127.0.0.1", + Port: 1111, + }) + + ch := conf.Channel() + var c *Channel + assert.Equal(t, c, ch) + +} diff --git a/starter/es/client.go b/starter/es/client.go new file mode 100644 index 0000000..910142c --- /dev/null +++ b/starter/es/client.go @@ -0,0 +1 @@ +package es diff --git a/starter/es/client_test.go b/starter/es/client_test.go new file mode 100644 index 0000000..2b1608e --- /dev/null +++ b/starter/es/client_test.go @@ -0,0 +1,31 @@ +package es + +import ( + "fmt" + "github.com/olivere/elastic/v6" + "hidevops.io/hiboot/pkg/at" +) + +type Properties struct { + at.ConfigurationProperties `value:"es"` + Port int `json:"port" default:"5672"` + Host string `json:"host" default:"127.0.0.1"` +} + +type Client struct { + *elastic.Client +} + +func newClient() (client *Client) { + return new(Client) +} + +func (c *Client) Connect(p *Properties) (err error) { + esUrl := fmt.Sprintf("http://%s:%d", p.Host, p.Port) + client, err := elastic.NewSimpleClient(elastic.SetURL(esUrl)) + if err != nil { + return + } + c.Client = client + return +} From ee746d880ca9c4bde3a2fbd8fe4dc33ec6131528 Mon Sep 17 00:00:00 2001 From: ClareChu <1062186165@qq.com> Date: Fri, 7 Feb 2020 16:25:34 +0800 Subject: [PATCH 2/2] support es --- examples/es/config/application-local.yml | 4 +++ examples/es/config/application.yml | 4 +-- examples/es/controller/user.go | 12 ++++----- examples/es/controller/user_test.go | 10 +++---- examples/es/entity/user.go | 18 +++++-------- examples/es/entity/user_test.go | 10 ------- examples/es/main.go | 2 +- go.mod | 2 ++ go.sum | 8 ++++++ starter/es/autoconfigure.go | 31 ++++++++++++++++++++++ starter/es/autoconfigure_test.go | 12 +++------ starter/es/client.go | 30 +++++++++++++++++++++ starter/es/client_test.go | 33 +++++++----------------- 13 files changed, 109 insertions(+), 67 deletions(-) diff --git a/examples/es/config/application-local.yml b/examples/es/config/application-local.yml index aaf6101..6e21ead 100644 --- a/examples/es/config/application-local.yml +++ b/examples/es/config/application-local.yml @@ -5,3 +5,7 @@ server: logging: level: info + +es: + host: 10.10.13.5 + port: 9200 \ No newline at end of file diff --git a/examples/es/config/application.yml b/examples/es/config/application.yml index cd92c01..83a39e6 100644 --- a/examples/es/config/application.yml +++ b/examples/es/config/application.yml @@ -3,13 +3,13 @@ app: project: examples - name: gorm-demo + name: es-demo profiles: include: - actuator - locale - logging - - gorm + - es logging: level: info diff --git a/examples/es/controller/user.go b/examples/es/controller/user.go index 0a95ffb..b9c8489 100644 --- a/examples/es/controller/user.go +++ b/examples/es/controller/user.go @@ -15,8 +15,8 @@ package controller import ( - "hidevops.io/hiboot-data/examples/gorm/entity" - "hidevops.io/hiboot-data/examples/gorm/service" + "hidevops.io/hiboot-data/examples/es/entity" + "hidevops.io/hiboot-data/examples/es/service" "hidevops.io/hiboot/pkg/app" "hidevops.io/hiboot/pkg/at" "hidevops.io/hiboot/pkg/model" @@ -43,14 +43,14 @@ func newUserController(userService service.UserService) *userController { // Post POST /user func (c *userController) Post(request *entity.User) (model.Response, error) { - err := c.userService.AddUser(request) + user, err := c.userService.AddUser(request) response := new(model.BaseResponse) - response.SetData(request) + response.SetData(user) return response, err } // GetById GET /id/{id} -func (c *userController) GetById(id uint64) (response model.Response, err error) { +func (c *userController) GetById(id string) (response model.Response, err error) { user, err := c.userService.GetUser(id) response = new(model.BaseResponse) if err != nil { @@ -70,7 +70,7 @@ func (c *userController) GetAll() (response model.Response, err error) { } // DeleteById DELETE /id/{id} -func (c *userController) DeleteById(id uint64) (response model.Response, err error) { +func (c *userController) DeleteById(id string) (response model.Response, err error) { err = c.userService.DeleteUser(id) response = new(model.BaseResponse) return diff --git a/examples/es/controller/user_test.go b/examples/es/controller/user_test.go index 298fb52..dc2ad88 100644 --- a/examples/es/controller/user_test.go +++ b/examples/es/controller/user_test.go @@ -17,8 +17,8 @@ package controller import ( "errors" "github.com/stretchr/testify/assert" - "hidevops.io/hiboot-data/examples/gorm/entity" - "hidevops.io/hiboot-data/examples/gorm/service/mocks" + "hidevops.io/hiboot-data/examples/es/entity" + "hidevops.io/hiboot-data/examples/es/service/mocks" "hidevops.io/hiboot/pkg/app/web" "hidevops.io/hiboot/pkg/log" "hidevops.io/hiboot/pkg/utils/idgen" @@ -36,7 +36,7 @@ func TestCrdRequest(t *testing.T) { userController := newUserController(mockUserService) testApp := web.NewTestApp(userController).Run(t) - id, err := idgen.Next() + id, err := idgen.NextString() assert.Equal(t, nil, err) testUser := &entity.User{ @@ -50,7 +50,7 @@ func TestCrdRequest(t *testing.T) { } // first, call mocks.UserService.AddUser - mockUserService.On("AddUser", testUser).Return(nil) + mockUserService.On("AddUser", testUser).Return(testUser, nil) // then run the test that will call UserService.AddUser t.Run("should add user with POST request", func(t *testing.T) { // First, let's Post User @@ -79,7 +79,7 @@ func TestCrdRequest(t *testing.T) { // assert that the expectations were met mockUserService.AssertExpectations(t) - unknownId, err := idgen.Next() + unknownId, err := idgen.NextString() assert.Equal(t, nil, err) mockUserService.On("GetUser", unknownId).Return((*entity.User)(nil), errors.New("not found")) diff --git a/examples/es/entity/user.go b/examples/es/entity/user.go index b8067eb..f63ff54 100644 --- a/examples/es/entity/user.go +++ b/examples/es/entity/user.go @@ -18,15 +18,11 @@ import "hidevops.io/hiboot/pkg/model" type User struct { model.RequestBody - Id uint64 `json:"id"` - Name string `json:"name" validate:"required"` - Username string `json:"username" validate:"required"` - Password string `json:"password" validate:"required"` - Email string `json:"email" validate:"required,email"` - Age uint `json:"age" validate:"gte=0,lte=130"` - Gender uint `json:"gender" validate:"gte=0,lte=2"` -} - -func (u *User) TableName() string { - return "user" + Id string `json:"id"` + Name string `json:"name"` + Username string `json:"username"` + Password string `json:"password"` + Email string `json:"email"` + Age uint `json:"age"` + Gender uint `json:"gender"` } diff --git a/examples/es/entity/user_test.go b/examples/es/entity/user_test.go index 72196a4..f480794 100644 --- a/examples/es/entity/user_test.go +++ b/examples/es/entity/user_test.go @@ -13,13 +13,3 @@ // limitations under the License. package entity - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestUser(t *testing.T) { - u := new(User) - assert.Equal(t, "user", u.TableName()) -} diff --git a/examples/es/main.go b/examples/es/main.go index 2e45523..61ea73b 100644 --- a/examples/es/main.go +++ b/examples/es/main.go @@ -15,7 +15,7 @@ package main import ( - _ "hidevops.io/hiboot-data/examples/gorm/controller" + _ "hidevops.io/hiboot-data/examples/es/controller" "hidevops.io/hiboot/pkg/app" "hidevops.io/hiboot/pkg/app/web" "hidevops.io/hiboot/pkg/starter/actuator" diff --git a/go.mod b/go.mod index d88565d..4e9bc62 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,8 @@ require ( github.com/lib/pq v1.0.0 // indirect github.com/magiconair/properties v1.8.0 github.com/mattn/go-sqlite3 v1.9.0 // indirect + github.com/olivere/elastic v6.2.27+incompatible // indirect + github.com/olivere/elastic/v6 v6.2.1 github.com/prometheus/client_golang v1.1.0 // indirect github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect github.com/soheilhy/cmux v0.1.4 // indirect diff --git a/go.sum b/go.sum index 28bb93a..69c22a1 100644 --- a/go.sum +++ b/go.sum @@ -67,6 +67,8 @@ github.com/fatih/structs v1.0.0 h1:BrX964Rv5uQ3wwS+KRUAJCBBw5PQmgJfJ6v4yly5QwU= github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/flosch/pongo2 v0.0.0-20180809100617-24195e6d38b0 h1:ZHx2BEERvWkuwuE7qWN9TuRxucHDH2JrsvneZjVJfo0= github.com/flosch/pongo2 v0.0.0-20180809100617-24195e6d38b0/go.mod h1:rE0ErqqBaMcp9pzj8JxV1GcfDBpuypXYxlR1c37AUwg= +github.com/fortytw2/leaktest v1.2.0 h1:cj6GCiwJDH7l3tMHLjZDo0QqPtrXJiWSI9JgpeQKw+Q= +github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/gavv/monotime v0.0.0-20171021193802-6f8212e8d10d h1:oYXrtNhqNKL1dVtKdv8XUq5zqdGVFNQ0/4tvccXZOLM= @@ -229,6 +231,7 @@ github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDe github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63 h1:nTT4s92Dgz2HlrB2NaMgvlfqHH39OgMhA7z3PK7PGD4= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= @@ -254,6 +257,10 @@ github.com/moul/http2curl v0.0.0-20170919181001-9ac6cf4d929b/go.mod h1:8UbvGypXm github.com/moul/http2curl v1.0.0 h1:dRMWoAtb+ePxMlLkrCbAqh4TlPHXvoGUSQ323/9Zahs= github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/olivere/elastic v6.2.27+incompatible h1:c57kY8PF/J6Iz2ATxHQkWFNkYyKDlEZr6hl/O5ZFNvQ= +github.com/olivere/elastic v6.2.27+incompatible/go.mod h1:J+q1zQJTgAz9woqsbVRqGeB5G1iqDKVBWLNSYW8yfJ8= +github.com/olivere/elastic/v6 v6.2.1 h1:tZ2NZWoFCdFnuQg1q9JCyjN6YTczNF03tLj954ptqNc= +github.com/olivere/elastic/v6 v6.2.1/go.mod h1:OeCPPyGCIn9j7/1Dk+tGE7gsezYo9lsJIiHhZjT/qQ4= github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.4.2 h1:3mYCb7aPxS/RU7TI1y4rkEn1oKmPRjNJLNEXgw7MH2I= @@ -299,6 +306,7 @@ github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4 github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM= github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a h1:JSvGDIbmil4Ui/dDdFBExb7/cmkNjyX5F97oglmvCDo= github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s= github.com/soheilhy/cmux v0.1.4 h1:0HKaf1o97UwFjHH9o5XsHUOF+tqmdA7KEzXLpiyaw0E= diff --git a/starter/es/autoconfigure.go b/starter/es/autoconfigure.go index 910142c..bc6761f 100644 --- a/starter/es/autoconfigure.go +++ b/starter/es/autoconfigure.go @@ -1 +1,32 @@ package es + +import ( + "hidevops.io/hiboot/pkg/app" +) + +const Profile = "es" + +type configuration struct { + app.Configuration + // the properties member name must be amqp if the mapstructure is amqp, + // so that the reference can be parsed + Properties *Properties +} + +func newConfiguration(properties *Properties) *configuration { + return &configuration{Properties: properties} +} + +func init() { + app.Register(newConfiguration, new(Properties)) +} + +// Repository method name must be unique +func (c *configuration) Client() *Client { + client := newClient() + err := client.Connect(c.Properties) + if err != nil { + return nil + } + return client +} diff --git a/starter/es/autoconfigure_test.go b/starter/es/autoconfigure_test.go index a43b58f..2a002f1 100644 --- a/starter/es/autoconfigure_test.go +++ b/starter/es/autoconfigure_test.go @@ -1,22 +1,16 @@ -package amqp +package es import ( - "github.com/magiconair/properties/assert" "hidevops.io/hiboot/pkg/log" "testing" ) -func TestAmqp(t *testing.T) { +func TestClient_Connect(t *testing.T) { log.SetLevel(log.DebugLevel) conf := newConfiguration(&Properties{ - Username: "user", - Password: "password", Host: "127.0.0.1", Port: 1111, }) - ch := conf.Channel() - var c *Channel - assert.Equal(t, c, ch) - + conf.Client() } diff --git a/starter/es/client.go b/starter/es/client.go index 910142c..2b1608e 100644 --- a/starter/es/client.go +++ b/starter/es/client.go @@ -1 +1,31 @@ package es + +import ( + "fmt" + "github.com/olivere/elastic/v6" + "hidevops.io/hiboot/pkg/at" +) + +type Properties struct { + at.ConfigurationProperties `value:"es"` + Port int `json:"port" default:"5672"` + Host string `json:"host" default:"127.0.0.1"` +} + +type Client struct { + *elastic.Client +} + +func newClient() (client *Client) { + return new(Client) +} + +func (c *Client) Connect(p *Properties) (err error) { + esUrl := fmt.Sprintf("http://%s:%d", p.Host, p.Port) + client, err := elastic.NewSimpleClient(elastic.SetURL(esUrl)) + if err != nil { + return + } + c.Client = client + return +} diff --git a/starter/es/client_test.go b/starter/es/client_test.go index 2b1608e..79d0144 100644 --- a/starter/es/client_test.go +++ b/starter/es/client_test.go @@ -1,31 +1,18 @@ package es import ( - "fmt" - "github.com/olivere/elastic/v6" + "github.com/magiconair/properties/assert" "hidevops.io/hiboot/pkg/at" + "testing" ) -type Properties struct { - at.ConfigurationProperties `value:"es"` - Port int `json:"port" default:"5672"` - Host string `json:"host" default:"127.0.0.1"` -} - -type Client struct { - *elastic.Client -} - -func newClient() (client *Client) { - return new(Client) -} - -func (c *Client) Connect(p *Properties) (err error) { - esUrl := fmt.Sprintf("http://%s:%d", p.Host, p.Port) - client, err := elastic.NewSimpleClient(elastic.SetURL(esUrl)) - if err != nil { - return +func TestConnect(t *testing.T) { + pro := &Properties{ + ConfigurationProperties: at.ConfigurationProperties{}, + Port: 0, + Host: "", } - c.Client = client - return + client := newClient() + err := client.Connect(pro) + assert.Equal(t, nil, err) }