mirror of
https://git.robbyzambito.me/http-nats-proxy
synced 2025-12-20 08:14:51 +00:00
Add client proxy
Add endpoint /proxy ... that can be used to proxy requests to other HTTP sites.
This commit is contained in:
58
main.go
58
main.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
|
"github.com/nats-io/nats.go/micro"
|
||||||
)
|
)
|
||||||
|
|
||||||
// printHelp outputs the usage information.
|
// printHelp outputs the usage information.
|
||||||
@@ -51,6 +53,25 @@ func URLToNATS(urlPath string) (string, error) {
|
|||||||
return strings.Join(segments, "."), nil
|
return strings.Join(segments, "."), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NATS subject to URL conversion
|
||||||
|
func NATSToURL(natsSubject string) (string, error) {
|
||||||
|
tokens := strings.Split(natsSubject, ".")
|
||||||
|
for i, token := range tokens {
|
||||||
|
// Reverse the special character encoding
|
||||||
|
decoded := strings.ReplaceAll(token, "%2E", ".")
|
||||||
|
decoded = strings.ReplaceAll(decoded, "%2A", "*")
|
||||||
|
decoded = strings.ReplaceAll(decoded, "%3E", ">")
|
||||||
|
|
||||||
|
// Unescape remaining URL encoding
|
||||||
|
unescaped, err := url.PathUnescape(decoded)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to unescape token: %w", err)
|
||||||
|
}
|
||||||
|
tokens[i] = unescaped
|
||||||
|
}
|
||||||
|
return "/" + strings.Join(tokens, "/"), nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
helpFlag := flag.Bool("help", false, "Display help information about available environment variables")
|
helpFlag := flag.Bool("help", false, "Display help information about available environment variables")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -188,6 +209,43 @@ func main() {
|
|||||||
w.Write(reply.Data)
|
w.Write(reply.Data)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
_, err = micro.AddService(nc, micro.Config{
|
||||||
|
Name: "http-nats-proxy",
|
||||||
|
Version: "0.1.0",
|
||||||
|
Endpoint: µ.EndpointConfig{
|
||||||
|
Subject: "http.*.*.proxy.>",
|
||||||
|
Handler: micro.HandlerFunc(func(natsReq micro.Request) {
|
||||||
|
// http.host.method.proxy.host.endpoint.>
|
||||||
|
httpPath := natsReq.Headers()["X-HTTP-Path"][0]
|
||||||
|
httpReqURL := fmt.Sprintf("https:/%s", strings.TrimPrefix(httpPath, "/proxy"))
|
||||||
|
httpBody := bytes.NewReader(natsReq.Data())
|
||||||
|
httpMethod := natsReq.Headers()["X-HTTP-Method"][0]
|
||||||
|
|
||||||
|
// Create a new request
|
||||||
|
httpReq, err := http.NewRequest(httpMethod, httpReqURL, httpBody)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(httpReq)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
resBody, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
natsReq.Respond(resBody)
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Could not make NATS microservice:", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Start the HTTP server
|
// Start the HTTP server
|
||||||
fmt.Println("Server is running on http://localhost:8080")
|
fmt.Println("Server is running on http://localhost:8080")
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
|||||||
Reference in New Issue
Block a user