proxy

package
v0.0.0-...-83adff0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2020 License: GPL-3.0 Imports: 12 Imported by: 0

README

business/blademaster/proxy

项目简介

blademaster 的 reverse proxy middleware,主要用于转发一些 API 请求

编译环境

  • 请只用 Golang v1.8.x 以上版本编译执行

依赖包

  • No other dependency

Documentation

Overview

Example

This example create several reverse proxy to show how to use proxy middleware. We proxy three path to `api.bilibili.com` and return response without any changes.

package main

import (
	"go-common/library/net/http/blademaster"
	"go-common/library/net/http/blademaster/middleware/proxy"
)

func main() {
	proxies := map[string]string{
		"/index":        "http://api.bilibili.com/html/index",
		"/ping":         "http://api.bilibili.com/api/ping",
		"/api/versions": "http://api.bilibili.com/api/web/versions",
	}

	engine := blademaster.Default()
	for path, ep := range proxies {
		engine.GET(path, proxy.NewAlways(ep))
	}
	engine.Run(":18080")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAlways

func NewAlways(dst string) bm.HandlerFunc

NewAlways is

func NewZoneProxy

func NewZoneProxy(matchZone, dst string) bm.HandlerFunc

NewZoneProxy is

Example

This example create several reverse proxy to show how to use jd proxy middleware. The request will be proxied to destination only when request is from specified datacenter.

package main

import (
	"go-common/library/net/http/blademaster"
	"go-common/library/net/http/blademaster/middleware/proxy"
)

func main() {
	proxies := map[string]string{
		"/index":        "http://api.bilibili.com/html/index",
		"/ping":         "http://api.bilibili.com/api/ping",
		"/api/versions": "http://api.bilibili.com/api/web/versions",
	}

	engine := blademaster.Default()
	// proxy to specified destination
	for path, ep := range proxies {
		engine.GET(path, proxy.NewZoneProxy("sh004", ep), func(ctx *blademaster.Context) {
			ctx.String(200, "Origin")
		})
	}
	// proxy with request path
	ug := engine.Group("/update", proxy.NewZoneProxy("sh004", "http://sh001-api.bilibili.com"))
	ug.POST("/name", func(ctx *blademaster.Context) {
		ctx.String(500, "Should not be accessed")
	})
	ug.POST("/sign", func(ctx *blademaster.Context) {
		ctx.String(500, "Should not be accessed")
	})
	engine.Run(":18080")
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL