Kmux:简约的用代码定义代理Proxy


kmux 是一个强大的Go软件包,通过允许开发人员将代理定义为代码,改变代理的实现方式。

通过提供直观且富有表现力的语法,kmux 简化了服务器设置、路由管理和代理配置。

借助 kmux,开发人员可以创建强大且灵活且易于维护的代理解决方案:

go get -u github.com/kamalshkeir/kmux@v1.91.4

package main

import (
    "fmt"
    
"net/http"

    
"github.com/kamalshkeir/kmux"
)

func main() {
    app := kmux.New()
    app.Use(func(h http.Handler) http.Handler {
        return kmux.Handler(func(c *kmux.Context) {
            fmt.Println(
"PROXY:", c.Request.Host+c.Request.URL.Path)
            h.ServeHTTP(c.ResponseWriter, c.Request)
        })
    })

    nc := app.ReverseProxy(
"nc.localhost", "http://localhost:9313") // http://nc.localhost:9999
    nc.Use(func(h http.Handler) http.Handler {
        return kmux.Handler(func(c *kmux.Context) {
            fmt.Println(
"NC APP:", c.Request.Host+c.Request.URL.Path)
        })
    })

    cv := app.ReverseProxy(
"dev.localhost", "https://kamalshkeir.dev") // http://dev.localhost:9999
    cv.Use(func(h http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            fmt.Println(
"PORTFOLIO:", r.Host+r.URL.Path)
        })
    })

    app.Run(
":9999")
}