Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
marcel-dempers committed Nov 12, 2021
1 parent f48c3bd commit 77c038d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 48 deletions.
24 changes: 12 additions & 12 deletions golang/introduction/part-5.database.redis/dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
FROM golang:1.15-alpine as dev

WORKDIR /work

FROM golang:1.15-alpine as build

WORKDIR /videos
COPY ./videos/* /videos/
RUN go build -o videos

FROM alpine as runtime
COPY --from=build /videos/videos /
FROM golang:1.15-alpine as dev
WORKDIR /work
FROM golang:1.15-alpine as build
WORKDIR /videos
COPY ./videos/* /videos/
RUN go build -o videos
FROM alpine as runtime
COPY --from=build /videos/videos /
CMD ./videos
6 changes: 3 additions & 3 deletions golang/introduction/part-5.database.redis/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
http.HandleFunc("/", HandleGetVideos)
http.HandleFunc("/update", HandleUpdateVideos)
http.ListenAndServe(":8080", nil)
http.ListenAndServe(":80", nil)
}
```

Expand Down Expand Up @@ -314,7 +314,7 @@ If we look at our sentinel configuration, our master alias is set to `mymaster`
```
docker run -it -p 80:80 `
--net redis `
-e REDIS_SENTINELS="sentinel-1:5000,sentinel-2:5000,sentinel-3:5000" `
-e REDIS_SENTINELS="sentinel-0:5000,sentinel-1:5000,sentinel-2:5000" `
-e REDIS_MASTER_NAME="mymaster" `
-e REDIS_PASSWORD="a-very-complex-password-here" `
-v ${PWD}:/work go sh
Expand Down Expand Up @@ -550,7 +550,7 @@ Run :
```
docker run -it -p 80:80 `
--net redis `
-e REDIS_SENTINELS="sentinel-1:5000,sentinel-2:5000,sentinel-3:5000" `
-e REDIS_SENTINELS="sentinel-0:5000,sentinel-1:5000,sentinel-2:5000" `
-e REDIS_MASTER_NAME="mymaster" `
-e REDIS_PASSWORD="a-very-complex-password-here" `
videos
Expand Down
39 changes: 19 additions & 20 deletions golang/introduction/part-5.database.redis/videos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@ import (
"net/http"
"encoding/json"
"io/ioutil"
"github.com/go-redis/redis/v8"
"context"
"strings"
"fmt"
"os"
"context"
"strings"
"github.com/go-redis/redis/v8"
)

var redis_sentinels = os.Getenv("REDIS_SENTINELS")
var redis_master = os.Getenv("REDIS_MASTER_NAME")
var redis_password = os.Getenv("REDIS_PASSWORD")
var ctx = context.Background()
var ctx = context.Background()
var redisClient *redis.Client

func main() {

var redis_sentinels = os.Getenv("REDIS_SENTINELS")
var redis_master = os.Getenv("REDIS_MASTER_NAME")
var redis_password = os.Getenv("REDIS_PASSWORD")

sentinelAddrs := strings.Split(redis_sentinels, ",")

rdb := redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: redis_master,
SentinelAddrs: sentinelAddrs,
MasterName: redis_master,
SentinelAddrs: sentinelAddrs,
Password: redis_password,
DB: 0,
})

redisClient = rdb

rdb.Ping(ctx)

http.HandleFunc("/", HandleGetVideos)
Expand All @@ -36,16 +39,14 @@ func main() {
http.ListenAndServe(":80", nil)
}


func HandleGetVideos(w http.ResponseWriter, r *http.Request){

id, ok := r.URL.Query()["id"]

if ok {
videoID := id[0]
video := getVideo(videoID)

videoID := id[0]
video := getVideo(videoID)

if video.Id == "" { //video not found, or empty ID
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{}"))
Expand All @@ -56,9 +57,8 @@ func HandleGetVideos(w http.ResponseWriter, r *http.Request){
if err != nil {
panic(err)
}

w.Write(videoBytes)
return
return

}

Expand All @@ -82,8 +82,8 @@ func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){
}

_, ok := r.URL.Query()["id"]

if ok {

var video video
err = json.Unmarshal(body, &video)
if err != nil {
Expand All @@ -95,7 +95,7 @@ func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){
return

}

var videos []video
err = json.Unmarshal(body, &videos)
if err != nil {
Expand All @@ -110,5 +110,4 @@ func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){
w.WriteHeader(405)
fmt.Fprintf(w, "Method not Supported!")
}
}

}
24 changes: 11 additions & 13 deletions golang/introduction/part-5.database.redis/videos/videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,24 @@ func getVideos()(videos []video){
video := getVideo(key)
videos = append(videos, video)
}

return videos
}

func getVideo(id string)(video video) {

value, err := redisClient.Get(ctx, id).Result()

if err == redis.Nil {
return video
}

if err != nil {
panic(err)
}

json.Unmarshal([]byte(value), &video)
if err != redis.Nil {
err = json.Unmarshal([]byte(value), &video)
}

return video
}

func saveVideos(videos []video)(){
for _, video := range videos {
saveVideo(video)
}
}

func saveVideo(video video)(){

videoBytes, err := json.Marshal(video)
Expand All @@ -60,7 +51,14 @@ func saveVideo(video video)(){
}

err = redisClient.Set(ctx, video.Id, videoBytes, 0).Err()
if err != nil {
if err != nil {
panic(err)
}

}

func saveVideos(videos []video)(){
for _, video := range videos {
saveVideo(video)
}
}

0 comments on commit 77c038d

Please sign in to comment.