-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pranav Kamath KV
authored and
Pranav Kamath KV
committed
Mar 31, 2021
1 parent
e88e95d
commit 1f08218
Showing
9 changed files
with
280 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/prnvkv/my-nats/cmd/config" | ||
"github.com/prnvkv/my-nats/pkg/request-reply/pub" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func init() { | ||
config.LoadConfig() | ||
viper.BindPFlags(pflag.CommandLine) | ||
viper.AutomaticEnv() | ||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
// viper.SetConfigName("config") | ||
// viper.SetConfigType("yaml") | ||
// viper.AddConfigPath("../../../deploy/") | ||
viper.AddConfigPath(viper.GetString("config.source")) | ||
|
||
if len(viper.GetString("config.file")) != 0 { | ||
err := viper.ReadInConfig() | ||
if err != nil { | ||
log.Errorf("Error cannot read config file : %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
func main() { | ||
msg := "Hello world" | ||
log.Infof("MAIN: message: '%s'", msg) | ||
subjectName := viper.GetString("nats.subject.dns") | ||
|
||
response, err := pub.Publish(subjectName, msg) | ||
if err != nil { | ||
log.Errorf("ERROR: %s", err.Error()) | ||
return | ||
} | ||
|
||
log.Infoln("Received the message: ", string(response)) | ||
runtime.Goexit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/prnvkv/my-nats/cmd/config" | ||
"github.com/prnvkv/my-nats/pkg/queue-group/sub" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// func init() { | ||
// pflag.Parse() | ||
// viper.BindPFlags(pflag.CommandLine) | ||
// viper.SetConfigName("config") | ||
// viper.SetConfigType("yaml") | ||
// viper.AddConfigPath("../../../deploy/") | ||
// viper.AddConfigPath(viper.GetString("config.source")) | ||
// viper.AutomaticEnv() | ||
// viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
|
||
// err := viper.ReadInConfig() | ||
// if err != nil { | ||
// log.Errorf("Error cannot read config file : %s\n", err.Error()) | ||
// os.Exit(1) | ||
// } | ||
|
||
// } | ||
|
||
func init() { | ||
config.LoadConfig() | ||
viper.BindPFlags(pflag.CommandLine) | ||
viper.AutomaticEnv() | ||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
// viper.SetConfigName("config") | ||
// viper.SetConfigType("yaml") | ||
// viper.AddConfigPath("../../../deploy/") | ||
viper.AddConfigPath(viper.GetString("config.source")) | ||
|
||
if len(viper.GetString("config.file")) != 0 { | ||
err := viper.ReadInConfig() | ||
if err != nil { | ||
log.Errorf("Error cannot read config file : %s\n", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
func main() { | ||
|
||
serverAddr := viper.GetString("nats.server.addr") | ||
serverPort := viper.GetString("nats.server.port") | ||
subjectName := viper.GetString("nats.subject.dns") | ||
queueGroupName := viper.GetString("nats.queue_group.name") | ||
|
||
fmt.Println("Server port and subject...", serverAddr, serverPort, subjectName, queueGroupName) | ||
msg, err := sub.Subscribe(subjectName, queueGroupName) | ||
if err != nil { | ||
log.Errorf("Error: %s", err) | ||
return | ||
} | ||
log.Infof("Recieved the message: %s", msg) | ||
|
||
runtime.Goexit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM golang:1.13.1 AS builder | ||
LABEL stage=server-intermediate | ||
|
||
WORKDIR /go/src/my-nats | ||
COPY . . | ||
RUN go build -o bin/nats-qreq ./cmd/req-qsub/pub/ | ||
|
||
FROM alpine:latest AS runner | ||
RUN mkdir -p /lib64 && \ | ||
ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2 | ||
WORKDIR /usr/local/bin | ||
COPY --from=builder /go/src/my-nats/bin/nats-qreq . | ||
# CMD ["nats-pub"] | ||
ENTRYPOINT ["nats-qreq"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM golang:1.13.1 AS builder | ||
LABEL stage=server-intermediate | ||
|
||
WORKDIR /go/src/my-nats | ||
COPY . . | ||
RUN go build -o bin/nats-qreqsub ./cmd/req-qsub/sub/ | ||
|
||
FROM alpine:latest AS runner | ||
RUN mkdir -p /lib64 && \ | ||
ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2 | ||
WORKDIR /usr/local/bin | ||
COPY --from=builder /go/src/my-nats/bin/nats-qreqsub . | ||
# CMD ["nats-pub"] | ||
ENTRYPOINT ["nats-qreqsub"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pub | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func Publish(subject string, message interface{}) ([]byte, error) { | ||
serverAddr := viper.GetString("nats.server.addr") | ||
serverPort := viper.GetString("nats.server.port") | ||
var natsConnection = "nats://" + serverAddr + ":" + serverPort | ||
|
||
log.Infof("Connecting the nats server: %s with subject %s\n", natsConnection, subject) | ||
nc, err := nats.Connect(natsConnection) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer nc.Close() | ||
|
||
var buf bytes.Buffer | ||
enc := gob.NewEncoder(&buf) | ||
err = enc.Encode(message) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Infof("Publishing the message to the subject: '%s'", subject) | ||
|
||
response, err := nc.Request(subject, buf.Bytes(), 1*time.Second) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response.Data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package sub | ||
|
||
import ( | ||
"reflect" | ||
"runtime" | ||
|
||
"github.com/nats-io/nats.go" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func Subscribe(subject string, queueGroupName string) ([]byte, error) { | ||
serverAddr := viper.GetString("nats.server.addr") | ||
serverPort := viper.GetString("nats.server.port") | ||
// subjectName := viper.GetString("nats.subject.dns") | ||
|
||
natsConnection := "nats://" + serverAddr + ":" + serverPort | ||
|
||
log.Infof("Subscriber connecting to nats messaging server: %s with subject %s Queue Group: %s\n", natsConnection, subject, queueGroupName) | ||
nc, err := nats.Connect(natsConnection) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// defer nc.Close() | ||
|
||
log.Infof("Consuming the message from the topic: %s\n", subject) | ||
var receivedMsg []byte | ||
nc.QueueSubscribe(subject, queueGroupName, func(m *nats.Msg) { | ||
receivedMsg = m.Data | ||
// err = m.Respond([]byte("success")) | ||
err = nc.Publish(m.Reply, []byte("success")) | ||
// err = m.Ack() | ||
if err != nil { | ||
log.Errorf("Error while ack : %s \n", err.Error()) | ||
return | ||
} | ||
log.Printf("[Received] %s\n", receivedMsg) | ||
}) | ||
|
||
if err != nil { | ||
log.Errorf("Error man!! \n ") | ||
return nil, err | ||
} | ||
|
||
if len(receivedMsg) == 0 || reflect.ValueOf(receivedMsg).Kind() == reflect.Ptr && reflect.ValueOf(receivedMsg).IsNil() { | ||
runtime.Goexit() | ||
} | ||
|
||
return receivedMsg, nil | ||
|
||
} |