You've already forked microservices
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:
4
cmd/cache/cache.go
vendored
4
cmd/cache/cache.go
vendored
@@ -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)
|
||||||
|
@@ -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)
|
||||||
}
|
}
|
||||||
|
@@ -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>
|
||||||
|
18
internal/cache/cache.go
vendored
18
internal/cache/cache.go
vendored
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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>
|
||||||
|
Reference in New Issue
Block a user