1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/FabianWilms/GoReadableID/readableId"
"github.com/google/uuid"
"github.com/gosimple/slug"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/micro"
)
// 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() {
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{}
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))
}
opts = append(opts, nats.Name("ids"))
nc, err := nats.Connect(natsURL, opts...)
if err != nil {
log.Fatal("Error connecting to NATS:", err)
}
defer nc.Close()
log.Println("Connected to NATS")
versionString := "0.0.0"
if v, exists := os.LookupEnv("VERSION"); exists {
versionString = v
}
srv, err := micro.AddService(nc, micro.Config{
Name: "ids",
Version: versionString,
})
if err != nil {
log.Fatalln("Could not create service", err)
}
err = srv.AddEndpoint("uuid", micro.HandlerFunc(func(req micro.Request) {
req.Respond([]byte(uuid.New().String()))
}))
if err != nil {
log.Fatalln("Could not create uuid endpoint", err)
}
err = srv.AddEndpoint("readable-id", micro.HandlerFunc(func(req micro.Request) {
req.Respond([]byte(readableId.GetRandomID()))
}))
if err != nil {
log.Fatalln("Could not create readable-id endpoint", err)
}
err = srv.AddEndpoint("slugify", micro.HandlerFunc(func(req micro.Request) {
req.Respond([]byte(slug.Make(string(req.Data()))))
}))
if err != nil {
log.Fatalln("Could not create slugify endpoint", err)
}
select {}
}
|