Categorygithub.com/fastrodev/fastrex
repositorypackage
0.0.0-20211105041338-1516bd717674
Repository: https://github.com/fastrodev/fastrex.git
Documentation: pkg.go.dev

# README

Fastrex

Coverage Status

Fast and simple web application framework for Go inspired by the most popular node.js web framework: Express.js. It implements ServeHTTP interface so you can use express style routing. It also wraps and extends the net/http Request and ResponseWriter into an easy to read and use function signature.

Get Started

Init folder and install:

mkdir app && cd app
go mod init app
go get github.com/fastrodev/fastrex

Create main.go file:

package main

import "github.com/fastrodev/fastrex"

func handler(req fastrex.Request, res fastrex.Response) {
	res.Send("root")
}

func main() {
	app := fastrex.New()
	app.Get("/", handler)
	err := app.Listen(9000)
	if err != nil {
		panic(err)
	}
}


Run webapp locally:

go run main.go

Middleware

You can access Request and Response field and function before the handler process the incoming request.

App Middleware

package main

import "github.com/fastrodev/fastrex"

func handler(req fastrex.Request, res fastrex.Response) {
	res.Send("root")
}

func appMiddleware(req fastrex.Request, res fastrex.Response, next fastrex.Next) {
	if req.URL.Path == "/" {
		res.Send("appMiddleware")
		return
	}

	next(req, res)
}

func main() {
	app := fastrex.New()
	app.Use(appMiddleware)
	app.Get("/", handler)
	err := app.Listen(9000)
	if err != nil {
		panic(err)
	}
}

Route Middleware

package main

import "github.com/fastrodev/fastrex"

func handler(req fastrex.Request, res fastrex.Response) {
	res.Send("root")
}

func routeMiddleware(req fastrex.Request, res fastrex.Response, next fastrex.Next) {
	if req.URL.Path == "/" {
		res.Send("appMiddleware")
		return
	}
}

func main() {
	app := fastrex.New()
	app.Get("/", handler, routeMiddleware)
	err := app.Listen(9000)
	if err != nil {
		panic(err)
	}
}

Module

You can group static files, paths, routes, middlewares, and handlers into a module.

package main

import "github.com/fastrodev/fastrex"

func moduleMiddleware(req fastrex.Request, res fastrex.Response, next fastrex.Next) {
	if req.URL.Path == "/api/user" {
		res.Send("userMiddleware")
		return
	}
	next(req, res)
}

func handler(req fastrex.Request, res fastrex.Response) {
	res.Send("userModule")
}

func module(app fastrex.App) fastrex.App {
	app.Use(moduleMiddleware)
	app.Get("/user", handler)
	return app
}

func main() {
	app := fastrex.New()
	app.Register(module, "/api")
	err := app.Listen(9000)
	if err != nil {
		panic(err)
	}
}

Template

You can render html by create HTML template at template folder.

<html>{{.Title}}{{.Name}}</html>

Then you add them to app with Template function.

And finally, call Render function from handler.

package main

import "github.com/fastrodev/fastrex"

func handler(req fastrex.Request, res fastrex.Response) {
	data := struct {
		Title string
		Name  string
	}{
		"hallo",
		"world",
	}
	err := res.Render(data)
	if err != nil {
		panic(err)
	}
}

func main() {
	app := fastrex.New()
	app.Template("template/app.html")
	app.Get("/", handler)
	err := app.Listen(9000)
	if err != nil {
		panic(err)
	}
}

Serverless

You can deploy your codes to google cloud function. With this approach, you don't call the Listen function again. You must create a new function as the entry point for standard net/http Request and ResponseWriter.

package serverless

import (
  "net/http"

  "github.com/fastrodev/fastrex"
)

func handler(req fastrex.Request, res fastrex.Response) {
  res.Json(`{"message":"hello"}`)
}

func createApp() fastrex.App {
  app := fastrex.New()
  app.Get("/", handler)
  return app
}

func Main(w http.ResponseWriter, r *http.Request) {
  createApp().Serverless(true).ServeHTTP(w, r)
}

How to deploy:

gcloud functions deploy Main --runtime go116 --trigger-http --allow-unauthenticated

Demo and full example: https://github.com/fastrodev/serverless

Benchmarks

ModuleRequests/secTransfer/sec
Fastrex95249.1110.99MB
Go std88700.4910.83MB
Node std50696.056.48MB
Express9006.682.05MB

Benchmarks repository: https://github.com/fastrodev/benchmarks

Contributing

We appreciate your help! The main purpose of this repository is to improve performance and readability, making it faster and easier to use.