Skip to content

Commit

Permalink
Merge pull request #25 from hidevopsio/v2
Browse files Browse the repository at this point in the history
v1.3.0
  • Loading branch information
john-deng authored Oct 10, 2019
2 parents 9c833b0 + 25ef2f5 commit 476563f
Show file tree
Hide file tree
Showing 30 changed files with 460 additions and 312 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ git:
branches:
only:
- master
- v2

go_import_path: hidevops.io/hiboot-data

Expand Down
7 changes: 7 additions & 0 deletions examples/amqp/config/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ server:

logging:
level: debug


amqp:
host: localhost
port: 5672
username: root
password: 123456
6 changes: 6 additions & 0 deletions examples/amqp/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ app:
include:
- amqp

amqp:
host: amqp
port: 5672
username: root
password: 123456

logging:
level: debug
26 changes: 21 additions & 5 deletions examples/amqp/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ package controller
import (
"hidevops.io/hiboot-data/examples/amqp/service"
"hidevops.io/hiboot/pkg/app"
"hidevops.io/hiboot/pkg/app/web"
"hidevops.io/hiboot/pkg/at"
"hidevops.io/hiboot/pkg/model"
)

//hi: RestController
type UserController struct {
web.Controller
at.RestController
at.RequestMapping `value:"/user"`

userService *service.UserService
}

Expand All @@ -39,18 +41,32 @@ func newUserController(userService *service.UserService) *UserController {
}

// Post /user
func (c *UserController) PostPublish() (model.Response, error) {
func (c *UserController) Publish(at struct{at.PostMapping `value:"/publish"`}) (model.Response, error) {
err := c.userService.PublishFanout()
return nil, err
}

func (c *UserController) Push(at struct{at.PostMapping `value:"/push"`}) (model.Response, error) {
err := c.userService.Publish()
return nil, err
}

func (c *UserController) PostReceive() (model.Response, error) {
func (c *UserController) Receive(at struct{at.PostMapping `value:"/receive"`}) (model.Response, error) {
c.userService.ReceiveFanout()
return nil, nil
}

func (c *UserController) PostReceive1() (model.Response, error) {
func (c *UserController) Receive1(at struct{at.PostMapping `value:"/receive1"`}) (model.Response, error) {
c.userService.ReceiveFanout3()
return nil, nil
}

func (c *UserController) Create(at struct{at.PostMapping `value:"/create"`}) (model.Response, error) {
err := c.userService.Create()
return nil, err
}

func (c *UserController) Create1(at struct{at.PostMapping `value:"/create1"`}) (model.Response, error) {
err := c.userService.Create1()
return nil, err
}
133 changes: 52 additions & 81 deletions examples/amqp/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,126 +15,97 @@
package service

import (
"fmt"
str_amqp "github.com/streadway/amqp"
"hidevops.io/hiboot-data/starter/amqp"
"hidevops.io/hiboot/pkg/app"
"hidevops.io/hiboot/pkg/log"

"time"
)

type UserService struct {
channel *amqp.Channel
newChannel amqp.NewChannel
}

func init() {
app.Register(newUserService)
}

// will inject BoltRepository that configured in hidevops.io/hiboot/pkg/starter/data/bolt
func newUserService(channel *amqp.Channel) *UserService {
func newUserService(newChannel amqp.NewChannel) *UserService {
return &UserService{
channel: channel,
newChannel: newChannel,
}
}

const (
mgsConnect = "hello world"
exchange = "test1"
exchange = "test23223"
queueName = "Test"
key = "hh"
)

func (s *UserService) PublishDirect() (err error) {
if s.channel == nil {
return fmt.Errorf("channel is not initialzied")
}
func (s *UserService) Create() error {
shn := s.newChannel()
err := shn.CreateFanout(queueName, exchange)
return err
}

err = s.channel.PublishDirect(exchange, queueName, "hello", "info")
return
func (s *UserService) Create1() error {
shn := s.newChannel()
err := shn.Create(queueName, exchange, key, str_amqp.ExchangeFanout)
return err
}

func (s *UserService) PublishFanout() (err error) {
if s.channel == nil {
return fmt.Errorf("channel is not initialzied")
}
func (s *UserService) PublishDirect() error {
shn := s.newChannel()
err := shn.PublishDirect(exchange, queueName, mgsConnect, "info")
return err
}

err = s.channel.PublishFanout(exchange, "hello")
return
func (s *UserService) Publish() error {
shn := s.newChannel()
err := shn.Push(exchange, key, "100000", "hello")
return err
}
//
//func (s *UserService) Receive() {
// for {
// msg, ok, err := s.channel.Get(queueName, true)
//
// if !ok {
// fmt.Println("do not get msg")
// time.Sleep(3*1e9)
// continue
// }
//
// //err = s.channel.Ack(msg.DeliveryTag, false)
// log.Infof("err :%v", err)
//
// b := BytesToString(&(msg.Body))
// fmt.Printf("receve msg is :%s\n", *b)
// }
//
//
//}

func (s *UserService) ReceiveFanout() error {
if s.channel == nil {
return fmt.Errorf("channel is not initialzied")
}
func (s *UserService) PublishFanout() error {
shn := s.newChannel()
err := shn.PublishFanout(exchange, "hello")
return err
}

func (s *UserService) ReceiveFanout() error {
go func() {
for {
c, err := s.channel.ReceiveFanout("test2", exchange)
log.Infof("cha: %s, err: %v", *c, err)
c := 1
shn := s.newChannel()
defer shn.Close()
chas, err := shn.Receive(queueName)
if err != nil {
log.Infof("err: %s", err)
}
for cha := range chas {
log.Debugf("cha :%v", *amqp.BytesToString(&(cha.Body)))
cha.Ack(false)
c++
log.Debugf("cha :%v", c)
if c == 5 {
return
}
}
}()
return nil
}


func (s *UserService) ReceiveFanout3() error {
if s.channel == nil {
return fmt.Errorf("channel is not initialzied")
}

go func() {
for {
c, err := s.channel.ReceiveFanout("test1", exchange)
log.Infof("cha: %s, err: %v", *c, err)
shn := s.newChannel()
c, _ := shn.ReceiveFanout("test22222", exchange)
if c != nil {
log.Infof("cha: %s", *c)
}
time.Sleep(5 * time.Second)
}
}()
return nil
}

//func (s *UserService) ReceiveFanout1() {
// for {
// _, err := s.channel.QueueDeclare("test1", false, false,
// false, false, nil)
//
// err = s.channel.QueueBind("test1", "", exchange, false, nil)
// msg, ok, err := s.channel.Get("test1", true)
// if !ok {
// fmt.Println("do not get msg1")
// time.Sleep(3*1e9)
// continue
// }
//
// //err = s.channel.Ack(msg.DeliveryTag, false)
// log.Infof("err :%v", err)
//
// b := BytesToString(&(msg.Body))
// fmt.Printf("receve msg is1 :%s\n", *b)
// }
//
//
//}
//
//func BytesToString(b *[]byte) *string {
// s := bytes.NewBuffer(*b)
// r := s.String()
// return &r
//}
12 changes: 7 additions & 5 deletions examples/bolt/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import (
"hidevops.io/hiboot-data/examples/bolt/entity"
"hidevops.io/hiboot-data/examples/bolt/service"
"hidevops.io/hiboot/pkg/app"
"hidevops.io/hiboot/pkg/app/web"
"hidevops.io/hiboot/pkg/at"
"hidevops.io/hiboot/pkg/model"
"net/http"
)

//hi: RestController
type UserController struct {
web.Controller
at.RestController
at.RequestMapping `value:"/user"`

userService *service.UserService
}

Expand All @@ -41,15 +43,15 @@ func newUserController(userService *service.UserService) *UserController {
}

// Post /user
func (c *UserController) Post(user *entity.User) (model.Response, error) {
func (c *UserController) Post(at struct{at.PostMapping `value:"/"`}, user *entity.User) (model.Response, error) {
err := c.userService.AddUser(user)
response := new(model.BaseResponse)
response.SetData(user)
return response, err
}

// Get /user/id/{id}
func (c *UserController) GetById(id string) (model.Response, error) {
func (c *UserController) GetById(at struct{at.GetMapping `value:"/{id}"`}, id string) (model.Response, error) {
user, err := c.userService.GetUser(id)
response := new(model.BaseResponse)
if err != nil {
Expand All @@ -61,7 +63,7 @@ func (c *UserController) GetById(id string) (model.Response, error) {
}

// Delete /user/id/{id}
func (c *UserController) DeleteById(id string) (response model.Response, err error) {
func (c *UserController) DeleteById(at struct{at.DeleteMapping `value:"/{id}"`}, id string) (response model.Response, err error) {
err = c.userService.DeleteUser(id)
response = new(model.BaseResponse)
return
Expand Down
8 changes: 4 additions & 4 deletions examples/bolt/controller/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {

func TestCrdRequest(t *testing.T) {
// TODO: mock UserService
testApp := web.NewTestApplication(t, newUserController)
testApp := web.NewTestApp(newUserController).Run(t)

t.Run("should add user with POST request", func(t *testing.T) {
// First, let's Post User
Expand All @@ -39,21 +39,21 @@ func TestCrdRequest(t *testing.T) {

t.Run("should get user with GET request", func(t *testing.T) {
// Then Get User
testApp.Get("/user/id/{id}").
testApp.Get("/user/{id}").
WithPath("id", "1").
Expect().Status(http.StatusOK)
})

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}").
testApp.Get("/user/{id}").
WithPath("id", "9999").
Expect().Status(http.StatusNotFound)
})

t.Run("should delete the record with DELETE request", func(t *testing.T) {
// Finally Delete User
testApp.Delete("/user/id/{id}").
testApp.Delete("/user/{id}").
WithPath("id", "1").
Expect().Status(http.StatusOK)
})
Expand Down
4 changes: 0 additions & 4 deletions examples/gorm/config/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ server:

logging:
level: info

gorm:
# mysql-dev is the host name of mysql server, you should change it to 127.0.0.1 if you start mysql on local machine.
host: mysql-dev
5 changes: 3 additions & 2 deletions examples/gorm/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"hidevops.io/hiboot-data/examples/gorm/entity"
"hidevops.io/hiboot-data/examples/gorm/service"
"hidevops.io/hiboot/pkg/app"
"hidevops.io/hiboot/pkg/app/web"
"hidevops.io/hiboot/pkg/at"
"hidevops.io/hiboot/pkg/model"
"net/http"
)

// RestController
type userController struct {
web.Controller
at.RestController

userService service.UserService
}

Expand Down
2 changes: 1 addition & 1 deletion examples/gorm/controller/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCrdRequest(t *testing.T) {

mockUserService := new(mocks.UserService)
userController := newUserController(mockUserService)
testApp := web.NewTestApplication(t, userController)
testApp := web.NewTestApp(userController).Run(t)

id, err := idgen.Next()
assert.Equal(t, nil, err)
Expand Down
Loading

0 comments on commit 476563f

Please sign in to comment.