Skip to content

Commit

Permalink
✨ feature: customizable colors (gofiber#1977)
Browse files Browse the repository at this point in the history
* ✨ feature: customizable colors

* ✨ feature: customizable colors

* ✨ feature: customizable colors
  • Loading branch information
efectn authored and trim21 committed Aug 15, 2022
1 parent 0a7b967 commit cf18767
Show file tree
Hide file tree
Showing 8 changed files with 739 additions and 623 deletions.
425 changes: 8 additions & 417 deletions app.go

Large diffs are not rendered by default.

164 changes: 0 additions & 164 deletions app_test.go
Expand Up @@ -6,7 +6,6 @@ package fiber

import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
Expand All @@ -26,7 +25,6 @@ import (

"github.com/gofiber/fiber/v2/utils"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)

var testEmptyHandler = func(c *Ctx) error {
Expand Down Expand Up @@ -1122,131 +1120,6 @@ func Test_App_Next_Method(t *testing.T) {
utils.AssertEqual(t, 404, resp.StatusCode, "Status code")
}

// go test -run Test_App_Listen
func Test_App_Listen(t *testing.T) {
app := New(Config{DisableStartupMessage: true})

utils.AssertEqual(t, false, app.Listen(":99999") == nil)

go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()

utils.AssertEqual(t, nil, app.Listen(":4003"))
}

// go test -run Test_App_Listen_Prefork
func Test_App_Listen_Prefork(t *testing.T) {
testPreforkMaster = true

app := New(Config{DisableStartupMessage: true, Prefork: true})

utils.AssertEqual(t, nil, app.Listen(":99999"))
}

// go test -run Test_App_ListenTLS
func Test_App_ListenTLS(t *testing.T) {
app := New()

// invalid port
utils.AssertEqual(t, false, app.ListenTLS(":99999", "./.github/testdata/ssl.pem", "./.github/testdata/ssl.key") == nil)
// missing perm/cert file
utils.AssertEqual(t, false, app.ListenTLS(":0", "", "./.github/testdata/ssl.key") == nil)

go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()

utils.AssertEqual(t, nil, app.ListenTLS(":0", "./.github/testdata/ssl.pem", "./.github/testdata/ssl.key"))
}

// go test -run Test_App_ListenTLS_Prefork
func Test_App_ListenTLS_Prefork(t *testing.T) {
testPreforkMaster = true

app := New(Config{DisableStartupMessage: true, Prefork: true})

// invalid key file content
utils.AssertEqual(t, false, app.ListenTLS(":0", "./.github/testdata/ssl.pem", "./.github/testdata/template.tmpl") == nil)

utils.AssertEqual(t, nil, app.ListenTLS(":99999", "./.github/testdata/ssl.pem", "./.github/testdata/ssl.key"))
}

// go test -run Test_App_ListenMutualTLS
func Test_App_ListenMutualTLS(t *testing.T) {
app := New()

// invalid port
utils.AssertEqual(t, false, app.ListenMutualTLS(":99999", "./.github/testdata/ssl.pem", "./.github/testdata/ssl.key", "./.github/testdata/ca-chain.cert.pem") == nil)
// missing perm/cert file
utils.AssertEqual(t, false, app.ListenMutualTLS(":0", "", "./.github/testdata/ssl.key", "") == nil)

go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()

utils.AssertEqual(t, nil, app.ListenMutualTLS(":0", "./.github/testdata/ssl.pem", "./.github/testdata/ssl.key", "./.github/testdata/ca-chain.cert.pem"))
}

// go test -run Test_App_ListenMutualTLS_Prefork
func Test_App_ListenMutualTLS_Prefork(t *testing.T) {
testPreforkMaster = true

app := New(Config{DisableStartupMessage: true, Prefork: true})

// invalid key file content
utils.AssertEqual(t, false, app.ListenMutualTLS(":0", "./.github/testdata/ssl.pem", "./.github/testdata/template.html", "") == nil)

utils.AssertEqual(t, nil, app.ListenMutualTLS(":99999", "./.github/testdata/ssl.pem", "./.github/testdata/ssl.key", "./.github/testdata/ca-chain.cert.pem"))
}

// go test -run Test_App_Listener
func Test_App_Listener(t *testing.T) {
app := New()

go func() {
time.Sleep(500 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()

ln := fasthttputil.NewInmemoryListener()
utils.AssertEqual(t, nil, app.Listener(ln))
}

// go test -run Test_App_Listener_Prefork
func Test_App_Listener_Prefork(t *testing.T) {
testPreforkMaster = true

app := New(Config{DisableStartupMessage: true, Prefork: true})

ln := fasthttputil.NewInmemoryListener()
utils.AssertEqual(t, nil, app.Listener(ln))
}

func Test_App_Listener_TLS_Listener(t *testing.T) {
// Create tls certificate
cer, err := tls.LoadX509KeyPair("./.github/testdata/ssl.pem", "./.github/testdata/ssl.key")
if err != nil {
utils.AssertEqual(t, nil, err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}

ln, err := tls.Listen(NetworkTCP4, ":0", config)
utils.AssertEqual(t, nil, err)

app := New()

go func() {
time.Sleep(time.Millisecond * 500)
utils.AssertEqual(t, nil, app.Shutdown())
}()

utils.AssertEqual(t, nil, app.Listener(ln))
}

// go test -v -run=^$ -bench=Benchmark_AcquireCtx -benchmem -count=4
func Benchmark_AcquireCtx(b *testing.B) {
app := New()
Expand Down Expand Up @@ -1710,40 +1583,3 @@ 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/*"))
}
99 changes: 99 additions & 0 deletions color.go
@@ -0,0 +1,99 @@
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io

package fiber

// Colors is a struct to define custom colors for Fiber app and middlewares.
type Colors struct {
// Black color.
//
// Optional. Default: "\u001b[90m"
Black string

// Red color.
//
// Optional. Default: "\u001b[91m"
Red string

// Green color.
//
// Optional. Default: "\u001b[92m"
Green string

// Yellow color.
//
// Optional. Default: "\u001b[93m"
Yellow string

// Blue color.
//
// Optional. Default: "\u001b[94m"
Blue string

// Magenta color.
//
// Optional. Default: "\u001b[95m"
Magenta string

// Cyan color.
//
// Optional. Default: "\u001b[96m"
Cyan string

// White color.
//
// Optional. Default: "\u001b[97m"
White string

// Reset color.
//
// Optional. Default: "\u001b[0m"
Reset string
}

// Default color codes
var DefaultColors = Colors{
Black: "\u001b[90m",
Red: "\u001b[91m",
Green: "\u001b[92m",
Yellow: "\u001b[93m",
Blue: "\u001b[94m",
Magenta: "\u001b[95m",
Cyan: "\u001b[96m",
White: "\u001b[97m",
Reset: "\u001b[0m",
}

// defaultColors is a function to override default colors to config
func defaultColors(colors Colors) Colors {
if colors.Red == "" {
colors.Red = DefaultColors.Red
}

if colors.Green == "" {
colors.Green = DefaultColors.Green
}

if colors.Yellow == "" {
colors.Yellow = DefaultColors.Yellow
}

if colors.Blue == "" {
colors.Blue = DefaultColors.Blue
}

if colors.Magenta == "" {
colors.Magenta = DefaultColors.Magenta
}

if colors.Cyan == "" {
colors.Cyan = DefaultColors.Cyan
}

if colors.Reset == "" {
colors.Reset = DefaultColors.Reset
}

return colors
}

0 comments on commit cf18767

Please sign in to comment.