Skip to content

Commit

Permalink
Merge pull request #2 from go-micro/dev
Browse files Browse the repository at this point in the history
feat: add v3 plugins
  • Loading branch information
xpunch authored Apr 16, 2022
2 parents 06bb53c + 0f6a645 commit d9a7a67
Show file tree
Hide file tree
Showing 684 changed files with 137,851 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fmt:
@sh scripts/fmt.sh

mod:
@sh scripts/deps.sh

test:
@sh scripts/tests.sh
7 changes: 7 additions & 0 deletions scripts/fmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash -ex

for d in $(find * -name 'go.mod'); do
pushd $(dirname $d)
go fmt
popd
done
23 changes: 17 additions & 6 deletions scripts/tests.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
#!/bin/bash -e

for d in $(find * -name 'go.mod'); do
for d in $(find 'v3' -name 'go.mod'); do
case $(dirname $d) in
'v4/config/source/configmap'|'v4/config/source/vault'|'v4/events/redis'|'v4/logger/windowseventlog'|'v4/store/mysql'|'v4/store/redis'|'v4/sync/consul'|'v4/sync/etcd')
echo skip $(dirname $d)
'v3/config/source/configmap'|'v3/config/source/vault'|'v3/events/redis'|'v3/logger/apex'|'v3/logger/windowseventlog'|'v3/store/mysql'|'v3/store/redis'|'v3/store/memory'|'v3/store/file'|'v3/sync/consul'|'v3/sync/etcd')
echo SKIP $(dirname $d)
;;
*)
pushd $(dirname $d) >/dev/null
go get
go vet
#go test -race -v
go test -v
go test
popd >/dev/null
esac
done

for d in $(find 'v4' -name 'go.mod'); do
case $(dirname $d) in
'v4/config/source/configmap'|'v4/config/source/vault'|'v4/events/redis'|'v4/events/natsjs'|'v4/logger/windowseventlog'|'v4/store/mysql'|'v4/store/redis'|'v4/sync/consul'|'v4/sync/etcd')
echo SKIP $(dirname $d)
;;
*)
pushd $(dirname $d) >/dev/null
go vet
go test # -race
popd >/dev/null
esac
done
147 changes: 147 additions & 0 deletions v3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
# Plugins [![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/github.com/go-micro/plugins/v3?status.svg)](https://godoc.org/github.com/go-micro/plugins/v3)

Go plugins is a place for community maintained plugins.

## Overview

Micro tooling is built on a powerful pluggable architecture. Plugins can be swapped out with zero code changes.
This repository contains plugins for all micro related tools. Read on for further info.

## Getting Started

* [Contents](#contents)
* [Usage](#usage)
* [Build Pattern](#build-pattern)
* [Contributions](#contributions)

## Contents

Contents of this repository:

| Directory | Description |
| --------- | ----------------------------------------------------------------|
| Broker | PubSub messaging; NATS, NSQ, RabbitMQ, Kafka |
| Client | RPC Clients; gRPC, HTTP |
| Codec | Message Encoding; BSON, Mercury |
| Micro | Micro Toolkit Plugins |
| Registry | Service Discovery; Etcd, Gossip, NATS |
| Selector | Load balancing; Label, Cache, Static |
| Server | RPC Servers; gRPC, HTTP |
| Transport | Bidirectional Streaming; NATS, RabbitMQ |
| Wrapper | Middleware; Circuit Breakers, Rate Limiting, Tracing, Monitoring|

## Usage

Plugins can be added to go-micro in the following ways. By doing so they'll be available to set via command line args or environment variables.

Import the plugins in a `plugins.go` file

```go
package main

import (
_ "github.com/go-micro/plugins/v3/broker/rabbitmq"
_ "github.com/go-micro/plugins/v3/registry/kubernetes"
_ "github.com/go-micro/plugins/v3/transport/nats"
)
```

Create your service and ensure you call `service.Init`

```go
package main

import (
"github.com/asim/go-micro/v3"
)

func main() {
service := micro.NewService(
// Set service name
micro.Name("my.service"),
)

// Parse CLI flags
service.Init()
}
```

Build your service

```
go build -o service ./main.go ./plugins.go
```

### Env

Use environment variables to set the

```
MICRO_BROKER=rabbitmq \
MICRO_REGISTRY=kubernetes \
MICRO_TRANSPORT=nats \
./service
```

### Flags

Or use command line flags to enable them

```shell
./service --broker=rabbitmq --registry=kubernetes --transport=nats
```

### Options

Import and set as options when creating a new service

```go
import (
"github.com/asim/go-micro/v3"
"github.com/go-micro/plugins/v3/registry/kubernetes"
)

func main() {
registry := kubernetes.NewRegistry() //a default to using env vars for master API

service := micro.NewService(
// Set service name
micro.Name("my.service"),
// Set service registry
micro.Registry(registry),
)
}
```

## Build

An anti-pattern is modifying the `main.go` file to include plugins. Best practice recommendation is to include
plugins in a separate file and rebuild with it included. This allows for automation of building plugins and
clean separation of concerns.

Create file plugins.go

```go
package main

import (
_ "github.com/go-micro/plugins/v3/broker/rabbitmq"
_ "github.com/go-micro/plugins/v3/registry/kubernetes"
_ "github.com/go-micro/plugins/v3/transport/nats"
)
```

Build with plugins.go

```shell
go build -o service main.go plugins.go
```

Run with plugins

```shell
MICRO_BROKER=rabbitmq \
MICRO_REGISTRY=kubernetes \
MICRO_TRANSPORT=nats \
service
```
68 changes: 68 additions & 0 deletions v3/acme/certmagic/certmagic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Package certmagic is the ACME provider from github.com/caddyserver/certmagic
package certmagic

import (
"crypto/tls"
"math/rand"
"net"
"time"

"github.com/asim/go-micro/v3/api/server/acme"
"github.com/asim/go-micro/v3/logger"
"github.com/caddyserver/certmagic"
)

type certmagicProvider struct {
opts acme.Options
}

// TODO: set self-contained options
func (c *certmagicProvider) setup() {
certmagic.DefaultACME.CA = c.opts.CA
if c.opts.ChallengeProvider != nil {
// Enabling DNS Challenge disables the other challenges
certmagic.DefaultACME.DNSProvider = c.opts.ChallengeProvider
}
if c.opts.OnDemand {
certmagic.Default.OnDemand = new(certmagic.OnDemandConfig)
}
if c.opts.Cache != nil {
// already validated by new()
certmagic.Default.Storage = c.opts.Cache.(certmagic.Storage)
}
// If multiple instances of the provider are running, inject some
// randomness so they don't collide
// RenewalWindowRatio [0.33 - 0.50)
rand.Seed(time.Now().UnixNano())
randomRatio := float64(rand.Intn(17)+33) * 0.01
certmagic.Default.RenewalWindowRatio = randomRatio
}

func (c *certmagicProvider) Listen(hosts ...string) (net.Listener, error) {
c.setup()
return certmagic.Listen(hosts)
}

func (c *certmagicProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
c.setup()
return certmagic.TLS(hosts)
}

// NewProvider returns a certmagic provider
func NewProvider(options ...acme.Option) acme.Provider {
opts := acme.DefaultOptions()

for _, o := range options {
o(&opts)
}

if opts.Cache != nil {
if _, ok := opts.Cache.(certmagic.Storage); !ok {
logger.Fatal("ACME: cache provided doesn't implement certmagic's Storage interface")
}
}

return &certmagicProvider{
opts: opts,
}
}
9 changes: 9 additions & 0 deletions v3/acme/certmagic/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/go-micro/plugins/v3/acme/certmagic

go 1.16

require (
github.com/asim/go-micro/v3 v3.7.1
github.com/caddyserver/certmagic v0.11.2
github.com/micro/cli/v2 v2.1.2 // indirect
)
Loading

0 comments on commit d9a7a67

Please sign in to comment.