Skip to content

Commit

Permalink
Merge pull request #14 from ahmedbodi/transports/redis
Browse files Browse the repository at this point in the history
[Transports] Merge Redis Transport into Master
  • Loading branch information
ahmedbodi authored Jun 8, 2021
2 parents 26680ef + e891587 commit ec0405b
Show file tree
Hide file tree
Showing 12 changed files with 519 additions and 12 deletions.
14 changes: 14 additions & 0 deletions .github/pull.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "1"
rules:
- base: transports/redis
upstream: dunglas:main
mergeMethod: merge
assignees:
- ahmedbodi
reviewers:
- ahmedbodi
conflictReviewers:
- ahmedbodi
label: ":arrow_heading_down: pull"
conflictLabel: "merge-conflict"

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist
/spec/mercure.html
/spec/mercure.txt
/spec/mercure.xml
.idea/
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ linters:
- paralleltest
- cyclop
- forcetypeassert
- funlen

# deprecated
- interfacer
Expand Down
18 changes: 9 additions & 9 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ builds:
- id: legacy
dir: cmd/mercure
ldflags:
- -s -w -X github.com/dunglas/mercure/common.version={{ .Version }} -X github.com/dunglas/mercure/common.commit={{ .ShortCommit }} -X github.com/dunglas/mercure/common.buildDate={{ .Date }}
- -s -w -X github.com/ahmedbodi/mercure/common.version={{ .Version }} -X github.com/ahmedbodi/mercure/common.commit={{ .ShortCommit }} -X github.com/ahmedbodi/mercure/common.buildDate={{ .Date }}
goos:
- linux
- darwin
Expand Down Expand Up @@ -90,10 +90,10 @@ dockers:
- ids:
- caddy
image_templates:
- 'dunglas/mercure:{{ .Tag }}'
- 'dunglas/mercure:v{{ .Major }}'
- 'dunglas/mercure:v{{ .Major }}.{{ .Minor }}'
- 'dunglas/mercure:latest'
- 'ahmedbodi/mercure:{{ .Tag }}'
- 'ahmedbodi/mercure:v{{ .Major }}'
- 'ahmedbodi/mercure:v{{ .Major }}.{{ .Minor }}'
- 'ahmedbodi/mercure:latest'
extra_files:
- public/
- Caddyfile
Expand All @@ -102,10 +102,10 @@ dockers:
- legacy
dockerfile: Dockerfile.legacy
image_templates:
- 'dunglas/mercure:legacy-{{ .Tag }}'
- 'dunglas/mercure:legacy-v{{ .Major }}'
- 'dunglas/mercure:legacy-v{{ .Major }}.{{ .Minor }}'
- 'dunglas/mercure:legacy-latest'
- 'ahmedbodi/mercure:legacy-{{ .Tag }}'
- 'ahmedbodi/mercure:legacy-v{{ .Major }}'
- 'ahmedbodi/mercure:legacy-v{{ .Major }}.{{ .Minor }}'
- 'ahmedbodi/mercure:legacy-latest'
extra_files:
- public/
signs:
Expand Down
31 changes: 31 additions & 0 deletions caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type JWTConfig struct {
Alg string `json:"alg,omitempty"`
}

type NewrelicConfig struct {
Name string `json:"name,omitempty"`
License string `json:"license,omitempty"`
}

type transportDestructor struct {
transport mercure.Transport
}
Expand Down Expand Up @@ -87,6 +92,8 @@ type Mercure struct {
// Maximum cache cost, defaults to 100MB, set to -1 to disable the cache. See https://github.com/dgraph-io/ristretto for details.
CacheMaxCost *int64 `json:"cache_max_cost,omitempty"`

NewRelic NewrelicConfig `json:"newrelic,omitempty"`

hub *mercure.Hub
logger *zap.Logger
}
Expand All @@ -110,6 +117,11 @@ func (m *Mercure) Provision(ctx caddy.Context) error { //nolint:funlen
if m.PublisherJWT.Key == "" {
return errors.New("a JWT key for publishers must be provided") //nolint:goerr113
}

if m.NewRelic.License != "" && m.NewRelic.Name == "" {
return errors.New("the NewRelic application name must be specified") //nolint:goerr113
}

if m.PublisherJWT.Alg == "" {
m.PublisherJWT.Alg = "HS256"
}
Expand Down Expand Up @@ -208,6 +220,11 @@ func (m *Mercure) Provision(ctx caddy.Context) error { //nolint:funlen
opts = append(opts, mercure.WithCORSOrigins(m.CORSOrigins))
}

if len(m.NewRelic.License) > 0 {
opts = append(opts, mercure.WithNewRelic(m.NewRelic.Name, m.NewRelic.License))
}


h, err := mercure.NewHub(opts...)
if err != nil {
return err //nolint:wrapcheck
Expand Down Expand Up @@ -354,6 +371,20 @@ func (m *Mercure) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { //nolint:fu
}

m.CacheMaxCost = &v

case "newrelic_name":
if !d.NextArg() {
return d.ArgErr()
}

m.NewRelic.Name = d.Val()

case "newrelic_license":
if !d.NextArg() {
return d.ArgErr()
}

m.NewRelic.License = d.Val()
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions caddy/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/go-piv/piv-go v1.5.0/go.mod h1:ON2WvQncm7dIkCQ7kYJs+nc3V4jHGfrrJnSF8HKy7Gk=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
Expand Down Expand Up @@ -545,6 +547,8 @@ github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96d
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
github.com/newrelic/go-agent v2.15.0+incompatible/go.mod h1:a8Fv1b/fYhFSReoTU6HDkTYIMZeSVNffmoS726Y0LzQ=
github.com/newrelic/go-agent v3.12.0+incompatible h1:XJeIroXVzIfxc8lX9Abl2HCIhDeNLqBepBvVhAaFyfo=
github.com/newrelic/go-agent v3.12.0+incompatible/go.mod h1:a8Fv1b/fYhFSReoTU6HDkTYIMZeSVNffmoS726Y0LzQ=
github.com/ngdinhtoan/glide-cleanup v0.2.0/go.mod h1:UQzsmiDOb8YV3nOsCxK/c9zPpCZVNoHScRE3EO9pVMM=
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
Expand All @@ -559,6 +563,7 @@ github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA=
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dunglas/jwt-go v3.2.1-0.20210401222640-f048a7c297bf+incompatible
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-redis/redis v6.15.9+incompatible
github.com/gofrs/uuid v4.0.0+incompatible
github.com/golang/protobuf v1.5.2 // indirect
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
Expand All @@ -16,6 +17,9 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/newrelic/go-agent v3.12.0+incompatible
github.com/onsi/ginkgo v1.10.3 // indirect
github.com/onsi/gomega v1.7.1 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/prometheus/client_golang v1.10.0
github.com/prometheus/client_model v0.2.0
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgO
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
Expand Down Expand Up @@ -188,6 +190,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand Down Expand Up @@ -254,13 +257,19 @@ github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzE
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/newrelic/go-agent v3.12.0+incompatible h1:XJeIroXVzIfxc8lX9Abl2HCIhDeNLqBepBvVhAaFyfo=
github.com/newrelic/go-agent v3.12.0+incompatible/go.mod h1:a8Fv1b/fYhFSReoTU6HDkTYIMZeSVNffmoS726Y0LzQ=
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.3 h1:OoxbjfXVZyod1fmWYhI7SEyaD8B00ynP3T+D5GiyHOY=
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis=
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
Expand Down Expand Up @@ -606,12 +615,14 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
Expand Down
13 changes: 10 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
newrelic "github.com/newrelic/go-agent"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -36,9 +37,15 @@ func (h *Hub) initHandler() {

h.registerSubscriptionHandlers(router)

router.HandleFunc(defaultHubURL, h.SubscribeHandler).Methods("GET", "HEAD")
router.HandleFunc(defaultHubURL, h.PublishHandler).Methods("POST")

if h.newrelicApp != nil {
h.logger.Info("Wrapping HTTP Handlers with NewRelic")
router.HandleFunc(newrelic.WrapHandleFunc(h.newrelicApp, defaultHubURL, h.SubscribeHandler)).Methods("GET", "HEAD")
router.HandleFunc(newrelic.WrapHandleFunc(h.newrelicApp, defaultHubURL, h.PublishHandler)).Methods("POST")
} else {
h.logger.Info("Not Wrapping HTTP Handlers with NewRelic")
router.HandleFunc(defaultHubURL, h.SubscribeHandler).Methods("GET", "HEAD")
router.HandleFunc(defaultHubURL, h.PublishHandler).Methods("POST")
}
secureMiddleware := secure.New(secure.Options{
IsDevelopment: h.debug,
AllowedHosts: h.allowedHosts,
Expand Down
18 changes: 18 additions & 0 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package mercure

import (
"fmt"
newrelic "github.com/newrelic/go-agent"
"net/http"
"time"

Expand Down Expand Up @@ -179,6 +180,22 @@ func WithTopicSelectorStore(tss *TopicSelectorStore) Option {
}
}

func WithNewRelic(name string, license string) Option {
return func(o *opt) error {
config := newrelic.NewConfig(name, license)
config.Enabled = true
config.BrowserMonitoring.Enabled = true
config.DistributedTracer.Enabled = true
nrApp, err := newrelic.NewApplication(config)
if err != nil {
return fmt.Errorf("error setting up newrelic: %w", err)
}

o.newrelicApp = nrApp
return nil
}
}

type jwtConfig struct {
key []byte
signingMethod jwt.SigningMethod
Expand All @@ -204,6 +221,7 @@ type opt struct {
allowedHosts []string
publishOrigins []string
corsOrigins []string
newrelicApp newrelic.Application
}

// Hub stores channels with clients currently subscribed and allows to dispatch updates.
Expand Down
Loading

0 comments on commit ec0405b

Please sign in to comment.