Skip to content

Commit

Permalink
publish new blog posts into a message queue
Browse files Browse the repository at this point in the history
  • Loading branch information
quantonganh committed Mar 18, 2024
1 parent 6f5dabe commit 4b717a6
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 35 deletions.
19 changes: 0 additions & 19 deletions client/newsletter.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,3 @@ func (n *Newsletter) Unsubscribe(r *http.Request, email, hash string) (*http.Res

return resp, nil
}

func (n *Newsletter) Send(r *http.Request, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/newsletter/send", n.baseURL), body)
if err != nil {
return nil, err
}

requestID, ok := hlog.IDFromRequest(r)
if ok {
req.Header.Set("Request-Id", requestID.String())
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Config struct {
Sentry struct {
DSN string
}

AMQP struct {
URL string
}
}

// Item represents a navbar item
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/rs/zerolog v1.23.0
github.com/russross/blackfriday/v2 v2.1.0
github.com/spf13/viper v1.3.2
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.17.0
golang.org/x/sync v0.1.0
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -50,6 +50,7 @@ require (
github.com/pelletier/go-toml v1.7.0 // indirect
github.com/philhofer/fwd v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rabbitmq/amqp091-go v1.9.0
github.com/rs/xid v1.2.1 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
Expand Down
10 changes: 9 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/quantonganh/httperror v0.0.5 h1:S2NuQxzQyQbLxcXiOY/6Att9sT+yXoXieJmAWJNt0b4=
github.com/quantonganh/httperror v0.0.5/go.mod h1:brtwPDwG4J80Xzva7ibcdKg/VnLubpKSPYbeECMFC00=
github.com/rabbitmq/amqp091-go v1.9.0 h1:qrQtyzB4H8BQgEuJwhmVQqVHB9O4+MNDJCCAcpc3Aoo=
github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc=
github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
Expand Down Expand Up @@ -316,11 +318,14 @@ github.com/steveyen/gtreap v0.1.0 h1:CjhzTa274PyJLJuMZwIzCO1PfC00oRa8d1Kc78bFXJM
github.com/steveyen/gtreap v0.1.0/go.mod h1:kl/5J7XbrOmlIbYIXdRHDDE5QxHqpk0cmkT7Z4dM9/Y=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/syndtr/goleveldb v0.0.0-20160425020131-cfa635847112/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
Expand Down Expand Up @@ -355,6 +360,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/gopher-lua v0.0.0-20171031051903-609c9cd26973/go.mod h1:aEV29XrmTYFr3CiRxZeGHpkvbwq+prZduBqMaascyCU=
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -439,6 +446,7 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
29 changes: 19 additions & 10 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/quantonganh/blog"
"github.com/quantonganh/blog/client"
"github.com/quantonganh/blog/markdown"
"github.com/quantonganh/blog/rabbitmq"
"github.com/quantonganh/blog/ui"
"github.com/quantonganh/httperror"
)
Expand All @@ -37,10 +38,11 @@ type Server struct {
Addr string
Domain string

PostService blog.PostService
SearchService blog.SearchService
Renderer blog.Renderer
NewsletterService blog.NewsletterService
PostService blog.PostService
SearchService blog.SearchService
Renderer blog.Renderer
NewsletterService blog.NewsletterService
MessageQueueService blog.MessageQueueService
}

// NewServer create new HTTP server
Expand All @@ -51,13 +53,20 @@ func NewServer(config *blog.Config, posts []*blog.Post) (*Server, error) {
if err != nil {
return nil, err
}

mqService, err := rabbitmq.NewMessageQueueService(config.AMQP.URL)
if err != nil {
return nil, err
}

s := &Server{
server: &http.Server{},
router: mux.NewRouter().StrictSlash(true),
PostService: postService,
SearchService: searchService,
Renderer: NewRender(config, postService),
NewsletterService: client.NewNewsletter(config.Newsletter.BaseURL),
server: &http.Server{},
router: mux.NewRouter().StrictSlash(true),
PostService: postService,
SearchService: searchService,
Renderer: NewRender(config, postService),
NewsletterService: client.NewNewsletter(config.Newsletter.BaseURL),
MessageQueueService: mqService,
}

zlog := zerolog.New(os.Stdout).With().
Expand Down
3 changes: 3 additions & 0 deletions http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ templates:
newsletter:
hmac:
secret: da02e221bc331c9875c5e1299fa8d765
amqp:
url: amqp://guest:guest@localhost:5672
`)
if err := viper.ReadConfig(bytes.NewBuffer(yamlConfig)); err != nil {
log.Fatal(err)
Expand Down
4 changes: 1 addition & 3 deletions http/webhook.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package http

import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
Expand Down Expand Up @@ -78,8 +77,7 @@ func (s *Server) webhookHandler(config *blog.Config) appHandler {
return err
}

_, err = s.NewsletterService.Send(r, bytes.NewBuffer(data))
if err != nil {
if err := s.MessageQueueService.Publish("added-posts", data); err != nil {
return err
}
}
Expand Down
5 changes: 5 additions & 0 deletions message_queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package blog

type MessageQueueService interface {
Publish(topic string, message []byte) error
}
1 change: 0 additions & 1 deletion newsletter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ type NewsletterService interface {
Subscribe(r *http.Request, body io.Reader) (*http.Response, error)
Confirm(r *http.Request, token string) (*http.Response, error)
Unsubscribe(r *http.Request, email, hash string) (*http.Response, error)
Send(r *http.Request, body io.Reader) (*http.Response, error)
}
60 changes: 60 additions & 0 deletions rabbitmq/message_queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package rabbitmq

import (
"context"
"time"

amqp "github.com/rabbitmq/amqp091-go"
)

type MessageQueueService struct {
ch *amqp.Channel
}

func NewMessageQueueService(url string) (*MessageQueueService, error) {
conn, err := amqp.Dial(url)
if err != nil {
return nil, err
}

ch, err := conn.Channel()
if err != nil {
return nil, err
}

return &MessageQueueService{
ch: ch,
}, nil
}

func (s *MessageQueueService) Publish(topic string, message []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

q, err := s.ch.QueueDeclare(
topic,
false,
false,
false,
false,
nil,
)
if err != nil {
return err
}

if err := s.ch.PublishWithContext(ctx,
"",
q.Name,
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: message,
},
); err != nil {
return err
}

return nil
}

0 comments on commit 4b717a6

Please sign in to comment.