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.
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
You can access Request
and Response
field and function before the handler process the incoming request.
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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
Module | Requests/sec | Transfer/sec |
---|---|---|
Fastrex | 95249.11 | 10.99MB |
Go std | 88700.49 | 10.83MB |
Node std | 50696.05 | 6.48MB |
Express | 9006.68 | 2.05MB |
Benchmarks repository: https://github.com/fastrodev/benchmarks
We appreciate your help! The main purpose of this repository is to improve performance and readability, making it faster and easier to use.