diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-03-02 15:53:19 -0500 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-03-02 16:03:18 -0500 |
commit | 55f0a026cbfa4d0a5681f8cb7919e623382a139e (patch) | |
tree | b93c32aa4f7d5b0229f5ff2e06e9b58da71cb2af | |
parent | ac7fca9281c64412b5d4f0c7e718e87945dd28b0 (diff) |
Added changes for remote deployment
Added example env files
Added justfile changes for deployment
Added changes in main to support auth methods
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | deploy_host.example.env | 2 | ||||
-rw-r--r-- | dev_host.example.env | 2 | ||||
-rw-r--r-- | justfile | 17 | ||||
-rw-r--r-- | main.go | 51 |
5 files changed, 69 insertions, 5 deletions
@@ -1,3 +1,3 @@ *~ bin/ - +.env diff --git a/deploy_host.example.env b/deploy_host.example.env new file mode 100644 index 0000000..ad9ec22 --- /dev/null +++ b/deploy_host.example.env @@ -0,0 +1,2 @@ +NATS_CREDS_FILE=/root/bsky-proxy.creds +NATS_URL=tls://apex.zambito.xyz
\ No newline at end of file diff --git a/dev_host.example.env b/dev_host.example.env new file mode 100644 index 0000000..a11ab63 --- /dev/null +++ b/dev_host.example.env @@ -0,0 +1,2 @@ +DEPLOY_HOST=root@alpha.apex +DEPLOY_DIR=src/bsky-nats-proxy
\ No newline at end of file @@ -1,8 +1,23 @@ +set dotenv-load := true + @default: just --list +clean: + rm -rf bin/ + run: build bin/bluesky-nats-proxy build: - go build -o bin/bluesky-nats-proxy
\ No newline at end of file + go build -o bin/bluesky-nats-proxy + +deploy: + ssh -t {{env('DEPLOY_HOST')}} "\ + cd {{env('DEPLOY_DIR')}} && \ + git pull -f --rebase && \ + just clean build run-daemon" + +run-daemon: + pkill bluesky-nats-proxy || true + ./bin/bluesky-nats &>> /var/log/bluesky-nats-proxy.log &
\ No newline at end of file @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "flag" "fmt" "log" "log/slog" @@ -18,11 +19,55 @@ import ( "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(" NATS_TOKEN - NATS token for authentication (optional)") + fmt.Println(" NATS_NKEY - NATS NKEY for authentication (optional)") + fmt.Println(" NATS_NKEY_SEED - NATS NKEY seed for authentication (optional)") + fmt.Println(" NATS_CREDS_FILE - Path to NATS credentials file (optional)") +} + func main() { - // Connect to NATS - nc, err := nats.Connect(nats.DefaultURL) + helpFlag := flag.Bool("help", false, "Display help information about available environment variables") + 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") + natsToken := os.Getenv("NATS_TOKEN") + natsNkey := os.Getenv("NATS_NKEY") + natsNkeySeed := os.Getenv("NATS_NKEY_SEED") + natsCredsFile := os.Getenv("NATS_CREDS_FILE") + + // Set up NATS connection options + opts := []nats.Option{nats.Name("Web proxy")} + + if natsUser != "" && natsPassword != "" { + opts = append(opts, nats.UserInfo(natsUser, natsPassword)) + } else if natsToken != "" { + opts = append(opts, nats.Token(natsToken)) + } else if natsNkey != "" && natsNkeySeed != "" { + log.Fatalln("NKEY connection not supported") + } else if natsCredsFile != "" { + opts = append(opts, nats.UserCredentials(natsCredsFile)) + } + + nc, err := nats.Connect(natsURL, opts...) if err != nil { - log.Fatalf("Error connecting to NATS: %v", err) + log.Fatal("Error connecting to NATS:", err) } defer nc.Close() log.Println("Connected to NATS") |