Skip to content

Commit b84897b

Browse files
committed
Added azure blob and http
1 parent d93cb24 commit b84897b

File tree

6 files changed

+69
-38
lines changed

6 files changed

+69
-38
lines changed

build/newsletter.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ FROM node:21
1515
WORKDIR /app
1616

1717
# Copy the certificates from the certs stage
18-
COPY --from=certs /app .
18+
# COPY --from=certs /app .
1919

2020
# Download and install dependencies
2121
COPY services/newsletter/package*.json ./

helm/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ services:
8787
USERS_SERVICE_API: https://users-service:7001
8888
PRODUCTS_SERVICE_API: https://products-service:7002
8989
CHECKOUT_SERVICE_API: https://checkout-service:7005
90-
NEWSLETTER_SERVICE_API: https://newsletter-service:7003
90+
NEWSLETTER_SERVICE_API: http://newsletter-service:7003

services/frontend/routes/newsletter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from fastapi.responses import HTMLResponse
55
from fastapi.templating import Jinja2Templates
66

7-
NEWSLETTER_SERVICE_API = os.environ.get('NEWSLETTER_SERVICE_API') or 'https://localhost:7003'
7+
NEWSLETTER_SERVICE_API = os.environ.get('NEWSLETTER_SERVICE_API') or 'http://localhost:7003'
88
router = APIRouter()
99

1010
templates = Jinja2Templates(directory="templates")

services/newsletter/index.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,28 @@ app.post('/subscribe', async (req, res) => {
6060
try {
6161
// Create a new subscriber in the database
6262
await Subscriber.create({ email });
63-
64-
// Mock sending email
65-
await fetch(`https://otterize.com`,{
66-
method: 'POST',
67-
body: JSON.stringify({ email }),
68-
headers: {'Content-Type': 'application/json'}
69-
});
70-
71-
return res.status(201).json({ message: 'Subscribed successfully' });
7263
} catch (error) {
73-
if (error.name === 'SequelizeUniqueConstraintError') {
74-
return res.status(201).json({ message: 'Subscribed successfully' });
64+
if (error.name !== 'SequelizeUniqueConstraintError') {
65+
console.log(error)
66+
return res.status(500).json({ error: 'Failed to subscribe' });
7567
}
76-
77-
console.log(error)
78-
return res.status(500).json({ error: 'Failed to subscribe' });
7968
}
69+
70+
console.log(`Sending welcome email to ${email}`);
71+
72+
// Mock sending email - over HTTP
73+
await fetch(`http://example.com`,{
74+
method: 'POST',
75+
body: JSON.stringify({ email }),
76+
headers: {'Content-Type': 'application/json'}
77+
});
78+
79+
return res.status(201).json({ message: 'Subscribed successfully' });
8080
});
8181

8282
// Certificate paths
8383
const certPath = path.join(__dirname, 'server.crt');
8484
const keyPath = path.join(__dirname, 'server.key');
85-
console.log(certPath);
8685

8786
// Start the server
8887
if (fs.existsSync(certPath) && fs.existsSync(keyPath)) {

services/products/go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ module otterize.com/microservices/products
33
go 1.23.1
44

55
require (
6+
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.5.0
7+
github.com/aws/aws-sdk-go-v2 v1.32.2
68
github.com/aws/aws-sdk-go-v2/config v1.28.0
79
github.com/aws/aws-sdk-go-v2/service/s3 v1.66.0
810
github.com/gorilla/mux v1.8.1
911
)
1012

1113
require (
12-
github.com/aws/aws-sdk-go-v2 v1.32.2 // indirect
14+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 // indirect
15+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
1316
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.6 // indirect
1417
github.com/aws/aws-sdk-go-v2/credentials v1.17.41 // indirect
1518
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.17 // indirect
@@ -25,4 +28,6 @@ require (
2528
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.2 // indirect
2629
github.com/aws/aws-sdk-go-v2/service/sts v1.32.2 // indirect
2730
github.com/aws/smithy-go v1.22.0 // indirect
31+
golang.org/x/net v0.29.0 // indirect
32+
golang.org/x/text v0.19.0 // indirect
2833
)

services/products/main.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"sync"
1717
"time"
1818

19+
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
1920
"github.com/aws/aws-sdk-go-v2/config"
2021
)
2122

@@ -42,33 +43,53 @@ func getEnv(key, fallback string) string {
4243
return fallback
4344
}
4445

46+
func readAzureBlob(client *azblob.Client) ([]byte, error) {
47+
log.Printf("Loading products from Azure container: %s\n", BucketName)
48+
49+
// Download the blob content
50+
resp, err := client.DownloadStream(context.TODO(), BucketName, ObjectKey, nil)
51+
if err != nil {
52+
return nil, fmt.Errorf("failed to download blob: %w", err)
53+
}
54+
defer resp.Body.Close()
55+
56+
return ioutil.ReadAll(resp.Body)
57+
}
58+
59+
func readS3Object(client *s3.Client) ([]byte, error) {
60+
log.Printf("Loading products from S3 bucket: %s\n", BucketName)
61+
62+
input := &s3.GetObjectInput{
63+
Bucket: aws.String(BucketName),
64+
Key: aws.String(ObjectKey),
65+
}
66+
67+
result, err := client.GetObject(context.TODO(), input)
68+
if err != nil {
69+
return nil, fmt.Errorf("Failed to get object from S3: %v\n", err)
70+
}
71+
defer result.Body.Close()
72+
73+
return ioutil.ReadAll(result.Body)
74+
}
75+
4576
// Load products from the JSON file
46-
func loadProducts(client *s3.Client) {
77+
func loadProducts(s3Client *s3.Client, blobClient *azblob.Client) {
4778
mu.Lock()
4879
defer mu.Unlock()
4980

5081
var data []byte
5182
var err error
5283

5384
if BucketEnabled == "true" {
54-
log.Printf("Loading products from S3 bucket: %s\n", BucketName)
55-
56-
input := &s3.GetObjectInput{
57-
Bucket: aws.String(BucketName),
58-
Key: aws.String(ObjectKey),
59-
}
60-
61-
result, err := client.GetObject(context.TODO(), input)
85+
data, err = readS3Object(s3Client)
6286
if err != nil {
63-
log.Printf("Failed to get object from S3: %v\n", err)
64-
return
87+
log.Fatalf("Failed to read aws products.json: %v", err)
6588
}
66-
defer result.Body.Close()
6789

68-
data, err = ioutil.ReadAll(result.Body)
90+
data, err = readAzureBlob(blobClient)
6991
if err != nil {
70-
log.Printf("Failed to read S3 object body: %v\n", err)
71-
return
92+
log.Fatalf("Failed to read azure products.json: %v", err)
7293
}
7394
} else {
7495
log.Printf("Loading products from filesystem: %s\n", ObjectKey)
@@ -84,12 +105,12 @@ func loadProducts(client *s3.Client) {
84105
}
85106
}
86107

87-
func reloadProductData(client *s3.Client) {
108+
func reloadProductData(client *s3.Client, blobClient *azblob.Client) {
88109
ticker := time.NewTicker(10 * time.Second)
89110
defer ticker.Stop()
90111

91112
for range ticker.C {
92-
loadProducts(client)
113+
loadProducts(client, blobClient)
93114
}
94115
}
95116

@@ -133,9 +154,15 @@ func main() {
133154
}
134155
s3Client := s3.NewFromConfig(cfg)
135156

157+
// Initialize Azure SDK for Blob Storage
158+
blobClient, err := azblob.NewClientWithNoCredential("https://ottersidestorage.blob.core.windows.net", nil)
159+
if err != nil {
160+
log.Fatalf("Unable to load AWS SDK config: %v", err)
161+
}
162+
136163
// Load products
137-
loadProducts(s3Client)
138-
go reloadProductData(s3Client)
164+
loadProducts(s3Client, blobClient)
165+
go reloadProductData(s3Client, blobClient)
139166

140167
// Set up the router
141168
r := mux.NewRouter()

0 commit comments

Comments
 (0)