diff --git a/golang/introduction/part-5.database.redis/dockerfile b/golang/introduction/part-5.database.redis/dockerfile new file mode 100644 index 0000000..283161d --- /dev/null +++ b/golang/introduction/part-5.database.redis/dockerfile @@ -0,0 +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 / +CMD ./videos \ No newline at end of file diff --git a/golang/introduction/part-5.database.redis/readme.md b/golang/introduction/part-5.database.redis/readme.md new file mode 100644 index 0000000..7c47134 --- /dev/null +++ b/golang/introduction/part-5.database.redis/readme.md @@ -0,0 +1,467 @@ +# Introduction to Go: Storing data in Redis Database + +Up until now, we've learned the fundamentals of Go and built a small web microservice that handles our video data. +Our service has a `/` `GET` endpoint for returning all videos, as well as a simple `/update` endpoint for updating our list of videos. + +We've learnt how to read and write from files and learn how to work with `json` data.
+This is important for learning Go, however there are a few challenges for using a file as storage.
+ +* It can be problematic if we have more than one instance of our service writing to the same file +* It's important to keep state out of our application, so if our application crashes, we don't lose our data + +[In part 1](../readme.md), we covered the fundamentals of writing basic Go
+[In part 2](../part-2.json/readme.md), we've learnt how to use basic data structures like `json` so we can send\receive data.
+[In part 3](../part-3.http/readme.md), we've learnt how to write a HTTP service to expose our videos data
+ + +## Go Dev Environment + +The same as Part 1+2+3, we start with a [dockerfile](./dockerfile) where we declare our version of `go`. + +The `dockerfile`: + +``` +FROM golang:1.15-alpine as dev + +WORKDIR /work +``` + +Let's build and start our container: + +``` +cd golang\introduction\part-5.database.redis + +docker build --target dev . -t go +docker run -it -p 80:80 -v ${PWD}:/work go sh +go version +``` + +## Our application + +Create a new directory that holds defines our `repository` and holds our `module` + +``` +mkdir videos + +``` + +* Define a module path + +``` +# change directory to your application source code + +cd videos + +# create a go module file + +go mod init videos + +``` + +## Create our base code + +We start out all our applications with a `main.go` defining our `package`, declaring our `import` dependencies
+and our entrypoint `main()` function + +``` +package main + +import ( + "net/http" + "encoding/json" + "io/ioutil" + "fmt" +) + +func main() { + + http.HandleFunc("/", HandleGetVideos) + http.HandleFunc("/update", HandleUpdateVideos) + + http.ListenAndServe(":8080", nil) +} +``` + +Now before we write these handler functions, we need our videos application + +## Create our Videos app + +Firstly, we create a separate code file `videos.go` that deals with our YouTube videos
+ +The `videos.go` file defines what a video `struct` looks like, a `getVideos()` function to retrieve
+videos list as a slice and a `saveVideos()` function to save videos to a file locally.
+ +We start with our dependencies.
+We import 2 packages, 1 for reading and writing files, and another for dealing with `json` + +``` +package main + +import ( + "io/ioutil" + "encoding/json" +) + +``` + +Then we define what a video `struct` looks like: + +``` +type video struct { + Id string + Title string + Description string + Imageurl string + Url string +} +``` + +We have a function for retrieving `video` objects as a list of type `slice` : + +``` +func getVideos()(videos []video){ + + fileBytes, err := ioutil.ReadFile("./videos.json") + + if err != nil { + panic(err) + } + + err = json.Unmarshal(fileBytes, &videos) + + if err != nil { + panic(err) + } + + return videos +} + +``` + +We also need to copy our `videos.json` file which contains our video data.
+ +And finally, we have a function that accepts a list of type `slice` and stores the videos to a local file + +``` +func saveVideos(videos []video)(){ + + videoBytes, err := json.Marshal(videos) + if err != nil { + panic(err) + } + + err = ioutil.WriteFile("./videos.json", videoBytes, 0644) + if err != nil { + panic(err) + } + +} +``` + +## HTTP Handlers + +Now we have to define our handler functions `HandleGetVideos` and `HandleUpdateVideos`, + +``` +func HandleGetVideos(w http.ResponseWriter, r *http.Request){ + + videos := getVideos() + videoBytes, err := json.Marshal(videos) + + if err != nil { + panic(err) + } + + w.Write(videoBytes) +} + +func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){ + + if r.Method == "POST" { + + body, err := ioutil.ReadAll(r.Body) + if err != nil { + panic(err) + } + + var videos []video + err = json.Unmarshal(body, &videos) + if err != nil { + w.WriteHeader(400) + fmt.Fprintf(w, "Bad request") + } + + saveVideos(videos) + + } else { + w.WriteHeader(405) + fmt.Fprintf(w, "Method not Supported!") + } +} + +``` + +Now so far, we have a web HTTP application that can list and update our youtube videos.
+Let's build and run it + +``` +go build + +./videos + +``` + +If we head over to `http://localhost` in the browser we can see our application returns our 2 videos.
. +We can use tools like PostMan to generate a `POST` request to update our videos too. + + +## Redis Go Package + +Now instead of reading and writing our records to a `json` file, we are going to read and write records to Redis. + +https://github.com/go-redis/redis/ + +``` +go get github.com/go-redis/redis/v8 + +``` + +And to use the library, we have to import it + +``` +import ( + "github.com/go-redis/redis/v8" +) +``` + +Now to connect to any database, you're going to need a bit of information: + +* Database Address +* Database Port +* Database Username\Password + +We can define these as environment variables and read those in our code + +``` +import ( + "os" +) +//main.go (global variables) + +var redis_sentinels = os.Getenv("REDIS_SENTINELS") +var redis_master = os.Getenv("REDIS_MASTER_NAME") +var redis_password = os.Getenv("REDIS_PASSWORD") + +``` + +We define an empty context and Redis client +Context can be used to control timeouts and deadlines for our application. We can set up an empty context for now. + +``` +import ( + "context" +) + +var ctx = context.Background() +var redisClient *redis.Client + +``` + +## Redis Sentinel Client + +https://redis.uptrace.dev/guide/sentinel.html#redis-server-client + + +``` +import ( + "strings" +) + +sentinelAddrs := strings.Split(redis_sentinels, ",") +rdb := redis.NewFailoverClient(&redis.FailoverOptions{ + MasterName: redis_master, + SentinelAddrs: sentinelAddrs, + Password: redis_password, + DB: 0, +}) + +redisClient = rdb + +rdb.Ping(ctx) +``` + +We can also add the `Ping()` to our handler functions to ensure it can connect using the global redis client variable + +## Networking + +Now we need to remember our go container may not be able to talk to the redis containers because they are running on different networks. + +If you took note, we started our Redis containers on a `redis` network by passing `--net redis` as a flag to our `docker run` commands.
+ +Let's restart our Go container on the same network + +We also need to set our `ENV` variables to point our container to the redis sentinels.
+ +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_MASTER_NAME="mymaster" ` + -e REDIS_PASSWORD="a-very-complex-password-here" ` + -v ${PWD}:/work go sh +``` + +We can now observe our container is connected to Redis.
+Our application: [http://localhost](http://localhost) + +# Store our Data + +So now we can store our video data in Redis instead of a local `json` file.
+We'll write the `json` document of a video (struct) to Redis.
+ +Let's create a `saveVideo()` function that stores a single record. + +``` +func saveVideo(video video)(){ + + videoBytes, err := json.Marshal(video) + if err != nil { + panic(err) + } + + err = redisClient.Set(ctx, video.Id, videoBytes, 0).Err() + if err != nil { + panic(err) + } +} +``` + +Now since we have one endpoint that saves all videos, we need to update it to save each video it has. + +``` +func saveVideos(videos []video)(){ + for _, video := range videos { + saveVideo(video) + } +} +``` + +To get the videos, let's create a function to retrieve a single record: + +``` +func getVideo(id string)(video video) { + + value, err := redisClient.Get(ctx, id).Result() + + if err != nil { + panic(err) + } + + if err != redis.Nil { + err = json.Unmarshal([]byte(value), &video) + } + + return video +} + +``` + +And finally we need to update our `GetVideos()` function to loop all the video keys and return all videos + +``` +func getVideos()(videos []video){ + + keys, err := redisClient.Keys(ctx,"*").Result() + + if err != nil { + panic(err) + } + + for _, key := range keys { + video := getVideo(key) + videos = append(videos, video) + } + + return videos +} + +``` + +And we need all our imports + +``` +import ( + "encoding/json" + "github.com/go-redis/redis/v8" +) +``` + +Now if we rebuild our all and access it, we get `null` as there are no videos in Redis. Let's add two! + +Let's `POST` the following using PostMan to url `http://localhost/update` +``` +[ + { + "id" : "QThadS3Soig", + "title" : "Kubernetes on Amazon", + "imageurl" : "https://i.ytimg.com/vi/QThadS3Soig/sddefault.jpg", + "url" : "https://youtu.be/QThadS3Soig", + "description" : "TEST" + }, + { + "id" : "eyvLwK5C2dw", + "title" : "Kubernetes on Azure", + "imageurl" : "https://i.ytimg.com/vi/eyvLwK5C2dw/mqdefault.jpg?sqp=CISC_PoF&rs=AOn4CLDo7kizrJozB0pxBhxL9JbyiW_EPw", + "url" : "https://youtu.be/eyvLwK5C2dw", + "description" : "TEST" + } +] + +``` + +If we refresh our page, we can now see two records! + +# Improvements + +Now that you have the fundamental knowledge of HTTP and Redis,
+you can update the code to retrieve 1 video by ID, or delete a video by ID.
+You can add search functionality and more! + +# Build our Docker container + +Full `dockerfile` : + +``` +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 + +``` + +Build : +``` +cd golang\introduction\part-5.database.redis +docker build . -t videos +``` + +Run : +``` +docker run -it -p 80:80 ` + --net redis ` + -e REDIS_SENTINELS="sentinel-1:5000,sentinel-2:5000,sentinel-3:5000" ` + -e REDIS_MASTER_NAME="mymaster" ` + -e REDIS_PASSWORD="a-very-complex-password-here" ` + videos +``` diff --git a/golang/introduction/part-5.database.redis/videos/go.mod b/golang/introduction/part-5.database.redis/videos/go.mod new file mode 100644 index 0000000..be78151 --- /dev/null +++ b/golang/introduction/part-5.database.redis/videos/go.mod @@ -0,0 +1,5 @@ +module videos + +go 1.15 + +require github.com/go-redis/redis/v8 v8.11.4 diff --git a/golang/introduction/part-5.database.redis/videos/go.sum b/golang/introduction/part-5.database.redis/videos/go.sum new file mode 100644 index 0000000..d8b13c3 --- /dev/null +++ b/golang/introduction/part-5.database.redis/videos/go.sum @@ -0,0 +1,89 @@ +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg= +github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/golang/introduction/part-5.database.redis/videos/main.go b/golang/introduction/part-5.database.redis/videos/main.go new file mode 100644 index 0000000..4fe4b4f --- /dev/null +++ b/golang/introduction/part-5.database.redis/videos/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "net/http" + "encoding/json" + "io/ioutil" + "github.com/go-redis/redis/v8" + "context" + "strings" + "fmt" + "os" +) + +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 redisClient *redis.Client + +func main() { + + sentinelAddrs := strings.Split(redis_sentinels, ",") + rdb := redis.NewFailoverClient(&redis.FailoverOptions{ + MasterName: redis_master, + SentinelAddrs: sentinelAddrs, + Password: redis_password, + DB: 0, + }) + + redisClient = rdb + rdb.Ping(ctx) + + http.HandleFunc("/", HandleGetVideos) + http.HandleFunc("/update", HandleUpdateVideos) + + http.ListenAndServe(":80", nil) +} + + +func HandleGetVideos(w http.ResponseWriter, r *http.Request){ + + redisClient.Ping(ctx) + videos := getVideos() + videoBytes, err := json.Marshal(videos) + + if err != nil { + panic(err) + } + + w.Write(videoBytes) +} + +func HandleUpdateVideos(w http.ResponseWriter, r *http.Request){ + + if r.Method == "POST" { + + body, err := ioutil.ReadAll(r.Body) + if err != nil { + panic(err) + } + + var videos []video + err = json.Unmarshal(body, &videos) + if err != nil { + w.WriteHeader(400) + fmt.Fprintf(w, "Bad request") + } + + saveVideos(videos) + + } else { + w.WriteHeader(405) + fmt.Fprintf(w, "Method not Supported!") + } +} + diff --git a/golang/introduction/part-5.database.redis/videos/videos.go b/golang/introduction/part-5.database.redis/videos/videos.go new file mode 100644 index 0000000..0b149f5 --- /dev/null +++ b/golang/introduction/part-5.database.redis/videos/videos.go @@ -0,0 +1,64 @@ +package main + +import ( + "encoding/json" + "github.com/go-redis/redis/v8" +) + +type video struct { + Id string + Title string + Description string + Imageurl string + Url string +} + +func getVideos()(videos []video){ + + keys, err := redisClient.Keys(ctx,"*").Result() + + if err != nil { + panic(err) + } + + for _, key := range keys { + video := getVideo(key) + videos = append(videos, video) + } + + return videos +} + +func getVideo(id string)(video video) { + + value, err := redisClient.Get(ctx, id).Result() + + if err != nil { + panic(err) + } + + 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) + if err != nil { + panic(err) + } + + err = redisClient.Set(ctx, video.Id, videoBytes, 0).Err() + if err != nil { + panic(err) + } +} \ No newline at end of file