-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.go
85 lines (72 loc) · 1.57 KB
/
html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package server
import (
"context"
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"github.com/comfyprog/allnews/feed"
"github.com/gin-gonic/gin"
)
//go:embed public
var public embed.FS
var frontendFs fs.FS
var staticFs fs.FS
func init() {
var err error
frontendFs, err = fs.Sub(public, "public")
if err != nil {
log.Fatal(err)
}
staticFs, err = fs.Sub(frontendFs, "static")
if err != nil {
log.Fatal(err)
}
}
func handleIndexPage() gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"Url": c.Request.URL.Path,
"Title": "Main page",
})
}
}
func handleAboutPage() gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "about.html", gin.H{
"Url": c.Request.URL.Path,
"Title": "About",
})
}
}
func handleSearchPage() gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "search.html", gin.H{
"Url": c.Request.URL.Path,
"Title": "Search",
})
}
}
type StatsGetter interface {
GetArticleStats(context.Context) ([]feed.ArticleStats, error)
}
func handleStatsPage(db StatsGetter, tags map[string][]string) gin.HandlerFunc {
return func(c *gin.Context) {
stats, err := db.GetArticleStats(c.Request.Context())
if err != nil {
c.HTML(http.StatusInternalServerError, "error.html", gin.H{
"Url": c.Request.URL.Path,
"Title": "Stats",
"Error": fmt.Sprintf("Error happened: %v", err),
})
return
}
c.HTML(http.StatusOK, "stats.html", gin.H{
"Url": c.Request.URL.Path,
"Title": "Stats",
"Resources": stats,
"Tags": tags,
})
}
}