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. // updateRedis updates Redis with a new Rabbit message.
func updateRedis(d amqp.Delivery, c *redis.Client) bool { 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 { if _, err := c.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.LPush(ctx, "messages", d.Body) pipe.LPush(ctx, "messages", d.Body)
pipe.LTrim(ctx, "messages", 0, 9) pipe.LTrim(ctx, "messages", 0, 9)
pipe.Incr(ctx, "count") pipe.Incr(ctx, "total")
return nil return nil
}); err != nil { }); err != nil {
log.Fatalf("update redis: %s", err) log.Fatalf("update redis: %s", err)

View File

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

View File

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

View File

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

View File

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