/gearbox

:gear: gearbox is a web framework written in Go with a focus on high performance and memory optimization

Primary LanguageGoMIT LicenseMIT


DeepSource

gearbox ⚙️ is a web framework for building micro services written in Go with a focus on high performance and memory optimization.

Currently, gearbox ⚙️ is under development (not production ready) and built on fasthttp which is up to 10x faster than net/http

In gearbox, we care about peformance and memory which will be used by each method while building things up and how we can improve that. It also takes more time to research about each component that will be used and compare it with different implementations of other open source web frameworks. It may end up writing our own components in an optimized way to achieve our goals

gearbox seeks to be

  • Secure 🔐
  • Fast 🚀
  • Simple 👓
  • Easy to use
  • Lightweight

Supported Go versions & installation

⚙️ gearbox requires version 1.11 or higher of Go (Download Go)

Just use go get to download and install gearbox

go get -u github.com/gogearbox/gearbox

Examples

package main

import (
	"github.com/gogearbox/gearbox"
)

func main() {
	// Setup gearbox
	gb := gearbox.New()

	// Define your handlers
	gb.Get("/hello", func(ctx *gearbox.Context) {
		ctx.RequestCtx.Response.SetBodyString("Hello World!")
	})

	// Start service
	gb.Start(":3000")
}

Parameters

package main

import (
	"github.com/gogearbox/gearbox"
)

func main() {
	// Setup gearbox
	gb := gearbox.New()

	// Handler with parameter
	gb.Get("/users/:user", func(ctx *gearbox.Context) {
		fmt.Printf("%s\n", ctx.Params["user"])
	})

	// Handler with optional parameter
	gb.Get("/search/:pattern?", func(ctx *gearbox.Context) {
		fmt.Printf("%s\n", ctx.Params["pattern"])
	})

	// Handler with regex parameter
	gb.Get("/book/:name:([a-z]+[0-3])", func(ctx *gearbox.Context) {
		fmt.Printf("%s\n", ctx.Params["name"])
	})

	// Start service
	gb.Start(":3000")
}

Middlewares

package main

import (
	"github.com/gogearbox/gearbox"
	"log"
)

func main() {
	// Setup gearbox
	gb := gearbox.New()

	// create a logger middleware
	logMiddleware := func(ctx *gearbox.Context) {
		log.Printf(ctx.RequestCtx.String())
		ctx.Next() // Next is what allows the request to continue to the next middleware/handler
	}

	// create an unauthorized middleware
	unAuthorizedMiddleware := func(ctx *gearbox.Context) {
		ctx.RequestCtx.SetStatusCode(401) // unauthorized status code
		ctx.RequestCtx.Response.SetBodyString("You are unauthorized to access this page!")
	}

	// Register the log middleware for all requests
	gb.Use(logMiddleware)

	// Define your handlers
	gb.Get("/hello", func(ctx *gearbox.Context) {
		ctx.RequestCtx.Response.SetBodyString("Hello World!")
	})
    
	// Register the routes to be used when grouping routes
	routes := []*gearbox.Route {
		gb.Get("/id", func(ctx *gearbox.Context) {
			ctx.RequestCtx.Response.SetBodyString("User X")
		}),
		gb.Delete("/id", func(ctx *gearbox.Context) {
			ctx.RequestCtx.Response.SetBodyString("Deleted")
		})
	}

	// Group account routes
	accountRoutes := gb.Group("/account", routes)

	// Group account routes to be under api
	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
	gb.Get("/protected", unAuthorizedMiddleware, func(ctx *gearbox.Context) {
		ctx.RequestCtx.Response.SetBodyString("You accessed a protected page")
	})

	// Start service
	gb.Start(":3000")
}

Contribute & Support

Check Our Docs for more information about gearbox and how to contribute

Contributors

Get in touch!

Feel free to chat with us on Discord, or email us at gearbox@googlegroups.com if you have questions, or suggestions

License

gearbox is licensed under MIT License

Logo is created by Mahmoud Sayed and distributed under Creative Commons License

Third-party library licenses