GithubHelp home page GithubHelp logo

ynwd / fastrex Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 1.0 214 KB

Express inspired web framework for Go. Fast and simple. Google cloud function ready.

Go 100.00%
serverless cloud-functions golang express fastify

fastrex's Introduction

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

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

Contributing

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

fastrex's People

Contributors

ynwd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

hunghoxuan

fastrex's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.