1
0
mirror of https://github.com/ebosas/microservices.git synced 2025-11-29 21:57:32 +02:00

Build messages page, cache API

This commit is contained in:
ebosas
2021-10-07 16:23:34 +03:00
parent ed3ce70b37
commit 866aff9f75
9 changed files with 213 additions and 41 deletions

27
cmd/server/api.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import (
"fmt"
"log"
"net/http"
"github.com/ebosas/microservices/internal/cache"
"github.com/go-redis/redis/v8"
)
// handleAPICache handles an API call for cached messages
func handleAPICache(cr *redis.Client) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
data, err := cache.GetCacheJSON(cr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Something happened"))
log.Printf("get cache json: %s", err)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, data)
}
}