Skip to content

Commit bd8b0f4

Browse files
author
cemayan
committed
redis-manager and read-api are added.
1 parent dc7142f commit bd8b0f4

File tree

23 files changed

+290
-19
lines changed

23 files changed

+290
-19
lines changed

cmd/api/read/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package main
33
import (
44
"github.com/cemayan/url-shortener/config/api"
55
"github.com/cemayan/url-shortener/internal/api/read/adapter/database"
6+
"github.com/cemayan/url-shortener/internal/api/read/application/router"
67
"github.com/cemayan/url-shortener/managers/db"
8+
"github.com/gofiber/fiber/v2"
9+
"github.com/gofiber/fiber/v2/middleware/cors"
710
"github.com/sirupsen/logrus"
811
"github.com/spf13/viper"
912
"os"
@@ -43,5 +46,15 @@ func init() {
4346
}
4447

4548
func main() {
49+
app := fiber.New()
50+
app.Use(cors.New())
4651

52+
router.SetupRoutes(app, configs, _log.WithFields(logrus.Fields{"service": "read-api"}))
53+
54+
v.WatchConfig()
55+
56+
err := app.Listen(":8081")
57+
if err != nil {
58+
return
59+
}
4760
}

cmd/event_handler/main.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package main
22

33
import (
4-
"github.com/apache/pulsar-client-go/pulsar"
54
pulsar_handler "github.com/cemayan/url-shortener/common/adapters/pulsar"
5+
"github.com/cemayan/url-shortener/common/adapters/redis"
66
"github.com/cemayan/url-shortener/common/ports/output"
77
"github.com/cemayan/url-shortener/config/event_handler"
88
"github.com/cemayan/url-shortener/internal/event_handler/adapter/database"
99
"github.com/cemayan/url-shortener/internal/event_handler/domain/service"
1010
"github.com/cemayan/url-shortener/internal/event_handler/helper"
11+
"github.com/cemayan/url-shortener/managers/cache"
1112
"github.com/cemayan/url-shortener/managers/db"
1213
"github.com/cemayan/url-shortener/managers/hook"
1314
"github.com/cemayan/url-shortener/managers/mq"
1415
"github.com/sirupsen/logrus"
1516
"github.com/spf13/viper"
1617
"os"
17-
"time"
1818
)
1919

2020
var _log *logrus.Logger
@@ -56,22 +56,18 @@ func init() {
5656

5757
func main() {
5858

59-
client, err := pulsar.NewClient(pulsar.ClientOptions{
60-
URL: configs.Pulsar.Url,
61-
OperationTimeout: 30 * time.Second,
62-
ConnectionTimeout: 30 * time.Second,
63-
})
64-
if err != nil {
65-
_log.WithFields(logrus.Fields{"method": "NewPulsarClient", "message": err.Error()}).Log(logrus.FatalLevel)
66-
return
67-
}
68-
6959
var pulsarManager mq.PulsarManager
70-
pulsarManager = mq.NewPulsarManager(client, configs.Pulsar, _log.WithFields(logrus.Fields{"service": "event-handler"}))
60+
pulsarManager = mq.NewPulsarManager(pulsarManager.New(), configs.Pulsar, _log.WithFields(logrus.Fields{"service": "event-handler"}))
7161

7262
var pulsarPort output.PulsarPort
7363
pulsarPort = pulsar_handler.NewPulsarHandler(pulsarManager, configs.Pulsar, _log.WithFields(logrus.Fields{"service": "event-handler"}))
7464

75-
eventService := service.NewEventService(pulsarPort, configs, _log.WithFields(logrus.Fields{"service": "event-service"}))
65+
var redisManager cache.RedisManager
66+
redisManager = cache.NewRedisManager(configs.Redis, _log.WithFields(logrus.Fields{"service": "cache-manager"}))
67+
68+
var redisPort output.RedisPort
69+
redisPort = redis.NewRedisHandler(redisManager.New(), _log.WithFields(logrus.Fields{"service": "cache-service"}))
70+
71+
eventService := service.NewEventService(redisPort, pulsarPort, configs, _log.WithFields(logrus.Fields{"service": "event-service"}))
7672
eventService.Consume()
7773
}

common/adapters/redis/redis.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package redis
2+
3+
import (
4+
"context"
5+
"github.com/cemayan/url-shortener/common/ports/output"
6+
"github.com/redis/go-redis/v9"
7+
log "github.com/sirupsen/logrus"
8+
"time"
9+
)
10+
11+
var Ctx = context.Background()
12+
13+
// A RedisSvc contains the required dependencies for this service
14+
type RedisSvc struct {
15+
client *redis.Client
16+
log *log.Entry
17+
}
18+
19+
func (r RedisSvc) Remove(key string) error {
20+
//TODO implement me
21+
panic("implement me")
22+
}
23+
24+
// Get is command of Redis's Get
25+
func (r RedisSvc) Get(key string) (string, error) {
26+
return r.client.Get(Ctx, key).Result()
27+
}
28+
29+
// Set is command of Redis's Set
30+
func (r RedisSvc) Set(key string, data string) error {
31+
err := r.client.Set(Ctx, key, data, 30*time.Minute).Err()
32+
if err != nil {
33+
r.log.WithFields(log.Fields{"method": "Set"}).Infof("An error occurred when set value to redis %v", err)
34+
}
35+
return err
36+
}
37+
38+
func NewRedisHandler(client *redis.Client, log *log.Entry) output.RedisPort {
39+
return &RedisSvc{client: client, log: log}
40+
}

common/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ type Cockroach struct {
1616
Name string `json:"name,omitempty"`
1717
}
1818

19+
type Redis struct {
20+
Address string `json:"address,omitempty"`
21+
AddressPort string `json:"addressPort,omitempty"`
22+
}
23+
1924
type Mongo struct {
2025
Uri string `json:"uri,omitempty"`
2126
DbName string `json:"dbName,omitempty"`

common/ports/output/redis_port.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package output
2+
3+
type RedisPort interface {
4+
Get(key string) (string, error)
5+
Set(key string, data string) error
6+
Remove(key string) error
7+
}

config/api/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type AppConfig struct {
2323
Cockroach common.Cockroach `json:"cockroach"`
2424
Mongo common.Mongo `json:"mongo"`
2525
Pulsar common.Pulsar `json:"pulsar"`
26+
Redis common.Redis `json:"redis"`
2627
}
2728

2829
// Load config file from given path

config/event_handler/config-dev.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ cockroach:
66
mongo:
77
dbName: urlShortener
88
uri: mongodb://localhost:27017
9+
redis:
10+
address: localhost
11+
addressPort: 6379
912
pulsar:
1013
url: pulsar://localhost:6650
1114
topic: events

config/event_handler/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type AppConfig struct {
2323
Cockroach common.Cockroach `json:"cockroach" `
2424
Kafka common.Kafka `json:"kafka"`
2525
Pulsar common.Pulsar `json:"pulsar"`
26+
Redis common.Redis `json:"redis"`
2627
}
2728

2829
// Load config file from given path

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
2929
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3030
github.com/danieljoos/wincred v1.1.2 // indirect
31+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
3132
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
3233
github.com/fsnotify/fsnotify v1.6.0 // indirect
3334
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
@@ -62,6 +63,7 @@ require (
6263
github.com/prometheus/client_model v0.3.0 // indirect
6364
github.com/prometheus/common v0.39.0 // indirect
6465
github.com/prometheus/procfs v0.9.0 // indirect
66+
github.com/redis/go-redis/v9 v9.0.2 // indirect
6567
github.com/rivo/uniseg v0.2.0 // indirect
6668
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
6769
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnG
100100
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
101101
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
102102
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
103+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
104+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
103105
github.com/dimfeld/httptreemux v5.0.1+incompatible h1:Qj3gVcDNoOthBAqftuD596rm4wg/adLLz5xh5CmpiCA=
104106
github.com/dimfeld/httptreemux v5.0.1+incompatible/go.mod h1:rbUlSV+CCpv/SuqUTP/8Bk2O3LyUV436/yaRGkhP6Z0=
105107
github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQxaLAeM=
@@ -393,6 +395,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
393395
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
394396
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
395397
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
398+
github.com/redis/go-redis/v9 v9.0.2 h1:BA426Zqe/7r56kCcvxYLWe1mkaz71LKF77GwgFzSxfE=
399+
github.com/redis/go-redis/v9 v9.0.2/go.mod h1:/xDTe9EF1LM61hek62Poq2nzQSGj0xSrEtEHbBQevps=
396400
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
397401
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
398402
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=

0 commit comments

Comments
 (0)