Skip to content

Commit

Permalink
fix: oubsub working for distributed calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
mati007thm committed Dec 7, 2023
1 parent e8dbeaf commit cb9a07f
Show file tree
Hide file tree
Showing 14 changed files with 829 additions and 192 deletions.
1 change: 1 addition & 0 deletions dapr-distributed-calendar/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ FROM node:17-alpine
WORKDIR /app
COPY . .
RUN npm install
# RUN npm install cloudevents
EXPOSE 3000
CMD [ "node", "node_controller.js" ]
2 changes: 1 addition & 1 deletion dapr-distributed-calendar/components/pubsub.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ spec:
- name: redisHost
value: redis:6379
- name: redisPassword
value: ""
value: "Pa55w.rd"
2 changes: 1 addition & 1 deletion dapr-distributed-calendar/components/statestore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ spec:
- name: redisHost
value: redis:6379
- name: redisPassword
value: ""
value: "Pa55w.rd"
- name: actorStateStore
value: "true"
18 changes: 8 additions & 10 deletions dapr-distributed-calendar/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ services:
build: ./python
ports:
- "5000"
- "3501"
depends_on:
- redis
- placement
networks:
- dapr-callendar-network
environment:
- FLASK_RUN_PORT=5000
pythonapp-dapr:
image: "daprio/daprd:edge"
command: ["./daprd",
"-app-id", "messages",
"-app-port", "5000",
"-dapr-http-port", "3501",
"-placement-host-address", "placement:50006",
"-components-path", "/components"]
volumes:
Expand All @@ -59,8 +63,7 @@ services:
build: ./go
ports:
- "6000"
deploy:
replicas: 2
- "3503"
environment:
- DAPR_HTTP_PORT=3503
depends_on:
Expand Down Expand Up @@ -94,20 +97,15 @@ services:
############################
# Redis state store
############################
# redis:
# image: "redis:alpine"
# ports:
# - "6380:6379"
# networks:
# - dapr-callendar-network
redis:
image: docker.io/bitnami/redis:7.2
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
# - ALLOW_EMPTY_PASSWORD=yes
- REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
- REDIS_PASSWORD=Pa55w.rd
ports:
- '6379:6379'
- '6379'
networks:
- dapr-callendar-network
volumes:
Expand Down
Binary file modified dapr-distributed-calendar/go/go_events
Binary file not shown.
18 changes: 15 additions & 3 deletions dapr-distributed-calendar/go/go_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func addEvent(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&event)
if err != nil {
log.Printf("Error while decoding: %e", err)
return
}
log.Printf("Event Name: %s", event.Name)
log.Printf("Event Date: %s", event.Date)
Expand All @@ -47,7 +48,7 @@ func addEvent(w http.ResponseWriter, r *http.Request) {
resp, err := http.Post(stateURL, "application/json", bytes.NewBuffer(state))
if err != nil {
log.Fatalln("Error posting to state", err)
http.Error(w, "Failed to write to store", http.StatusServiceUnavailable)
return
}
log.Printf("Response after posting to state: %s", resp.Status)
http.Error(w, "All Okay", http.StatusOK)
Expand All @@ -70,6 +71,7 @@ func deleteEvent(w http.ResponseWriter, r *http.Request) {
resp, err := client.Do(req)
if err != nil {
log.Fatalln("Error deleting event", err)
return
}
log.Printf("Response after delete call: %s", resp.Status)

Expand All @@ -81,25 +83,35 @@ func deleteEvent(w http.ResponseWriter, r *http.Request) {

func getEvent(w http.ResponseWriter, r *http.Request) {
type Identity struct {
ID string
ID string `json:"id"`
}
var eventID Identity

err := json.NewDecoder(r.Body).Decode(&eventID)
if err != nil {
log.Printf("Error decoding id")
return
}
getURL := stateURL + "/" + eventID.ID
req, err := http.NewRequest(http.MethodGet, getURL, nil)
if err != nil {
log.Fatalln("Error creating get request", err)
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalln("Error getting event", err)
return
}
log.Printf("Response after get call: %s", resp.Status)

defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading response body: %v", err)
return
}

log.Printf(string(bodyBytes))
}
Expand Down
18 changes: 17 additions & 1 deletion dapr-distributed-calendar/node_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const bodyParser = require('body-parser');
require('isomorphic-fetch');

const app = express();
// const { HTTP } = require("cloudevents");
app.use(bodyParser.json());

const daprPort = process.env.DAPR_HTTP_PORT || 3500;
Expand All @@ -17,6 +18,22 @@ const publishUrl = `http://localhost:${daprPort}/v1.0/publish/${pubsub_name}/${t

const port = 3000;

// app.get('/dapr/subscribe', (_req, res) => {
// res.json([
// {
// pubsubname: "pubsub",
// topic: "events-topic",
// route: "getmsg"
// }
// ]);
// });

// app.post('/getmsg', (req, res) => {
// const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
// console.log(receivedEvent);
// res.sendStatus(200);
// });

function send_notif(data) {
var message = {
"data": {
Expand Down Expand Up @@ -110,7 +127,6 @@ app.get('/event/:id', (req, res) =>{
if (!response.ok) {
throw "Failed to get state.";
}

console.log("Successfully got state.");
res.status(200).send();
}).catch((error) => {
Expand Down
1 change: 1 addition & 0 deletions dapr-distributed-calendar/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cb9a07f

Please sign in to comment.