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

v0.18.6 #2239

Merged
merged 18 commits into from
Apr 19, 2022
Merged

v0.18.6 #2239

Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
pull-requests: write # for actions/stale to close stale PRs
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v4
- uses: actions/stale@v5
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: "This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days."
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Setup Go ${{ matrix.go }}
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

Expand Down
31 changes: 18 additions & 13 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ import (
// Without an App you can't do much!
type App struct {
Options
// Middleware returns the current MiddlewareStack for the App/Group.
Middleware *MiddlewareStack `json:"-"`
ErrorHandlers ErrorHandlers `json:"-"`
router *mux.Router
moot *sync.RWMutex
routes RouteList
root *App
children []*App
filepaths []string
// Middleware, ErrorHandlers, router, and filepaths are moved to Home.
Home
sio4 marked this conversation as resolved.
Show resolved Hide resolved
moot *sync.RWMutex
routes RouteList
// TODO: to be deprecated #road-to-v1
root *App
children []*App

// Routenamer for the app. This field provides the ability to override the
// base route namer for something more specific to the app.
Expand All @@ -44,17 +42,24 @@ func New(opts Options) *App {

a := &App{
Options: opts,
ErrorHandlers: ErrorHandlers{
http.StatusNotFound: defaultErrorHandler,
http.StatusInternalServerError: defaultErrorHandler,
Home: Home{
name: opts.Name,
host: opts.Host,
prefix: opts.Prefix,
ErrorHandlers: ErrorHandlers{
http.StatusNotFound: defaultErrorHandler,
http.StatusInternalServerError: defaultErrorHandler,
},
router: mux.NewRouter(),
},
router: mux.NewRouter(),
moot: &sync.RWMutex{},
routes: RouteList{},
children: []*App{},

RouteNamer: baseRouteNamer{},
}
a.Home.app = a // replace root.
a.Home.appSelf = a // temporary, reverse reference to the group app.

dem := a.defaultErrorMiddleware
a.Middleware = newMiddlewareStack(dem)
Expand Down
5 changes: 4 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (h HTTPError) Unwrap() error {

// Error returns the cause of the error as string.
func (h HTTPError) Error() string {
return h.Cause.Error()
if h.Cause != nil {
return h.Cause.Error()
}
return "unknown cause"
}

// ErrorHandler interface for handling an error for a
Expand Down
12 changes: 12 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ func Test_defaultErrorHandler_XML_production(t *testing.T) {
r.Contains(b, `</response>`)
}

func Test_defaultErrorHandler_nil_error(t *testing.T) {
r := require.New(t)
app := New(Options{})
app.GET("/", func(c Context) error {
return c.Error(http.StatusInternalServerError, nil)
})

w := httptest.New(app)
res := w.JSON("/").Get()
r.Equal(http.StatusInternalServerError, res.Code)
}

func Test_PanicHandler(t *testing.T) {
app := New(Options{})
app.GET("/string", func(c Context) error {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module github.com/gobuffalo/buffalo
go 1.16

require (
github.com/BurntSushi/toml v1.0.0
github.com/BurntSushi/toml v1.1.0
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.13.0
github.com/gobuffalo/envy v1.10.1
github.com/gobuffalo/events v1.4.2
github.com/gobuffalo/flect v0.2.4
github.com/gobuffalo/flect v0.2.5
github.com/gobuffalo/github_flavored_markdown v1.1.1
github.com/gobuffalo/helpers v0.6.4
github.com/gobuffalo/httptest v1.5.1
github.com/gobuffalo/logger v1.0.6
github.com/gobuffalo/meta v0.3.1
github.com/gobuffalo/nulls v0.4.1
github.com/gobuffalo/plush/v4 v4.1.9
github.com/gobuffalo/plush/v4 v4.1.10
github.com/gobuffalo/pop/v6 v6.0.1
github.com/gobuffalo/tags/v3 v3.1.2
github.com/google/go-cmp v0.5.6 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver/v3 v3.0.3/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
Expand Down Expand Up @@ -111,8 +111,9 @@ github.com/gobuffalo/fizz v1.10.0/go.mod h1:J2XGPO0AfJ1zKw7+2BA+6FEGAkyEsdCOLvN9
github.com/gobuffalo/fizz v1.14.0 h1:hicZBYSwSWITXEDUR77tqrLU1/vScXHddd02IaFkkPI=
github.com/gobuffalo/fizz v1.14.0/go.mod h1:0aF1kAZYCfKqbLM/lmZ3jXFyqqWE/kY/nIOKnNdAYXQ=
github.com/gobuffalo/flect v0.2.0/go.mod h1:W3K3X9ksuZfir8f/LrfVtWmCDQFfayuylOJ7sz/Fj80=
github.com/gobuffalo/flect v0.2.4 h1:BSYA8+T60cdyq+vynaSUjqSVI9mDEg9ZfQUXKmfjo4I=
github.com/gobuffalo/flect v0.2.4/go.mod h1:1ZyCLIbg0YD7sDkzvFdPoOydPtD8y9JQnrOROolUcM8=
github.com/gobuffalo/flect v0.2.5 h1:H6vvsv2an0lalEaCDRThvtBfmg44W/QHXBCYUXf/6S4=
github.com/gobuffalo/flect v0.2.5/go.mod h1:1ZyCLIbg0YD7sDkzvFdPoOydPtD8y9JQnrOROolUcM8=
github.com/gobuffalo/genny/v2 v2.0.8/go.mod h1:R45scCyQfff2HysNJHNanjrpvPw4Qu+rM1MOMDBB5oU=
github.com/gobuffalo/github_flavored_markdown v1.1.0/go.mod h1:TSpTKWcRTI0+v7W3x8dkSKMLJSUpuVitlptCkpeY8ic=
github.com/gobuffalo/github_flavored_markdown v1.1.1 h1:kUf8ginyBOTRXcKSTPsPAqlA25vQ80+xAspLIYaxmTU=
Expand All @@ -131,8 +132,9 @@ github.com/gobuffalo/nulls v0.4.1 h1:k7QVCJfMplv9VRQQLb4N1d8tXUdGvcdMNfp4BfMnG2M
github.com/gobuffalo/nulls v0.4.1/go.mod h1:pp8e1hWTRJZFpMl4fj/CVbSMlaxjeGKkFq4RuBZi3w8=
github.com/gobuffalo/packd v1.0.1/go.mod h1:PP2POP3p3RXGz7Jh6eYEf93S7vA2za6xM7QT85L4+VY=
github.com/gobuffalo/plush/v4 v4.0.0/go.mod h1:ErFS3UxKqEb8fpFJT7lYErfN/Nw6vHGiDMTjxpk5bQ0=
github.com/gobuffalo/plush/v4 v4.1.9 h1:u9rQBuYCeHC0ppKxsZljk5vb1oT8PQa5EMNTAN2337s=
github.com/gobuffalo/plush/v4 v4.1.9/go.mod h1:9OOII9uAM5pZnhWu1OkQnboXJjaWMQ7kcTl3zNcxvTM=
github.com/gobuffalo/plush/v4 v4.1.10 h1:sBTmJJ1Hpj4ExqC4NFmCvAgH6Iq6t58JaWvAl8NEAFc=
github.com/gobuffalo/plush/v4 v4.1.10/go.mod h1:9OOII9uAM5pZnhWu1OkQnboXJjaWMQ7kcTl3zNcxvTM=
github.com/gobuffalo/pop/v6 v6.0.0/go.mod h1:5rd3OnViLhjteR8+0i/mT9Q4CzkTzCoR7tm/9mmAic4=
github.com/gobuffalo/pop/v6 v6.0.1 h1:4YhzxW4hVvf0xLW9zVkhPZFuH5VmBc4ffIIP/C++SLQ=
github.com/gobuffalo/pop/v6 v6.0.1/go.mod h1:5NO7ehmyRjRctnbMDhIqKkkg6zvdueufYltxErfp9BU=
Expand Down
30 changes: 16 additions & 14 deletions grifts.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,35 @@ func printMiddleware(a *App) {

func printMiddlewareByRoute(a *App) {
mws := map[string]string{}
// TODO: middleware is 'per App' can it be a loop for Apps?
fasmat marked this conversation as resolved.
Show resolved Hide resolved
for _, r := range a.Routes() {
if mws[r.App.Name] == "" {
pname := ""
key := r.App.host + r.App.name
if mws[key] == "" {
pname := r.App.host
if parent := getParentApp(r.App.root, r.App.Name); parent != nil {
pname = parent.Name
pname += parent.Name
}

mws[r.App.Name] = r.App.Middleware.String()
if mws[pname] != mws[r.App.Name] {
fmt.Printf("-> %s\n", r.App.Name)
printMiddlewareStackWithIndent(mws[r.App.Name])
mws[key] = r.App.Middleware.String()
if pname == key || mws[pname] != mws[key] {
fmt.Printf("-> %s\n", key)
printMiddlewareStackWithIndent(mws[key])
} else {
fmt.Printf("-> %s (see: %v)\n", r.App.Name, pname)
fmt.Printf("-> %s (see: %v)\n", key, pname)
}
}
s := "\n" + mws[r.App.Name]
s := "\n" + mws[key]
for k := range r.App.Middleware.skips {
mw := strings.Split(k, funcKeyDelimeter)[0]
h := strings.Split(k, funcKeyDelimeter)[1]
if h == r.HandlerName {
s = strings.Replace(s, "\n"+mw, "", 1)
}
}
if "\n"+mws[r.App.Name] != s {
if "\n"+mws[key] != s {
ahn := strings.Split(r.HandlerName, "/")
hn := ahn[len(ahn)-1]
fmt.Printf("-> %s %s (by %s)\n", r.Method, r.Path, hn)
fmt.Printf("-> %s %s (by %s)\n", r.Method, r.App.host+r.Path, hn)
printMiddlewareStackWithIndent(s)
}
}
Expand Down Expand Up @@ -106,10 +108,10 @@ func routesGrift(a *App) {
grift.Add("routes", func(c *grift.Context) error {
routes := a.Routes()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
fmt.Fprintln(w, "METHOD\t PATH\t ALIASES\t NAME\t HANDLER")
fmt.Fprintln(w, "------\t ----\t -------\t ----\t -------")
fmt.Fprintln(w, "METHOD\t HOST\t PATH\t ALIASES\t NAME\t HANDLER")
fmt.Fprintln(w, "------\t ----\t ----\t -------\t ----\t -------")
for _, r := range routes {
fmt.Fprintf(w, "%s\t %s\t %s\t %s\t %s\n", r.Method, r.Path, strings.Join(r.Aliases, " "), r.PathName, r.HandlerName)
fmt.Fprintf(w, "%s\t %s\t %s\t %s\t %s\t %s\n", r.Method, r.App.host, r.Path, strings.Join(r.Aliases, " "), r.PathName, r.HandlerName)
}
w.Flush()
return nil
Expand Down
39 changes: 39 additions & 0 deletions home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package buffalo

import (
"github.com/gorilla/mux"
)

/* TODO: consider to split out Home (or Router, whatever) from App #road-to-v1
Group and Domain based multi-homing are actually not an App if the concept
of the App represents the application. The App should be only one for whole
application.

For an extreme example, App.Group().Stop() or even App.Group().Serve() are
still valid function calls while they should not be allowed and the result
could be strage.
*/

// Home is a container for Domains and Groups that independantly serves a
// group of pages with its own Middleware and ErrorHandlers. It is usually
// a multi-homed server domain or group of paths under a certain prefix.
//
// While the App is for managing whole application life cycle along with its
// default Home, including initializing and stopping its all components such
// as listeners and long-running jobs, Home is only for a specific group of
// services to serve its service logic efficiently.
type Home struct {
app *App // will replace App.root
appSelf *App // temporary while the App is in action.
// replace Options' Name, Host, and Prefix
name string
host string
prefix string

// moved from App
// Middleware returns the current MiddlewareStack for the App/Group.
Middleware *MiddlewareStack `json:"-"`
ErrorHandlers ErrorHandlers `json:"-"`
router *mux.Router
filepaths []string
}
14 changes: 12 additions & 2 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// Routes returns a list of all of the routes defined
// in this application.
func (a *App) Routes() RouteList {
// CHKME: why this function is exported? can we deprecate it?
if a.root != nil {
return a.root.routes
}
Expand Down Expand Up @@ -64,11 +65,20 @@ type RouteHelperFunc func(opts map[string]interface{}) (template.HTML, error)
// and the name of the Handler defined to process that route.
type RouteList []*RouteInfo

var methodOrder = map[string]string{
"GET": "1",
"POST": "2",
"PUT": "3",
"DELETE": "4",
}

func (a RouteList) Len() int { return len(a) }
func (a RouteList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a RouteList) Less(i, j int) bool {
x := a[i].Path // + a[i].Method
y := a[j].Path // + a[j].Method
// NOTE: it was used for sorting of app.routes but we don't sort the routes anymore.
// keep it for compatibility but could be deprecated.
x := a[i].App.host + a[i].Path + methodOrder[a[i].Method]
y := a[j].App.host + a[j].Path + methodOrder[a[j].Method]
return x < y
}

Expand Down
2 changes: 1 addition & 1 deletion route_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (ri *RouteInfo) Alias(aliases ...string) *RouteInfo {
func (ri *RouteInfo) Name(name string) *RouteInfo {
routeIndex := -1
for index, route := range ri.App.Routes() {
if route.Path == ri.Path && route.Method == ri.Method {
if route.App.host == ri.App.host && route.Path == ri.Path && route.Method == ri.Method {
routeIndex = index
break
}
Expand Down
Loading