This commit is contained in:
marcel-dempers 2021-11-12 22:15:01 +11:00
parent f48c3bd323
commit 77c038d44d
4 changed files with 46 additions and 49 deletions

View File

@ -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

View File

@ -85,7 +85,7 @@ func main() {
http.HandleFunc("/", HandleGetVideos)
http.HandleFunc("/update", HandleUpdateVideos)
http.ListenAndServe(":8080", nil)
http.ListenAndServe(":80", nil)
}
```
@ -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
@ -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

View File

@ -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)
@ -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)
if ok {
videoID := id[0]
video := getVideo(videoID)
if video.Id == "" { //video not found, or empty ID
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{}"))
@ -56,9 +57,8 @@ func HandleGetVideos(w http.ResponseWriter, r *http.Request){
if err != nil {
panic(err)
}
w.Write(videoBytes)
return
return
}
@ -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 {
@ -95,7 +95,7 @@ func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){
return
}
var videos []video
err = json.Unmarshal(body, &videos)
if err != nil {
@ -110,5 +110,4 @@ func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){
w.WriteHeader(405)
fmt.Fprintf(w, "Method not Supported!")
}
}
}

View File

@ -25,7 +25,6 @@ func getVideos()(videos []video){
video := getVideo(key)
videos = append(videos, video)
}
return videos
}
@ -33,25 +32,17 @@ 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)
@ -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)
}
}