|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "fmt" |
| 6 | + _ "github.com/go-sql-driver/mysql" |
| 7 | + nats "github.com/nats-io/nats.go" |
| 8 | + "math/rand" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func generateAndStoreString() (string, error) { |
| 13 | + // Connect to the database |
| 14 | + db, err := sql.Open("mysql", "root:password@tcp(mysql:3306)/mysql") |
| 15 | + if err != nil { |
| 16 | + return "", err |
| 17 | + } |
| 18 | + defer db.Close() |
| 19 | + |
| 20 | + // Generate a random string |
| 21 | + // Define a string of characters to use |
| 22 | + characters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 23 | + |
| 24 | + // Generate a random string of length 10 |
| 25 | + randomString := make([]byte, 64) |
| 26 | + for i := range randomString { |
| 27 | + randomString[i] = characters[rand.Intn(len(characters))] |
| 28 | + } |
| 29 | + |
| 30 | + // Insert the random number into the database |
| 31 | + _, err = db.Exec("INSERT INTO generator_async(random_string) VALUES(?)", string(randomString)) |
| 32 | + if err != nil { |
| 33 | + return "", err |
| 34 | + } |
| 35 | + |
| 36 | + fmt.Printf("Random string %s has been inserted into the database\n", string(randomString)) |
| 37 | + return string(randomString), nil |
| 38 | +} |
| 39 | + |
| 40 | +func main() { |
| 41 | + err := createGeneratordb() |
| 42 | + if err != nil { |
| 43 | + panic(err.Error()) |
| 44 | + } |
| 45 | + |
| 46 | + nc, _ := nats.Connect("nats://my-nats:4222") |
| 47 | + defer nc.Close() |
| 48 | + |
| 49 | + nc.Subscribe("generator", func(msg *nats.Msg) { |
| 50 | + s, err := generateAndStoreString() |
| 51 | + if err != nil { |
| 52 | + print(err) |
| 53 | + } |
| 54 | + nc.Publish("generator_reply", []byte(s)) |
| 55 | + nc.Publish("confirmation", []byte(s)) |
| 56 | + }) |
| 57 | + |
| 58 | + nc.Subscribe("confirmation_reply", func(msg *nats.Msg) { |
| 59 | + stringReceived(string(msg.Data)) |
| 60 | + }) |
| 61 | + // Subscribe to the queue |
| 62 | + // when a message comes in call generateAndStoreString() then put the string on the |
| 63 | + // reply queue. also add a message onto the confirmation queue |
| 64 | + |
| 65 | + // subscribe to the confirmation reply queue |
| 66 | + // when a message comes in call |
| 67 | + |
| 68 | + for { |
| 69 | + time.Sleep(1 * time.Second) |
| 70 | + } |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +func createGeneratordb() error { |
| 75 | + db, err := sql.Open("mysql", "root:password@tcp(mysql:3306)/mysql") |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + defer db.Close() |
| 80 | + |
| 81 | + // try to create a table for us |
| 82 | + _, err = db.Exec("CREATE TABLE IF NOT EXISTS generator_async(random_string VARCHAR(100), seen BOOLEAN, requested BOOLEAN)") |
| 83 | + |
| 84 | + return err |
| 85 | +} |
| 86 | + |
| 87 | +func stringReceived(input string) { |
| 88 | + |
| 89 | + // Connect to the database |
| 90 | + db, err := sql.Open("mysql", "root:password@tcp(mysql:3306)/mysql") |
| 91 | + if err != nil { |
| 92 | + print(err) |
| 93 | + } |
| 94 | + defer db.Close() |
| 95 | + |
| 96 | + _, err = db.Exec("UPDATE generator_async SET requested = true WHERE random_string = ?", input) |
| 97 | + if err != nil { |
| 98 | + print(err) |
| 99 | + } |
| 100 | +} |
0 commit comments