• Stars
    star
    212
  • Rank 186,122 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created about 6 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

reverse http / websocket proxy based on fasthttp

fasthttp-reverse-proxy

Go Report Card GoReportCard

reverse http proxy handler based on fasthttp.

Features

  • HTTP reverse proxy based fasthttp

    • it's faster than golang standard httputil.ReverseProxy library.
    • implemented by fasthttp.HostClient
    • support balance distribute based rounddobin
    • HostClient object pool with an overlay of fasthttp connection pool.
  • WebSocket reverse proxy.

Get started

HTTP (with balancer option)

var (
	proxyServer = proxy.NewReverseProxy("localhost:8080")

	// use with balancer
	// weights = map[string]proxy.Weight{
	// 	"localhost:8080": 20,
	// 	"localhost:8081": 30,
	// 	"localhost:8082": 50,
	// }
	// proxyServer = proxy.NewReverseProxy("", proxy.WithBalancer(weights))

)

// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
	// all proxy to localhost
	proxyServer.ServeHTTP(ctx)
}

func main() {
	if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
		log.Fatal(err)
	}
}

Websocket

var (
	proxyServer *proxy.WSReverseProxy
	once        sync.Once
)

// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
	once.Do(func() {
		var err error
		proxyServer, err = proxy.NewWSReverseProxyWith(
			proxy.WithURL_OptionWS("ws://localhost:8080/echo"),
		)
		if err != nil {
			panic(err)
		}
	})

	switch string(ctx.Path()) {
	case "/echo":
		proxyServer.ServeHTTP(ctx)
	case "/":
		fasthttp.ServeFileUncompressed(ctx, "./index.html")
	default:
		ctx.Error("Unsupported path", fasthttp.StatusNotFound)
	}
}

func main() {
	log.Println("serving on: 8081")
	if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
		log.Fatal(err)
	}
}

Usages

Contrast

References

Thanks

JetBrains

More Repositories

1

go-qrcode

To help gophers generate QR Codes with customized styles, such as color, block size, block shape, and icon.
Go
518
star
2

protoc-gen-fieldmask

Generate FieldMask utility functions for protobuf
Go
34
star
3

infrastructure

collecting some tools those will be useful while coding a web application with Golang
Go
28
star
4

cassem

(WIP) A distributed Configuration Center Application.
Go
24
star
5

rpc

remote procedure call. DO NOT use it in PRODUCTION.
Go
23
star
6

gitlab-flow

A CLI tool to help manage the flow of developing on gitlab.
Go
17
star
7

socket.io-app

socket.io based realtime app
JavaScript
13
star
8

tracing-practice

opentracing practice in golang micro server (gRPC + HTTP)
Makefile
7
star
9

recipe

my own recipe App codes including recipe-spider, recipe-mobile, recipe-server, maybe post some blog to record this.
JavaScript
5
star
10

websocket

websocket protocol implemented base Go
Go
5
star
11

opentelemetry-quake

Work like a Quake of OpenTelemetry toolchain, you have no need to know how it works, just use the power to thunder.
Go
4
star
12

apollo-synchronizer

Help developer to sync between local file and remote apollo portal web since portal web is so messy to use
Go
4
star
13

go-watcher

go-watcher for hot reloading with your command ~
Go
4
star
14

shorturl

ShortUrl System Coding
Go
3
star
15

cached-repository

a basic repository to support cached data in memory and is based LRU-K cache replacing algorithm
Go
3
star
16

playground

coding practice in go, java, python and some third-party libs usage demo
Go
2
star
17

gateway

api gateway for micro server
Go
2
star
18

snowflake

twitter's global ID generator alg implementation based c, and it provide extension for lua and CLI program.
C
2
star
19

gRBAC-server

admin api server of gRBAC, also view
Go
1
star
20

reedsolomon

Go
1
star
21

log

A light log library for go application.
Go
1
star
22

gorm-opentracing

opentracing support for gorm2
Go
1
star
23

zimuku-spider

spider to crawl subtitles files and extract, clean so I can use sentences in subtitles
SRecode Template
1
star
24

hashtable

golang hashtable implements based redis dict
Go
1
star