feat: initial commit
This commit is contained in:
3
examples/README.md
Normal file
3
examples/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Examples
|
||||
|
||||
- [`basic_rewrite/`](./basic_rewrite/) - Use static host matching rules to proxy requests to remote servers
|
12
examples/basic_rewrite/README.md
Normal file
12
examples/basic_rewrite/README.md
Normal 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.
|
40
examples/basic_rewrite/main.go
Normal file
40
examples/basic_rewrite/main.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user