Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rabbitmq connection #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*.log
.log
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# learn-pub-sub-starter (Peril)

This is the starter code used in Boot.dev's [Learn Pub/Sub](https://learn.boot.dev/learn-pub-sub) course.
42 changes: 40 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
package main

import "fmt"
import (
"fmt"
"log"
"os"
"os/signal"

"github.com/imhasandl/pub-sub-starter/internal/pubsub"
"github.com/imhasandl/pub-sub-starter/internal/routing"
amqp "github.com/rabbitmq/amqp091-go"
)

func main() {
fmt.Println("Starting Peril server...")
const rabbitmqConnection = "amqp://guest:guest@localhost:5672/"

conn, err := amqp.Dial(rabbitmqConnection)
if err != nil {
log.Fatalf("could not connect to RabbitMQ: %v", err)
}
defer conn.Close()
fmt.Println("Peril game server connected to RabbitMQ!")

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt)
<-signalChan

publishCh, err := conn.Channel()
if err != nil {
log.Fatalf("could not create channel: %v", err)
}

err = pubsub.PublishJSON(
publishCh,
routing.ExchangePerilDirect,
routing.PauseKey,
routing.PlayingState{
IsPaused: true,
},
)
if err != nil {
log.Printf("could not publish time: %v", err)
}
fmt.Println("Pause message sent!")
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/bootdotdev/learn-pub-sub-starter
module github.com/imhasandl/pub-sub-starter

go 1.22.1

require github.com/rabbitmq/amqp091-go v1.10.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
2 changes: 1 addition & 1 deletion internal/gamelogic/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"time"

"github.com/bootdotdev/learn-pub-sub-starter/internal/routing"
"github.com/imhasandl/pub-sub-starter/internal/routing"
)

const logsFile = "game.log"
Expand Down
2 changes: 1 addition & 1 deletion internal/gamelogic/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gamelogic
import (
"fmt"

"github.com/bootdotdev/learn-pub-sub-starter/internal/routing"
"github.com/imhasandl/pub-sub-starter/internal/routing"
)

func (gs *GameState) HandlePause(ps routing.PlayingState) {
Expand Down
30 changes: 30 additions & 0 deletions internal/pubsub/publish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package pubsub

import (
"context"
"encoding/json"

amqp "github.com/rabbitmq/amqp091-go"
)


func PublishJSON[T any](ch *amqp.Channel, exchange, key string, val T) error {
dat, err := json.Marshal(val)
if err != nil {
return err
}

err = ch.PublishWithContext(
context.Background(),
exchange,
key,
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: dat,
},
)

return err
}