feature: print all routes message when server starts (#1677)

* feature: print all routes message when server starts

* feature: print all routes message when server starts

* feature:  print all routes message when server starts

* 🐛fix: errors unhandled

* 🐛fix: ignore child process and add some "-" to the table head

* 🐛fix: add printRoutesMessage for listener and listenTLS
pull/1681/head
vecpeng 2021-12-30 21:13:31 +08:00 committed by GitHub
parent 244d5a5450
commit f98a9ba405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
110 changed files with 237 additions and 28 deletions

74
app.go
View File

@ -20,10 +20,12 @@ import (
"os"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"text/tabwriter"
"time"
"github.com/gofiber/fiber/v2/internal/colorable"
@ -350,6 +352,10 @@ type Config struct {
// Default: []string
TrustedProxies []string `json:"trusted_proxies"`
trustedProxiesMap map[string]struct{}
//If set to true, will print all routes with their method, path and handler.
// Default: false
EnablePrintRoutes bool `json:"print_routes"`
}
// Static defines configuration options when defining static assets.
@ -389,6 +395,14 @@ type Static struct {
Next func(c *Ctx) bool
}
// RouteMessage is some message need to be print when server starts
type RouteMessage struct {
name string
method string
path string
handlers string
}
// Default Config values
const (
DefaultBodyLimit = 4 * 1024 * 1024
@ -713,6 +727,10 @@ func (app *App) Listener(ln net.Listener) error {
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), getTlsConfig(ln) != nil, "")
}
// Print routes
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}
// Start listening
return app.server.Serve(ln)
}
@ -737,6 +755,10 @@ func (app *App) Listen(addr string) error {
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), false, "")
}
// Print routes
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}
// Start listening
return app.server.Serve(ln)
}
@ -777,6 +799,10 @@ func (app *App) ListenTLS(addr, certFile, keyFile string) error {
if !app.config.DisableStartupMessage {
app.startupMessage(ln.Addr().String(), true, "")
}
// Print routes
if app.config.EnablePrintRoutes {
app.printRoutesMessage()
}
// Start listening
return app.server.ServeTLS(ln, certFile, keyFile)
}
@ -1198,3 +1224,51 @@ func (app *App) startupMessage(addr string, tls bool, pids string) {
_, _ = fmt.Fprintln(out, output)
}
// printRoutesMessage print all routes with method, path, name and handlers
// in a format of table, like this:
// method | path | name | handlers
// GET | / | routeName | github.com/gofiber/fiber/v2.emptyHandler
// HEAD | / | | github.com/gofiber/fiber/v2.emptyHandler
func (app *App) printRoutesMessage() {
// ignore child processes
if IsChild() {
return
}
const (
// cBlack = "\u001b[90m"
// cRed = "\u001b[91m"
cCyan = "\u001b[96m"
cGreen = "\u001b[92m"
cYellow = "\u001b[93m"
cBlue = "\u001b[94m"
// cMagenta = "\u001b[95m"
cWhite = "\u001b[97m"
// cReset = "\u001b[0m"
)
var routes []RouteMessage
for _, routeStack := range app.stack {
for _, route := range routeStack {
var newRoute = RouteMessage{}
newRoute.name = route.Name
newRoute.method = route.Method
newRoute.path = route.Path
for _, handler := range route.Handlers {
newRoute.handlers += runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name() + " "
}
routes = append(routes, newRoute)
}
}
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
// Sort routes by path
sort.Slice(routes, func(i, j int) bool {
return routes[i].path < routes[j].path
})
_, _ = fmt.Fprintf(w, "%smethod\t%s| %spath\t%s| %sname\t%s| %shandlers\n", cBlue, cWhite, cGreen, cWhite, cCyan, cWhite, cYellow)
_, _ = fmt.Fprintf(w, "%s------\t%s| %s----\t%s| %s----\t%s| %s--------\n", cBlue, cWhite, cGreen, cWhite, cCyan, cWhite, cYellow)
for _, route := range routes {
_, _ = fmt.Fprintf(w, "%s%s\t%s| %s%s\t%s| %s%s\t%s| %s%s\n", cBlue, route.method, cWhite, cGreen, route.path, cWhite, cCyan, route.name, cWhite, cYellow, route.handlers)
}
_ = w.Flush()
}

View File

@ -1576,3 +1576,40 @@ func Test_App_UseMountedErrorHandlerForBestPrefixMatch(t *testing.T) {
utils.AssertEqual(t, nil, err, "iotuil.ReadAll()")
utils.AssertEqual(t, "hi, i'm a custom sub sub fiber error", string(b), "Third fiber Response body")
}
func emptyHandler(c *Ctx) error {
return nil
}
func Test_App_print_Route(t *testing.T) {
app := New(Config{EnablePrintRoutes: true})
app.Get("/", emptyHandler).Name("routeName")
printRoutesMessage := captureOutput(func() {
app.printRoutesMessage()
})
fmt.Println(printRoutesMessage)
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "GET"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "/"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "emptyHandler"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "routeName"))
}
func Test_App_print_Route_with_group(t *testing.T) {
app := New(Config{EnablePrintRoutes: true})
app.Get("/", emptyHandler)
v1 := app.Group("v1")
v1.Get("/test", emptyHandler).Name("v1")
v1.Post("/test/fiber", emptyHandler)
v1.Put("/test/fiber/*", emptyHandler)
printRoutesMessage := captureOutput(func() {
app.printRoutesMessage()
})
fmt.Println(printRoutesMessage)
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "GET"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "/"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "emptyHandler"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "/v1/test"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "POST"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "/v1/test/fiber"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "PUT"))
utils.AssertEqual(t, true, strings.Contains(printRoutesMessage, "/v1/test/fiber/*"))
}

View File

@ -1,3 +1,4 @@
//go:build appengine
// +build appengine
package colorable

View File

@ -1,5 +1,5 @@
// +build !windows
// +build !appengine
//go:build !windows && !appengine
// +build !windows,!appengine
package colorable

View File

@ -1,5 +1,5 @@
// +build windows
// +build !appengine
//go:build windows && !appengine
// +build windows,!appengine
package colorable

View File

@ -1,3 +1,4 @@
//go:build !appengine
// +build !appengine
package fasttemplate

View File

@ -1,3 +1,4 @@
//go:build appengine
// +build appengine
package fasttemplate

View File

@ -1,3 +1,4 @@
//go:build appengine
// +build appengine
package fwd

View File

@ -1,3 +1,4 @@
//go:build !appengine
// +build !appengine
package fwd

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package oleutil

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package oleutil

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package oleutil

View File

@ -1,6 +1,7 @@
// This file is here so go get succeeds as without it errors with:
// no buildable Go source files in ...
//
//go:build !windows
// +build !windows
package oleutil

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build 386
// +build 386
package ole

View File

@ -1,3 +1,4 @@
//go:build amd64
// +build amd64
package ole

View File

@ -1,3 +1,4 @@
//go:build windows && 386
// +build windows,386
package ole

View File

@ -1,3 +1,4 @@
//go:build windows && amd64
// +build windows,amd64
package ole

View File

@ -1,3 +1,4 @@
//go:build ppc64le
// +build ppc64le
package ole

View File

@ -1,3 +1,4 @@
//go:build s390x
// +build s390x
package ole

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package ole

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package ole

View File

@ -1,3 +1,4 @@
//go:build darwin
// +build darwin
package common

View File

@ -1,3 +1,4 @@
//go:build freebsd || openbsd
// +build freebsd openbsd
package common

View File

@ -1,3 +1,4 @@
//go:build linux
// +build linux
package common

View File

@ -1,3 +1,4 @@
//go:build openbsd
// +build openbsd
package common

View File

@ -1,3 +1,4 @@
//go:build linux || freebsd || darwin || openbsd
// +build linux freebsd darwin openbsd
package common

View File

@ -1,3 +1,4 @@
//go:build darwin
// +build darwin
package cpu

View File

@ -1,5 +1,5 @@
// +build darwin
// +build cgo
//go:build darwin && cgo
// +build darwin,cgo
package cpu

View File

@ -1,5 +1,5 @@
// +build darwin
// +build !cgo
//go:build darwin && !cgo
// +build darwin,!cgo
package cpu

View File

@ -1,3 +1,4 @@
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows && !dragonfly
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly
package cpu

View File

@ -1,3 +1,4 @@
//go:build linux
// +build linux
package cpu

View File

@ -1,3 +1,4 @@
//go:build openbsd
// +build openbsd
package cpu

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package cpu

View File

@ -1,3 +1,4 @@
//go:build freebsd || openbsd
// +build freebsd openbsd
package load

View File

@ -1,3 +1,4 @@
//go:build freebsd
// +build freebsd
package load

View File

@ -1,3 +1,4 @@
//go:build openbsd
// +build openbsd
package load

View File

@ -1,3 +1,4 @@
//go:build darwin
// +build darwin
package mem

View File

@ -1,5 +1,5 @@
// +build darwin
// +build cgo
//go:build darwin && cgo
// +build darwin,cgo
package mem

View File

@ -1,5 +1,5 @@
// +build darwin
// +build !cgo
//go:build darwin && !cgo
// +build darwin,!cgo
package mem

View File

@ -1,3 +1,4 @@
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows
package mem

View File

@ -1,3 +1,4 @@
//go:build freebsd
// +build freebsd
package mem

View File

@ -1,3 +1,4 @@
//go:build linux
// +build linux
package mem

View File

@ -1,3 +1,4 @@
//go:build openbsd
// +build openbsd
package mem

View File

@ -1,5 +1,6 @@
// +build openbsd
// +build 386
//go:build openbsd && 386
// +build openbsd,386
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs mem/types_openbsd.go

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package mem

View File

@ -1,3 +1,4 @@
//go:build ignore
// +build ignore
/*

View File

@ -1,3 +1,4 @@
//go:build aix
// +build aix
package net

View File

@ -1,3 +1,4 @@
//go:build darwin
// +build darwin
package net

View File

@ -1,3 +1,4 @@
//go:build !aix && !darwin && !linux && !freebsd && !openbsd && !windows
// +build !aix,!darwin,!linux,!freebsd,!openbsd,!windows
package net

View File

@ -1,3 +1,4 @@
//go:build freebsd
// +build freebsd
package net

View File

@ -1,3 +1,4 @@
//go:build linux
// +build linux
package net

View File

@ -1,3 +1,4 @@
//go:build openbsd
// +build openbsd
package net

View File

@ -1,3 +1,4 @@
//go:build freebsd || darwin
// +build freebsd darwin
package net

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package net

View File

@ -1,3 +1,4 @@
//go:build darwin
// +build darwin
package process

View File

@ -1,5 +1,6 @@
// +build darwin
// +build arm64
//go:build darwin && arm64
// +build darwin,arm64
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs process/types_darwin.go

View File

@ -1,5 +1,5 @@
// +build darwin
// +build cgo
//go:build darwin && cgo
// +build darwin,cgo
package process

View File

@ -1,5 +1,5 @@
// +build darwin
// +build !cgo
//go:build darwin && !cgo
// +build darwin,!cgo
package process

View File

@ -1,3 +1,4 @@
//go:build !darwin && !linux && !freebsd && !openbsd && !windows
// +build !darwin,!linux,!freebsd,!openbsd,!windows
package process

View File

@ -1,3 +1,4 @@
//go:build freebsd
// +build freebsd
package process

View File

@ -1,5 +1,6 @@
// +build freebsd
// +build arm64
//go:build freebsd && arm64
// +build freebsd,arm64
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs process/types_freebsd.go

View File

@ -1,3 +1,4 @@
//go:build linux
// +build linux
package process

View File

@ -1,3 +1,4 @@
//go:build openbsd
// +build openbsd
package process

View File

@ -1,5 +1,6 @@
// +build openbsd
// +build 386
//go:build openbsd && 386
// +build openbsd,386
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs process/types_openbsd.go

View File

@ -1,3 +1,4 @@
//go:build linux || freebsd || openbsd || darwin
// +build linux freebsd openbsd darwin
package process

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package process

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package process

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package process

View File

@ -5,6 +5,7 @@
// Hand Writing
// - all pointer in ExternProc to uint64
//go:build ignore
// +build ignore
/*

View File

@ -1,3 +1,4 @@
//go:build ignore
// +build ignore
// We still need editing by hands.

View File

@ -1,3 +1,4 @@
//go:build ignore
// +build ignore
// We still need editing by hands.

View File

@ -1,3 +1,4 @@
//go:build (darwin || freebsd || openbsd || netbsd || dragonfly) && !appengine
// +build darwin freebsd openbsd netbsd dragonfly
// +build !appengine

View File

@ -1,3 +1,4 @@
//go:build appengine || js || nacl || wasm
// +build appengine js nacl wasm
package isatty

View File

@ -1,3 +1,4 @@
//go:build plan9
// +build plan9
package isatty

View File

@ -1,5 +1,5 @@
// +build solaris
// +build !appengine
//go:build solaris && !appengine
// +build solaris,!appengine
package isatty

View File

@ -1,3 +1,4 @@
//go:build (linux || aix) && !appengine
// +build linux aix
// +build !appengine

Some files were not shown because too many files have changed in this diff Show More