1
0
mirror of https://github.com/ebosas/microservices.git synced 2025-08-24 20:08:55 +02:00

Messages page, cache

This commit is contained in:
ebosas
2021-10-08 21:40:10 +03:00
parent 89536de358
commit 5357502b71
5 changed files with 23 additions and 31 deletions

4
cmd/cache/cache.go vendored
View File

@@ -47,11 +47,11 @@ func main() {
// updateRedis updates Redis with a new Rabbit message.
func updateRedis(d amqp.Delivery, c *redis.Client) bool {
// Add a message, limit to 10 in cache, +1 total count.
// Add a message, limit to 10 in cache, increment total count.
if _, err := c.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.LPush(ctx, "messages", d.Body)
pipe.LTrim(ctx, "messages", 0, 9)
pipe.Incr(ctx, "count")
pipe.Incr(ctx, "total")
return nil
}); err != nil {
log.Fatalf("update redis: %s", err)

View File

@@ -3,7 +3,6 @@ package main
import (
"context"
"embed"
"encoding/json"
"fmt"
"html/template"
"log"
@@ -73,32 +72,18 @@ func handleHome(w http.ResponseWriter, r *http.Request) {
// handleMessages handles the messages page.
func handleMessages(cr *redis.Client) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
cacheJSON, err := cache.GetCacheJSON(cr)
cacheData, cacheJSON, err := cache.GetCache(cr)
if err != nil {
log.Printf("get cache: %s", err)
return
}
var cached cache.Cache
err = json.Unmarshal([]byte(cacheJSON), &cached)
if err != nil {
log.Printf("unmarshal cache: %s", err)
return
}
data := map[string]interface{}{
"Data": cached,
"Data": cacheData,
"Json": cacheJSON,
}
// data := struct {
// Data cache.Cache
// Json string
// }{cached, "Hello"}
// fmt.Println(data.Json)
funcMap := template.FuncMap{"fdate": formatTime}
funcMap := template.FuncMap{"ftime": formatTime}
t := template.Must(template.New("").Funcs(funcMap).ParseFS(filesTempl, "template/template.html", "template/navbar.html", "template/messages.html"))
t.ExecuteTemplate(w, "layout", data)
}

View File

@@ -12,7 +12,7 @@
<tbody>
{{ range .Data.Messages }}
<tr>
<td>{{ .Time | fdate }}</td>
<td>{{ .Time | ftime }}</td>
<td>{{ .Text }}</td>
<td>{{ .Source }}</td>
</tr>

View File

@@ -17,21 +17,21 @@ type Cache struct {
Messages []models.Message `json:"messages"`
}
// GetCache gets cached messages from Redis by
// calling GetCacheJSON and unmarshalling the returned JSON.
func GetCache(c *redis.Client) (*Cache, error) {
// GetCache gets cached messages from Redis,
// returns both a Cache struct and a JSON string.
func GetCache(c *redis.Client) (*Cache, string, error) {
cacheJSON, err := GetCacheJSON(c)
if err != nil {
return &Cache{}, fmt.Errorf("get cache json: %v", err)
return &Cache{}, "", fmt.Errorf("get cache json: %v", err)
}
var cache Cache
err = json.Unmarshal([]byte(cacheJSON), &cache)
if err != nil {
return &Cache{}, fmt.Errorf("unmarshal cache: %v", err)
return &Cache{}, "", fmt.Errorf("unmarshal cache: %v", err)
}
return &cache, nil
return &cache, cacheJSON, nil
}
// GetCacheJSON reads cached messages from Redis, returns JSON.
@@ -41,8 +41,10 @@ func GetCacheJSON(c *redis.Client) (string, error) {
return "", fmt.Errorf("lrange redis: %v", err)
}
total, err := c.Get(context.Background(), "count").Result()
if err != nil {
total, err := c.Get(context.Background(), "total").Result()
if err == redis.Nil {
total = "0"
} else if err != nil {
return "", fmt.Errorf("get redis: %v", err)
}

View File

@@ -55,6 +55,11 @@ function Messages() {
<td>{msg.source}</td>
</tr>
))}
{!messages.length &&
<tr>
<td colSpan="3">No messages</td>
</tr>
}
</tbody>
</table>
</div>