feat: initial commit

This commit is contained in:
2023-04-24 19:53:37 +02:00
commit 6854557831
13 changed files with 701 additions and 0 deletions

3
examples/README.md Normal file
View File

@ -0,0 +1,3 @@
# Examples
- [`basic_rewrite/`](./basic_rewrite/) - Use static host matching rules to proxy requests to remote servers

View File

@ -0,0 +1,12 @@
# Example: Basic rewriting
This example use the [`WithRewriteHosts()`](../middleware/rewrite_hosts.go) middleware to rewrite incoming requests based on static host matching rules.
# How to run
Run with `go run ./examples/basic_rewrite` then open your browser on theses 2 URLs:
- http://127.0.0.1:3000
- http://localhost:3000
You'll see that the two URLs are targeting two differents remote servers.

View File

@ -0,0 +1,40 @@
package main
import (
"log"
"net/http"
"net/url"
"forge.cadoles.com/Cadoles/go-proxy"
"forge.cadoles.com/Cadoles/go-proxy/middleware"
)
func main() {
// We declare our url rewritting mappings
mappings := map[string]*url.URL{
"127.0.0.1:*": mustParse("https://gitea.com"),
"localhost:*": mustParse("https://www.cadoles.com"),
}
// We create a new proxy with our mappings
proxy := proxy.New(
middleware.WithRewriteHosts(mappings),
)
// We start a HTTP server, listening on all interfaces with our proxy
// as an handler
if err := http.ListenAndServe("127.0.0.1:3000", proxy); err != nil {
log.Fatalf("%v", err)
}
}
// Simple utility function to transform raw urls
// to *url.URL
func mustParse(rawURL string) *url.URL {
url, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
return url
}