From 334279ab55fb8fd2c6e3b52e5094ad53265b557b Mon Sep 17 00:00:00 2001 From: Abdelrahman Ahmed Date: Mon, 27 Jul 2020 01:34:34 +0200 Subject: [PATCH] update README (#65) --- README.md | 18 ++++++++++++------ .../ssl-cert-snakeoil.crt | 0 .../ssl-cert-snakeoil.key | 0 context.go | 4 ++-- gearbox.go | 4 ++-- gearbox_test.go | 4 ++-- router.go | 2 ++ tree.go | 6 ++++-- 8 files changed, 24 insertions(+), 14 deletions(-) rename ssl-cert-snakeoil.crt => assets/ssl-cert-snakeoil.crt (100%) rename ssl-cert-snakeoil.key => assets/ssl-cert-snakeoil.key (100%) diff --git a/README.md b/README.md index 3b97378..3b6dc9b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@

-**gearbox** :gear: is a web framework for building micro services written in Go with a focus on high performance and memory optimization. It's built on [fasthttp](https://github.com/valyala/fasthttp) which is **up to 10x faster** than net/http +**gearbox** :gear: is a web framework for building micro services written in Go with a focus on high performance. It's built on [fasthttp](https://github.com/valyala/fasthttp) which is **up to 10x faster** than net/http ### gearbox seeks to be @@ -90,8 +90,9 @@ func main() { package main import ( - "github.com/gogearbox/gearbox" "log" + + "github.com/gogearbox/gearbox" ) func main() { @@ -101,12 +102,16 @@ func main() { // create a logger middleware logMiddleware := func(ctx gearbox.Context) { log.Printf("log message!") - ctx.Next() // Next is what allows the request to continue to the next middleware/handler + + // Next is what allows the request to continue to the next + // middleware/handler + ctx.Next() } // create an unauthorized middleware unAuthorizedMiddleware := func(ctx gearbox.Context) { - ctx.Status(gearbox.StatusUnauthorized).SendString("You are unauthorized to access this page!") + ctx.Status(gearbox.StatusUnauthorized) + .SendString("You are unauthorized to access this page!") } // Register the log middleware for all requests @@ -134,7 +139,8 @@ func main() { gb.Group("/api", accountRoutes) // Define a route with unAuthorizedMiddleware as the middleware - // you can define as many middlewares as you want and have the handler as the last argument + // you can define as many middlewares as you want and have + // the handler as the last argument gb.Get("/protected", unAuthorizedMiddleware, func(ctx gearbox.Context) { ctx.SendString("You accessed a protected page") }) @@ -179,7 +185,7 @@ Check [Our Docs](https://gogearbox.com/docs) for more information about **gearbo ### Sponsors Organizations that are helping to manage, promote, and support **Gearbox** :gear: -| | +| [](https://www.trella.app) | |:-: | | [trella](https://www.trella.app): *A B2B technology platform and trucking
marketplace that connects shippers with carriers* | diff --git a/ssl-cert-snakeoil.crt b/assets/ssl-cert-snakeoil.crt similarity index 100% rename from ssl-cert-snakeoil.crt rename to assets/ssl-cert-snakeoil.crt diff --git a/ssl-cert-snakeoil.key b/assets/ssl-cert-snakeoil.key similarity index 100% rename from ssl-cert-snakeoil.key rename to assets/ssl-cert-snakeoil.key diff --git a/context.go b/context.go index 7738014..a0cbebe 100644 --- a/context.go +++ b/context.go @@ -50,7 +50,7 @@ func (ctx *context) Context() *fasthttp.RequestCtx { return ctx.requestCtx } -// SendString sends body of response as a string +// SendString sets body of response as a string func (ctx *context) SendString(value string) Context { ctx.requestCtx.SetBodyString(value) return ctx @@ -77,7 +77,7 @@ func (ctx *context) Query(key string) string { return GetString(ctx.requestCtx.QueryArgs().Peek(key)) } -// Body contains the raw body submitted in a POST request +// Body returns the raw body submitted in a POST request func (ctx *context) Body() string { return GetString(ctx.requestCtx.Request.Body()) } diff --git a/gearbox.go b/gearbox.go index 0cc0bbe..91d0bcf 100644 --- a/gearbox.go +++ b/gearbox.go @@ -1,4 +1,4 @@ -// Package gearbox is a web framework with a focus on high performance and memory optimization +// Package gearbox is a web framework with a focus on high performance package gearbox import ( @@ -328,7 +328,6 @@ func (gb *gearbox) Start(address string) error { type customLogger struct{} func (dl *customLogger) Printf(format string, args ...interface{}) { - //log.Printf(format) } // newHTTPServer returns a new instance of fasthttp server @@ -444,6 +443,7 @@ func (gb *gearbox) Group(prefix string, routes []*Route) []*Route { return routes } +// Static serves files in root directory under specific prefix func (gb *gearbox) Static(prefix, root string) { if gb.settings.CaseInSensitive { prefix = strings.ToLower(prefix) diff --git a/gearbox_test.go b/gearbox_test.go index 2fa4ed2..836022f 100644 --- a/gearbox_test.go +++ b/gearbox_test.go @@ -379,8 +379,8 @@ func TestStart(t *testing.T) { // TestStartWithTLS tests start service method func TestStartWithTLS(t *testing.T) { gb := New(&Settings{ - TLSKeyPath: "ssl-cert-snakeoil.key", - TLSCertPath: "ssl-cert-snakeoil.crt", + TLSKeyPath: "./assets/ssl-cert-snakeoil.key", + TLSCertPath: "./assets/ssl-cert-snakeoil.crt", TLSEnabled: true, }) diff --git a/router.go b/router.go index 84dedd0..a244d6f 100644 --- a/router.go +++ b/router.go @@ -80,6 +80,8 @@ func (r *router) allowed(reqMethod, path string, ctx *context) string { var allow string pathLen := len(path) + + // handle * and /* requests if (pathLen == 1 && path[0] == '*') || (pathLen > 1 && path[1] == '*') { for method := range r.trees { if method == MethodOptions { diff --git a/tree.go b/tree.go index bf7fba7..f8ca694 100644 --- a/tree.go +++ b/tree.go @@ -9,7 +9,7 @@ type nodeType uint8 const ( static nodeType = iota root - parama + param catchAll ) @@ -21,6 +21,7 @@ type node struct { handlers handlersChain } +// addRoute adds a node with the provided handlers to the path func (n *node) addRoute(path string, handlers handlersChain) { currentNode := n originalPath := path @@ -74,7 +75,7 @@ func (n *node) addRoute(path string, handlers handlersChain) { originalPath + "'") } } else { - nType = parama + nType = param if _, ok := paramNames[pathSegment]; ok { panic("parameter " + pathSegment + " must be unique in path '" + originalPath + "'") @@ -117,6 +118,7 @@ func (n *node) addRoute(path string, handlers handlersChain) { } } +// matchRoute returns handlers registered with the given path func (n *node) matchRoute(path string, ctx *context) handlersChain { pathLen := len(path) if pathLen > 0 && path[0] != '/' {