Skip to content

Commit 99a2e75

Browse files
authored
Merge pull request #18 from yaninyzwitty/develop
refactor, to work with fargate
2 parents 44dfd81 + 0a6934f commit 99a2e75

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ database:
77
ssl_mode: require
88
pulsar:
99
uri: pulsar+ssl://pulsar-aws-eucentral1.streaming.datastax.com:6651
10-
topic_name: persistent://witty-tenant/default/products_topic
10+
topic_name: persistent://witty-cluster/default/products_topic
1111
token: some_token
1212

1313

helpers/env.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package helpers
2+
3+
import "os"
4+
5+
func GetEnvOrDefault(key, defaultValue string) string {
6+
if value := os.Getenv(key); value != "" {
7+
return value
8+
}
9+
return defaultValue
10+
}

main.go

+24-31
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,46 @@ import (
1010
"syscall"
1111
"time"
1212

13+
"github.com/joho/godotenv"
1314
"github.com/yaninyzwitty/pulsar-outbox-products-service/controller"
1415
"github.com/yaninyzwitty/pulsar-outbox-products-service/database"
1516
"github.com/yaninyzwitty/pulsar-outbox-products-service/helpers"
16-
"github.com/yaninyzwitty/pulsar-outbox-products-service/pkg"
1717
"github.com/yaninyzwitty/pulsar-outbox-products-service/pulsar"
1818
"github.com/yaninyzwitty/pulsar-outbox-products-service/router"
1919
"github.com/yaninyzwitty/pulsar-outbox-products-service/sonyflake"
2020
)
2121

2222
var (
23-
cfg pkg.Config
24-
password string
25-
token string
23+
password string
24+
token string
25+
host = "ep-dark-unit-a2z9twbi-pooler.eu-central-1.aws.neon.tech"
26+
dbName = "neondb"
27+
SSLMode = "require"
28+
pulsarURI = "pulsar+ssl://pulsar-aws-eucentral1.streaming.datastax.com:6651"
29+
TopicName = "persistent://witty-cluster/default/products_topic"
30+
port = 3000
2631
)
2732

2833
func main() {
29-
// Load configuration
30-
file, err := os.Open("my_config.yaml")
31-
if err != nil {
32-
slog.Error("failed to open config.yaml", "error", err)
34+
// Load environment variables
35+
if err := godotenv.Load(); err != nil {
36+
slog.Error("failed to load .env", "error", err)
3337
os.Exit(1)
3438
}
35-
defer file.Close()
36-
37-
if err := cfg.LoadConfig(file); err != nil {
38-
slog.Error("failed to load config", "error", err)
39-
os.Exit(1)
40-
}
41-
42-
// if err := godotenv.Load(); err != nil {
43-
// slog.Error("failed to load .env", "error", err)
44-
// os.Exit(1)
45-
// }
46-
password = os.Getenv("DB_PASSWORD")
47-
token = os.Getenv("PULSAR_TOKEN")
39+
password = helpers.GetEnvOrDefault("DB_PASSWORD", "")
40+
token = helpers.GetEnvOrDefault("PULSAR_TOKEN", "")
4841

4942
// Initialize dependencies
5043
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
5144
defer cancel()
5245

5346
dbConfig := database.DbConfig{
54-
Host: cfg.Database.Host,
47+
Host: host,
5548
Port: 5432,
56-
User: cfg.Database.User,
49+
User: "neondb_owner",
5750
Password: password,
58-
DbName: cfg.Database.Database,
59-
SSLMode: cfg.Database.SSLMode,
51+
DbName: dbName, // Fixed typo (dnName → dbName)
52+
SSLMode: SSLMode,
6053
MaxConn: 500,
6154
}
6255

@@ -78,9 +71,9 @@ func main() {
7871
}
7972

8073
pulsarCfg := pulsar.PulsarConfig{
81-
URI: cfg.Pulsar.URI,
74+
URI: pulsarURI, // Used hardcoded URI instead of uninitialized cfg.Pulsar.URI
8275
Token: token,
83-
TopicName: cfg.Pulsar.TopicName,
76+
TopicName: TopicName, // Used hardcoded topic instead of uninitialized cfg.Pulsar.TopicName
8477
}
8578

8679
pulsarClient, err := pulsarCfg.CreatePulsarConnection(ctx)
@@ -98,19 +91,19 @@ func main() {
9891
defer producer.Close()
9992

10093
productController := controller.NewProductsController(pool)
101-
router := router.NewRouter(productController)
94+
appRouter := router.NewRouter(productController) // Renamed to avoid conflict
10295

10396
// Start the server
10497
server := &http.Server{
105-
Addr: fmt.Sprintf(":%d", cfg.Server.Port),
106-
Handler: router,
98+
Addr: fmt.Sprintf(":%d", port), // This assumes port is set elsewhere
99+
Handler: appRouter,
107100
}
108101

109102
stopCh := make(chan os.Signal, 1)
110103
signal.Notify(stopCh, os.Interrupt, syscall.SIGTERM)
111104

112105
go func() {
113-
slog.Info("SERVER starting", "port", cfg.Server.Port)
106+
slog.Info("SERVER starting", "port", port)
114107
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
115108
slog.Error("server error", "error", err)
116109
os.Exit(1)

0 commit comments

Comments
 (0)