feat(mtp): stomp server

This commit is contained in:
leandrofars 2023-10-27 01:47:53 -03:00
parent f6d53f8e73
commit 64f0d5a088
6 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,65 @@
package stomp
import (
"log"
"os"
"time"
"github.com/go-stomp/stomp"
)
type Stomp struct {
Addr string
Conn *stomp.Conn
StopConn os.Signal
Connected bool
Username string
Password string
}
func (s *Stomp) Connect() {
log.Println("STOMP username:", s.Username)
log.Println("STOMP password:", s.Password)
var options []func(*stomp.Conn) error = []func(*stomp.Conn) error{
stomp.ConnOpt.Login(s.Username, s.Password),
stomp.ConnOpt.Host("/"),
}
const MAX_TRIES = 3
for i := 0; i < MAX_TRIES; i++ {
log.Println("Starting new STOMP client")
stompConn, err := stomp.Dial("tcp", s.Addr, options...)
if err != nil {
log.Println("Error connecting to STOMP server:", err.Error())
if i == MAX_TRIES-1 {
log.Printf("Reached max tries count: %d, stop trying to connect", MAX_TRIES)
return
}
time.Sleep(1 * time.Second)
continue
}
s.Conn = stompConn
s.Connected = true
break
}
log.Println("Connected to STOMP broker-->", s.Addr)
}
func (s *Stomp) Disconnect() {
if s.Connected {
s.Conn.Disconnect()
}
return
}
func (s *Stomp) Publish(msg []byte, topic, respTopic string, retain bool) {
//s.Conn.Send()
}
func (s *Stomp) Subscribe() {
//s.Conn.Subscribe()
}

View File

@ -0,0 +1,3 @@
STOMP_USERNAME=""
STOMP_PASSWORD=""
# if both variables above are empty, the STOMP server will run without auth

1
backend/services/stomp/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env.local

View File

@ -0,0 +1,66 @@
package main
import (
"log"
"net"
"os"
stomp_server "github.com/go-stomp/stomp/server"
"github.com/joho/godotenv"
)
type Credentials struct {
Login string
Passwd string
}
func (c *Credentials) Authenticate(login, passwd string) bool {
if c.Login == "" && c.Passwd == "" {
log.Println("NEW CLIENT AUTH: Server is running with no auth")
return true
}
if login != c.Login || passwd != c.Passwd {
log.Println("NEW CLIENT AUTH: Invalid Credentials")
return false
}
log.Println("NEW CLIENT AUTH: OK")
return true
}
func main() {
err := godotenv.Load()
localEnv := ".env.local"
if _, err := os.Stat(localEnv); err == nil {
_ = godotenv.Overload(localEnv)
log.Println("Loaded variables from '.env.local'")
} else {
log.Println("Loaded variables from '.env'")
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
creds := Credentials{
Login: os.Getenv("STOMP_USERNAME"),
Passwd: os.Getenv("STOMP_PASSWORD"),
}
l, err := net.Listen("tcp", stomp_server.DefaultAddr)
if err != nil {
log.Println("Error to open tcp port: ", err)
}
s := stomp_server.Server{
Addr: stomp_server.DefaultAddr,
HeartBeat: stomp_server.DefaultHeartBeat,
Authenticator: &creds,
}
log.Println("Started STOMP server at port", s.Addr)
err = s.Serve(l)
if err != nil {
log.Println("Error to start stomp server: ", err)
}
}

View File

@ -0,0 +1,8 @@
module github.com/leandrofars/stomp
go 1.21.3
require (
github.com/go-stomp/stomp v2.1.4+incompatible // indirect
github.com/joho/godotenv v1.5.1 // indirect
)

View File

@ -0,0 +1,4 @@
github.com/go-stomp/stomp v2.1.4+incompatible h1:D3SheUVDOz9RsjVWkoh/1iCOwD0qWjyeTZMUZ0EXg2Y=
github.com/go-stomp/stomp v2.1.4+incompatible/go.mod h1:VqCtqNZv1226A1/79yh+rMiFUcfY3R109np+7ke4n0c=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=