mirror of
https://git.robbyzambito.me/http-nats-proxy
synced 2025-12-21 08:44:50 +00:00
Passed to o3
this removed a bunch of things i wanted though??
This commit is contained in:
85
main.go
85
main.go
@@ -1,27 +1,66 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// printHelp outputs the usage information.
|
||||||
|
func printHelp() {
|
||||||
|
fmt.Println("Usage: HTTP-to-NATS proxy server")
|
||||||
|
fmt.Println("\nThe following environment variables are supported:")
|
||||||
|
fmt.Println(" NATS_URL - NATS connection URL (default: nats://127.0.0.1:4222)")
|
||||||
|
fmt.Println(" NATS_USER - NATS username for authentication (optional)")
|
||||||
|
fmt.Println(" NATS_PASSWORD - NATS password for authentication (optional)")
|
||||||
|
fmt.Println(" HTTP_PORT - HTTP port to listen on (default: 8080)")
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Connect to the NATS server
|
helpFlag := flag.Bool("help", false, "Display help information about available environment variables")
|
||||||
nc, err := nats.Connect(nats.DefaultURL)
|
flag.Parse()
|
||||||
|
|
||||||
|
if *helpFlag {
|
||||||
|
printHelp()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read NATS connection info from environment variables
|
||||||
|
natsURL := os.Getenv("NATS_URL")
|
||||||
|
if natsURL == "" {
|
||||||
|
natsURL = nats.DefaultURL // defaults to "nats://127.0.0.1:4222"
|
||||||
|
}
|
||||||
|
natsUser := os.Getenv("NATS_USER")
|
||||||
|
natsPassword := os.Getenv("NATS_PASSWORD")
|
||||||
|
|
||||||
|
// Read HTTP port from environment variables
|
||||||
|
httpPort := os.Getenv("HTTP_PORT")
|
||||||
|
if httpPort == "" {
|
||||||
|
httpPort = "8080"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up NATS connection options if authentication info is provided.
|
||||||
|
var opts []nats.Option
|
||||||
|
if natsUser != "" && natsPassword != "" {
|
||||||
|
opts = append(opts, nats.UserInfo(natsUser, natsPassword))
|
||||||
|
}
|
||||||
|
|
||||||
|
nc, err := nats.Connect(natsURL, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error connecting to NATS:", err)
|
log.Fatal("Error connecting to NATS:", err)
|
||||||
}
|
}
|
||||||
defer nc.Close()
|
defer nc.Close()
|
||||||
|
|
||||||
// HTTP handler to proxy all requests to NATS
|
// HTTP handler function
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Read the entire request body (if any)
|
// Read the entire request body
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Error reading request body", http.StatusInternalServerError)
|
http.Error(w, "Error reading request body", http.StatusInternalServerError)
|
||||||
@@ -29,55 +68,43 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
// Create the NATS subject.
|
// Drop the leading slash, replace remaining slashes with dots to form the subject
|
||||||
// Remove the leading slash from the path and replace remaining slashes with dots.
|
subject := strings.ReplaceAll(strings.TrimPrefix(r.URL.Path, "/"), "/", ".")
|
||||||
// The subject is prefixed with "http.<method>.", where <method> is lower-case.
|
// Prefix the subject with "http." to follow the convention
|
||||||
path := strings.TrimPrefix(r.URL.Path, "/")
|
|
||||||
subjectPath := strings.ReplaceAll(path, "/", ".")
|
|
||||||
subject := fmt.Sprintf("http.%s.%s", strings.ToLower(r.Method), subjectPath)
|
|
||||||
|
|
||||||
log.Println("Forwarding HTTP", r.Method, "request on", r.URL.Path, "to NATS subject:", subject)
|
|
||||||
|
|
||||||
// Create a new NATS message with the HTTP request body
|
|
||||||
msg := nats.Msg{
|
msg := nats.Msg{
|
||||||
Subject: subject,
|
Subject: fmt.Sprintf("http.%s", subject),
|
||||||
Data: body,
|
Data: body,
|
||||||
Header: nats.Header{},
|
Header: nats.Header{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy over all the HTTP request headers to the NATS message headers
|
log.Println("Forwarding request to subject:", msg.Subject)
|
||||||
|
|
||||||
|
// Copy HTTP headers to the NATS message headers
|
||||||
for key, values := range r.Header {
|
for key, values := range r.Header {
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
msg.Header.Add(key, value)
|
msg.Header.Add(key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add additional HTTP meta-data as headers
|
// Send request to NATS and wait for reply (timeout: 30 seconds)
|
||||||
msg.Header.Set("X-HTTP-Method", r.Method)
|
|
||||||
msg.Header.Set("X-HTTP-Path", r.URL.Path)
|
|
||||||
msg.Header.Set("X-HTTP-Query", r.URL.RawQuery)
|
|
||||||
msg.Header.Set("X-Remote-Addr", r.RemoteAddr)
|
|
||||||
|
|
||||||
// Send the NATS request and wait synchronously for a reply (timeout: 30 seconds)
|
|
||||||
reply, err := nc.RequestMsg(&msg, 30*time.Second)
|
reply, err := nc.RequestMsg(&msg, 30*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Error processing request", http.StatusInternalServerError)
|
http.Error(w, "Error processing request", http.StatusInternalServerError)
|
||||||
log.Println("NATS request error:", err)
|
log.Println("Error processing the request:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set any response headers from the NATS reply on the HTTP response
|
// Copy headers from the NATS reply to the HTTP response
|
||||||
for key, values := range reply.Header {
|
for key, values := range reply.Header {
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
w.Header().Add(key, value)
|
w.Header().Add(key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the reply body (from NATS) back to the HTTP client
|
// Write NATS reply body to HTTP response
|
||||||
w.Write(reply.Data)
|
w.Write(reply.Data)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Start the HTTP server
|
fmt.Println("Server is running on http://localhost:" + httpPort)
|
||||||
fmt.Println("Server is running on http://localhost:8080")
|
log.Fatal(http.ListenAndServe(":"+httpPort, nil))
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user