Skip to content

Commit

Permalink
Merge pull request #1 from micro/move-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
crufter authored Oct 2, 2020
2 parents 61fe9c1 + 9f7e30e commit 6f037a2
Show file tree
Hide file tree
Showing 31 changed files with 4,482 additions and 2 deletions.
41 changes: 41 additions & 0 deletions blob-store/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"bytes"
"io/ioutil"

"github.com/google/uuid"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/store"
)

func main() {
service.New(
service.Name("blob-store"),
service.Version("latest"),
)

key := uuid.New().String()

buf := bytes.NewBuffer([]byte("world"))
if err := store.DefaultBlobStore.Write(key, buf); err != nil {
logger.Fatalf("Error writing to blob store: %v", err)
} else {
logger.Infof("Write okay")
}

res, err := store.DefaultBlobStore.Read(key)
if err != nil {
logger.Fatalf("Error reading from the blog store: %v", err)
}
bytes, err := ioutil.ReadAll(res)
if err != nil {
logger.Fatalf("Error reading result: %v", err)
}
logger.Infof("Read from blob store: %v", string(bytes))

if err := store.DefaultBlobStore.Delete(key); err != nil {
logger.Fatalf("Error deleting from blob store: %v", err)
}
}
59 changes: 59 additions & 0 deletions config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"fmt"
"time"

"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/config"
)

type keyConfig struct {
Subkey string `json:"subkey"`
Subkey1 int `json:"subkey1"`
Subkey2 string `json:"subkey2"`
}

type conf struct {
Key keyConfig `json:"key"`
}

func main() {
// get a value
go func() {
for {
time.Sleep(time.Second)
// Test merge
err := config.Set("key", map[string]interface{}{
"Subkey3": "Merge",
})
if err != nil {
fmt.Println("ERROR: ", err)
}

val, _ := config.Get("key.subkey3")
if val.String("") != "Merge" {
fmt.Println("ERROR: key.subkey3 should be 'Merge' but it is:", val.String(""))
}

val, err = config.Get("key.subkey")
fmt.Println("Value of key.subkey: ", val.String(""), err)

val, _ = config.Get("key", config.Secret(true))
c := conf{}
err = val.Scan(&c.Key)
fmt.Println("Value of key.subkey1: ", c.Key.Subkey1, err)
fmt.Println("Value of key.subkey2: ", c.Key.Subkey2)

val, _ = config.Get("key.subkey3")
fmt.Println("Value of key.subkey3: ", val.String(""))

// Test defaults
val, _ = config.Get("key.subkey_does_not_exist")
fmt.Println("Default", val.String("Hello"))
}
}()

// run the service
service.Run()
}
154 changes: 154 additions & 0 deletions events/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package main

import (
"sync"
"time"

goevents "github.com/micro/go-micro/v3/events"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/events"
"github.com/micro/micro/v3/service/logger"
)

type testPayload struct {
Message string
}

var (
payload = testPayload{Message: "HelloWorld"}
metadata = map[string]string{"foo": "bar"}
)

func main() {
srv := service.New()
srv.Init()

go func() { // test 1, ordinary sub with autoack
evChan, err := events.Subscribe("test1")
if err != nil {
logger.Fatalf("Error creating subscriber: %v", err)
}
// Is there a race condition here with publish coming straight after subscribe? Seems to be according to this
// test so sleep for a bit to wait for nats to register subscription properly
time.Sleep(2 * time.Second)

logger.Infof("TEST1: publishing event")

err = events.Publish("test1", payload, goevents.WithMetadata(metadata))
if err != nil {
logger.Errorf("TEST1: Error publishing event: %v", err)
return
}

logger.Infof("TEST1: waiting for event")
event, ok := <-evChan
if !ok {
logger.Error("TEST1: Channel closed")
return
}

if event.Topic != "test1" {
logger.Errorf("TEST1: Incorrect topic: %v", event.Topic)
return
}
if event.Metadata == nil || event.Metadata["foo"] != metadata["foo"] {
logger.Errorf("TEST1: Incorrect metadata: %v", event.Metadata)
return
}

var result testPayload
if err := event.Unmarshal(&result); err != nil {
logger.Errorf("TEST1: Error decoding result: %v. Payload was %v", err, string(event.Payload))
return
}
if result.Message != payload.Message {
logger.Errorf("TEST1: Incorrect message: %v", result.Message)
return
}

logger.Infof("TEST1: Finished ok")

}()

go func() { // test 2, sub with manual ack
evChan, err := events.Subscribe("test2", goevents.WithAutoAck(false, 5*time.Second))
if err != nil {
logger.Errorf("TEST2: Error creating subscriber: %v", err)
return
}

// Is there a race condition here with publish coming straight after subscribe? Seems to be according to this
// test so sleep for a bit to wait for nats to register subscription properly
time.Sleep(2 * time.Second)

logger.Infof("TEST2: publishing event")
err = events.Publish("test2", payload, goevents.WithMetadata(metadata))

if err != nil {
logger.Errorf("TEST2: Error publishing event: %v", err)
return
}

logger.Infof("TEST2: waiting for event")
event, ok := <-evChan
if !ok {
logger.Errorf("TEST2: Channel closed")
return
}

if event.Topic != "test2" {
logger.Errorf("TEST2: Incorrect topic: %v", event.Topic)
return
}
if event.Metadata == nil || event.Metadata["foo"] != metadata["foo"] {
logger.Errorf("TEST2: Incorrect metadata: %v", event.Metadata)
return
}

var result testPayload
if err := event.Unmarshal(&result); err != nil {
logger.Errorf("TEST2: Error decoding result: %v. Payload was %v", err, string(event.Payload))
return
}
if result.Message != payload.Message {
logger.Errorf("TEST2: Incorrect message: %v", result.Message)
return
}
id := event.ID
// nack the event to put it back on the queue
logger.Infof("TEST2: Nacking the event")
event.Nack()

logger.Infof("TEST2: waiting for event")
select {
case event := <-evChan:
if event.ID != id {
logger.Errorf("Unexpected event received, expected %s, received %s", id, event.ID)
return
}
logger.Infof("TEST2: Acking the event")
event.Ack()

case <-time.After(10 * time.Second):
logger.Errorf("Timed out waiting for event")
return
}

// we've acked so should receive nothing else
logger.Infof("TEST2: Waiting for timeout")
select {
case event := <-evChan:
logger.Errorf("Unexpected event received %s", event.ID)
return
case <-time.After(10 * time.Second):

}

logger.Infof("TEST2: Finished ok")

}()
wg := sync.WaitGroup{}
wg.Add(1)
// wait indefinitely so this only runs once
wg.Wait()
}
35 changes: 33 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,42 @@ module github.com/micro/services
go 1.14

require (
github.com/caddyserver/certmagic v0.12.0 // indirect
github.com/cenkalti/backoff/v4 v4.0.2 // indirect
github.com/cloudflare/cloudflare-go v0.13.3 // indirect
github.com/evanphx/json-patch/v5 v5.1.0 // indirect
github.com/go-acme/lego/v3 v3.9.0 // indirect
github.com/gobwas/httphead v0.0.0-20200921212729-da3d93bc3c58 // indirect
github.com/gobwas/ws v1.0.4 // indirect
github.com/golang/protobuf v1.4.2
github.com/google/uuid v1.1.2
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gosimple/slug v1.9.0
github.com/micro/go-micro/v3 v3.0.0-beta.2.0.20200918112555-9168c7c61064
github.com/micro/micro/v3 v3.0.0-beta.4.0.20200918115538-32e9a17127d7
github.com/hashicorp/go-retryablehttp v0.6.7 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/micro/go-micro v1.18.0 // indirect
github.com/micro/go-micro/v3 v3.0.0-beta.3
github.com/micro/go-plugins/broker/nats/v3 v3.0.0-20200908121001-4ea6f6760baf // indirect
github.com/micro/go-plugins/events/stream/nats/v3 v3.0.0-20200908121001-4ea6f6760baf // indirect
github.com/micro/go-plugins/metrics/prometheus/v3 v3.0.0-20200908121001-4ea6f6760baf // indirect
github.com/micro/go-plugins/registry/etcd/v3 v3.0.0-20200908121001-4ea6f6760baf // indirect
github.com/micro/go-plugins/store/cockroach/v3 v3.0.0-20200908121001-4ea6f6760baf // indirect
github.com/micro/micro/v3 v3.0.0-beta.5.1
github.com/miekg/dns v1.1.31 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
github.com/xanzy/go-gitlab v0.38.1 // indirect
github.com/xlab/treeprint v1.0.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20201002094018-c90954cbb977 // indirect
golang.org/x/net v0.0.0-20200930145003-4acb6c075d10 // indirect
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 // indirect
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f // indirect
google.golang.org/genproto v0.0.0-20201001141541-efaab9d3c4f7 // indirect
google.golang.org/grpc v1.32.0
google.golang.org/protobuf v1.25.0
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
Loading

0 comments on commit 6f037a2

Please sign in to comment.