Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

adds a couple of new constructors for Message, including New(*buffalo.App). this fixes #1296 #1338

Merged
merged 6 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type App struct {
ErrorHandlers ErrorHandlers `json:"-"`
ErrorMiddleware MiddlewareFunc `json:"-"`
router *mux.Router
moot *sync.Mutex
moot *sync.RWMutex
routes RouteList
root *App
children []*App
Expand All @@ -40,7 +40,7 @@ func New(opts Options) *App {
500: defaultErrorHandler,
},
router: mux.NewRouter(),
moot: &sync.Mutex{},
moot: &sync.RWMutex{},
routes: RouteList{},
children: []*App{},
}
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ require (
github.com/gobuffalo/buffalo-pop v0.0.0-20180927180522-49c868b84258
github.com/gobuffalo/envy v1.6.4
github.com/gobuffalo/events v1.0.2
github.com/gobuffalo/genny v0.0.0-20180928194301-41d045c55d80
github.com/gobuffalo/genny v0.0.0-20180928214617-9033ae798a5a
github.com/gobuffalo/github_flavored_markdown v1.0.5
github.com/gobuffalo/httptest v1.0.1
github.com/gobuffalo/makr v1.1.5
github.com/gobuffalo/mw-basicauth v0.0.0-20180802152105-15d67c4ac152
github.com/gobuffalo/mw-basicauth v0.0.0-20180928200421-abc8ee16c5a2
github.com/gobuffalo/mw-contenttype v0.0.0-20180802152300-74f5a47f4d56
github.com/gobuffalo/mw-csrf v0.0.0-20180802151833-446ff26e108b
github.com/gobuffalo/mw-forcessl v0.0.0-20180802152810-73921ae7a130
Expand All @@ -24,15 +24,15 @@ require (
github.com/gobuffalo/plush v3.7.16+incompatible
github.com/gobuffalo/pop v4.7.3+incompatible
github.com/gobuffalo/tags v2.0.11+incompatible
github.com/gobuffalo/x v0.0.0-20180924043718-b9bd64714615
github.com/gobuffalo/x v0.0.0-20180930144534-b01151fcafd9
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.6.2
github.com/gorilla/sessions v1.1.2
github.com/markbates/deplist v1.0.3
github.com/markbates/grift v1.0.4
github.com/markbates/inflect v1.0.0
github.com/markbates/oncer v0.0.0-20180924034138-723ad0170a46
github.com/markbates/refresh v1.4.8
github.com/markbates/refresh v1.4.9
github.com/markbates/sigtx v1.0.0
github.com/monoculum/formam v0.0.0-20180901015400-4e68be1d79ba
github.com/pkg/errors v0.8.0
Expand Down
18 changes: 8 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ github.com/gobuffalo/envy v1.6.4 h1:kxamN+VYjPMzEdjc2mn4CIKiuYXGxc8VIwXJNixFlNY=
github.com/gobuffalo/envy v1.6.4/go.mod h1:Abh+Jfw475/NWtYMEt+hnJWRiC8INKWibIMyNt1w2Mc=
github.com/gobuffalo/events v0.0.0-20180919212028-867dc2348bad/go.mod h1:llrZYJ6ap/G7OwAEIbGJzyKlzWJy/F84lPgilFIBpH4=
github.com/gobuffalo/events v0.0.0-20180922214205-bcd0f5dd556a/go.mod h1:1y8L9VCEC17GFRd98juWpWqIX7WXvDDJ3W1qNy02iHg=
github.com/gobuffalo/events v0.0.0-20180924042527-b0372953254d/go.mod h1:rSHVdTfkpJgtv4ZaMihvdxA/3dw6Rzfse75/ewrm08M=
github.com/gobuffalo/events v1.0.2 h1:9sokzhPipEL6mN+KHxs6dNSE+WcPa+vNZSC9qTGnBbc=
github.com/gobuffalo/events v1.0.2/go.mod h1:i+LaFjejdNAXYYy/wjTf9FD/9I2cnxI+38YxBaSsNX0=
github.com/gobuffalo/fizz v1.0.7/go.mod h1:fbtmvB0dcsGJUxM/S8biqkQtvykqPQGdkcg94zVu8GA=
Expand All @@ -55,11 +54,11 @@ github.com/gobuffalo/genny v0.0.0-20180921151430-2d326bbbbcb1/go.mod h1:8wmnQwAS
github.com/gobuffalo/genny v0.0.0-20180922164512-230e627635f7/go.mod h1:8wmnQwASeX0/m66Sd6qisbIBn9MGoxRbBe0DNEUVLZs=
github.com/gobuffalo/genny v0.0.0-20180924032338-7af3a40f2252/go.mod h1:tUTQOogrr7tAQnhajMSH6rv1BVev34H2sa1xNHMy94g=
github.com/gobuffalo/genny v0.0.0-20180924035723-a6a364609caf/go.mod h1:+NiDK371DHISWnXZWaBKoJ1ZPHLprDnSXt6NpiV9JqE=
github.com/gobuffalo/genny v0.0.0-20180924040918-fce452ea81be h1:JesZnM2NvQ6RCkiEszDy2jQdQ4xpDNmJ2lPcWdUVVwg=
github.com/gobuffalo/genny v0.0.0-20180924040918-fce452ea81be/go.mod h1:jm0W3oe9WQnNUq1DY5w1Ow9SvXc5y9joYuWYyRLmHoA=
github.com/gobuffalo/genny v0.0.0-20180926172232-22498a7facf1/go.mod h1:8WCqtWF1jL89MyXYbglxvCY4fdM+x3wlqSOUwtJi3ik=
github.com/gobuffalo/genny v0.0.0-20180928194301-41d045c55d80 h1:s8FiYdcmEB1johvlabKtHD21rVQ+sPRwgFwl9c9qcFQ=
github.com/gobuffalo/genny v0.0.0-20180928194301-41d045c55d80/go.mod h1:8WCqtWF1jL89MyXYbglxvCY4fdM+x3wlqSOUwtJi3ik=
github.com/gobuffalo/genny v0.0.0-20180928214617-9033ae798a5a h1:X6D4R7FaY3sbORd8FRvcdWqR+54gGLMIDQy9JUfdXVo=
github.com/gobuffalo/genny v0.0.0-20180928214617-9033ae798a5a/go.mod h1:8WCqtWF1jL89MyXYbglxvCY4fdM+x3wlqSOUwtJi3ik=
github.com/gobuffalo/github_flavored_markdown v1.0.0/go.mod h1:c8/8gRnd6MSyyk+fp6E8O8cUTHd7P2cnDnH4G7o91l0=
github.com/gobuffalo/github_flavored_markdown v1.0.4/go.mod h1:uRowCdK+q8d/RF0Kt3/DSalaIXbb0De/dmTqMQdkQ4I=
github.com/gobuffalo/github_flavored_markdown v1.0.5 h1:YvGVf7yj1akgsb+qc64Q0WX8uhpuZSibChbqOMRSAqE=
Expand All @@ -73,11 +72,10 @@ github.com/gobuffalo/makr v1.1.2/go.mod h1:5/uvRIyR+DQHJrX0UPCPF0WhQGMJG5/xd9XT9
github.com/gobuffalo/makr v1.1.5 h1:lOlpv2iz0dNa4qse0ZYQgbtT+ybwVxWEAcOZbcPmeYc=
github.com/gobuffalo/makr v1.1.5/go.mod h1:Y+o0btAH1kYAMDJW/TX3+oAXEu0bmSLLoC9mIFxtzOw=
github.com/gobuffalo/mapi v0.0.0-20180919210455-5485bb0900f5/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc=
github.com/gobuffalo/mapi v0.0.0-20180924042459-c7d85897722b/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc=
github.com/gobuffalo/mapi v1.0.0 h1:9zYDVEhaNO69oSBNGErd4q+Qo6ogMumkvC7nYgB3Dz4=
github.com/gobuffalo/mapi v1.0.0/go.mod h1:4VAGh89y6rVOvm5A8fKFxYG+wIW6LO1FMTG9hnKStFc=
github.com/gobuffalo/mw-basicauth v0.0.0-20180802152105-15d67c4ac152 h1:gxsF8w9epZSG/LrhrIfBDrqxDwjNPv0J1hkGVCmDExo=
github.com/gobuffalo/mw-basicauth v0.0.0-20180802152105-15d67c4ac152/go.mod h1:+vSUajk1TCB7xvyLuDIzhIK+XKqn0St6xLK/CxxpUU4=
github.com/gobuffalo/mw-basicauth v0.0.0-20180928200421-abc8ee16c5a2 h1:0gKlD66bGLL19wbLZ3X7TKUoqZmZDWdEisGT6FoD814=
github.com/gobuffalo/mw-basicauth v0.0.0-20180928200421-abc8ee16c5a2/go.mod h1:162JuI+Qn+/ZSK6bgyIBCLUyy1UV1+qD1IvDEh0sJNo=
github.com/gobuffalo/mw-contenttype v0.0.0-20180802152300-74f5a47f4d56 h1:SUFp8EbFjlKXkvqstoxPWx3nVPV3BSKZTswQNTZFaik=
github.com/gobuffalo/mw-contenttype v0.0.0-20180802152300-74f5a47f4d56/go.mod h1:7EvcmzBbeCvFtQm5GqF9ys6QnCxz2UM1x0moiWLq1No=
github.com/gobuffalo/mw-csrf v0.0.0-20180802151833-446ff26e108b h1:A13B4mhcFQcjPJ1GFBrh61B4Qo87fZa82FfTt9LX/QU=
Expand Down Expand Up @@ -123,8 +121,8 @@ github.com/gobuffalo/validate v2.0.3+incompatible h1:6f4JCEz11Zi6iIlexMv7Jz10RBP
github.com/gobuffalo/validate v2.0.3+incompatible/go.mod h1:N+EtDe0J8252BgfzQUChBgfd6L93m9weay53EWFVsMM=
github.com/gobuffalo/x v0.0.0-20180117215853-11ca13c05abd/go.mod h1:WevpGD+5YOreDJznWevcn8NTmQEW5STSBgIkpkjzqXc=
github.com/gobuffalo/x v0.0.0-20180816203319-dc54d929c4a2/go.mod h1:oX3kj94UfTZqmz9XowUgR7Ghpz5I+JmkMDpWJlwa9h8=
github.com/gobuffalo/x v0.0.0-20180924043718-b9bd64714615 h1:jqp3UQXGsUq6CQCVlfASyq4CGHH3rD+EgHmmTjYQSQk=
github.com/gobuffalo/x v0.0.0-20180924043718-b9bd64714615/go.mod h1:WevpGD+5YOreDJznWevcn8NTmQEW5STSBgIkpkjzqXc=
github.com/gobuffalo/x v0.0.0-20180930144534-b01151fcafd9 h1:oEoU+32iIiRKhGy9zAPQqZByCSYeimkEnzaryTQlSVk=
github.com/gobuffalo/x v0.0.0-20180930144534-b01151fcafd9/go.mod h1:avgKWtYleoIdUvElvJWQsTRxawk4z6WIF/gcy/4PFCg=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -187,8 +185,8 @@ github.com/markbates/oncer v0.0.0-20180924034138-723ad0170a46 h1:hE7AFWf+YsdjT67
github.com/markbates/oncer v0.0.0-20180924034138-723ad0170a46/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/markbates/refresh v1.4.1/go.mod h1:og/05QDfszH/SCl3w8VI2Or990yna0wS2lqRcJoDqAg=
github.com/markbates/refresh v1.4.7/go.mod h1:2G3FyugwHQX33+HtC7B5X2ZioIxF4UzjjeKMLGuNELo=
github.com/markbates/refresh v1.4.8 h1:Qj5UfSVFDsjPl3NaIpD+eG2WFkoWftch0DZDfuu7S8A=
github.com/markbates/refresh v1.4.8/go.mod h1:l9HCdgQRlK/GyW9phtWhJe2HdR3yrEE+vYCY7yPKgc4=
github.com/markbates/refresh v1.4.9 h1:2QblqFoY+cejmmk+5aNvyKnqO+bVKYcoDUdszPk55h0=
github.com/markbates/refresh v1.4.9/go.mod h1:7liDfImC9uQqSO7U0vTLGKrJneQgccvQV7LdGChSiKg=
github.com/markbates/sigtx v1.0.0 h1:y/xtkBvNPRjD4KeEplf4w9rJVSc23/xl+jXYGowTwy0=
github.com/markbates/sigtx v1.0.0/go.mod h1:QF1Hv6Ic6Ca6W+T+DL0Y/ypborFKyvUY9HmuCD4VeTc=
github.com/markbates/willie v1.0.6/go.mod h1:XtK+fmQ7tgVMIVAS1ghwuqVPup3GtgrNY3UCvUlPdzM=
Expand Down
11 changes: 11 additions & 0 deletions mail/attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mail

import "io"

// Attachment are files added into a email message
type Attachment struct {
Name string
Reader io.Reader
ContentType string
Embedded bool
}
7 changes: 7 additions & 0 deletions mail/body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mail

// Body represents one of the bodies in the Message could be main or alternative
type Body struct {
Content string
ContentType string
}
37 changes: 34 additions & 3 deletions mail/mail.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
package mail

// Sender interface for any upcoming mailers.
type Sender interface {
Send(Message) error
import (
"context"
"sync"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
)

// NewMessage builds a new message.
func NewMessage() Message {
return Message{
Context: context.Background(),
Headers: map[string]string{},
Data: render.Data{},
moot: &sync.RWMutex{},
}
}

// NewFromData builds a new message with raw template data given
func NewFromData(data render.Data) Message {
d := render.Data{}
for k, v := range data {
d[k] = v
}
m := NewMessage()
m.Data = d
return m
}

// New builds a new message with the current buffalo.Context
func New(c buffalo.Context) Message {
m := NewFromData(c.Data())
m.Context = c
return m
}
41 changes: 41 additions & 0 deletions mail/mail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mail

import (
"html/template"
"testing"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/httptest"
"github.com/stretchr/testify/require"
)

func Test_NewFromData(t *testing.T) {
r := require.New(t)
m := NewFromData(map[string]interface{}{
"foo": "bar",
})
r.Equal("bar", m.Data["foo"])
}

func Test_New(t *testing.T) {
r := require.New(t)

var m Message
app := buffalo.New(buffalo.Options{})
app.GET("/", func(c buffalo.Context) error {
c.Set("foo", "bar")
m = New(c)
return c.Render(200, render.String(""))
})
w := httptest.New(app)
w.HTML("/").Get()

r.NotNil(m)
r.Equal("bar", m.Data["foo"])
rp, ok := m.Data["rootPath"].(buffalo.RouteHelperFunc)
r.True(ok)
x, err := rp(map[string]interface{}{})
r.NoError(err)
r.Equal(template.HTML("/"), x)
}
48 changes: 23 additions & 25 deletions mail/message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mail

import (
"context"
"io"
"sync"

"bytes"

Expand All @@ -10,37 +12,38 @@ import (

//Message represents an Email message
type Message struct {
From string
To []string
CC []string
Bcc []string
Subject string
Headers map[string]string

Context context.Context
From string
To []string
CC []string
Bcc []string
Subject string
Headers map[string]string
Data render.Data
Bodies []Body
Attachments []Attachment
moot *sync.RWMutex
}

// Body represents one of the bodies in the Message could be main or alternative
type Body struct {
Content string
ContentType string
}

// Attachment are files added into a email message
type Attachment struct {
Name string
Reader io.Reader
ContentType string
Embedded bool
func (m *Message) merge(data render.Data) render.Data {
d := render.Data{}
m.moot.Lock()
for k, v := range m.Data {
d[k] = v
}
m.moot.Unlock()
for k, v := range data {
d[k] = v
}
return d
}

// AddBody the message by receiving a renderer and rendering data, first message will be
// used as the main message Body rest of them will be passed as alternative bodies on the
// email message
func (m *Message) AddBody(r render.Renderer, data render.Data) error {
buf := bytes.NewBuffer([]byte{})
err := r.Render(buf, data)
err := r.Render(buf, m.merge(data))

if err != nil {
return err
Expand Down Expand Up @@ -95,8 +98,3 @@ func (m *Message) AddEmbedded(name string, r io.Reader) error {
func (m *Message) SetHeader(field, value string) {
m.Headers[field] = value
}

//NewMessage Builds a new message.
func NewMessage() Message {
return Message{Headers: map[string]string{}}
}
6 changes: 6 additions & 0 deletions mail/sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package mail

// Sender interface for any upcoming mailers.
type Sender interface {
Send(Message) error
}
10 changes: 10 additions & 0 deletions route_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ func (a *App) Group(groupPath string) *App {
return g
}

// RouteHelpers returns a map of BuildPathHelper() for each route available in the app.
func (a *App) RouteHelpers() map[string]RouteHelperFunc {
rh := map[string]RouteHelperFunc{}
for _, route := range a.Routes() {
cRoute := route
rh[cRoute.PathName] = cRoute.BuildPathHelper()
}
return rh
}

func (a *App) addRoute(method string, url string, h Handler) *RouteInfo {
a.moot.Lock()
defer a.moot.Unlock()
Expand Down
28 changes: 28 additions & 0 deletions route_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,31 @@ func Test_RouteList_Lookup(t *testing.T) {
r.Nil(lRoute)

}

func Test_App_RouteHelpers(t *testing.T) {
r := require.New(t)

a := New(Options{})
r.Nil(a.root)

a.GET("/foo", voidHandler)
a.GET("/test/{id}", voidHandler)

rh := a.RouteHelpers()

r.Len(rh, 2)

f, ok := rh["fooPath"]
r.True(ok)
x, err := f(map[string]interface{}{})
r.NoError(err)
r.Equal("/foo/", string(x))

f, ok = rh["testPath"]
r.True(ok)
x, err = f(map[string]interface{}{
"id": 1,
})
r.NoError(err)
r.Equal("/test/1/", string(x))
}