Skip to content

Commit 494cc36

Browse files
committed
Added redis
1 parent baae5a4 commit 494cc36

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Diff for: main.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ package main
33
import (
44
"net/http"
55
"github.com/gorilla/mux"
6+
"github.com/go-redis/redis"
67
"html/template"
78
)
89

10+
var client *redis.Client
911
var templates *template.Template
1012

1113
func main() {
14+
client = redis.NewClient(&redis.Options{
15+
Addr: "localhost:6379",
16+
})
1217
templates = template.Must(template.ParseGlob("templates/*.html"))
1318
r := mux.NewRouter()
1419
r.HandleFunc("/", indexHandler).Methods("GET")
@@ -17,5 +22,9 @@ func main() {
1722
}
1823

1924
func indexHandler(w http.ResponseWriter, r *http.Request) {
20-
templates.ExecuteTemplate(w, "index.html", nil)
25+
comments, err := client.LRange("comments", 0, 10).Result()
26+
if err != nil {
27+
return
28+
}
29+
templates.ExecuteTemplate(w, "index.html", comments)
2130
}

Diff for: templates/index.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<html>
22
<head>
3-
<title>Super awesome amazing page</title>
3+
<title>Comments</title>
44
</head>
55
<body>
6-
<h1>Some basic HTML code</h1>
6+
<h1>Comments</h1>
7+
{{ range . }}
8+
<div>{{ . }}</div>
9+
{{ end }}
710
</body>
811
</html>

0 commit comments

Comments
 (0)