reprogl

Форк
0
/
static.go 
147 строк · 3.4 Кб
1
package handlers
2

3
import (
4
	"fmt"
5
	"net/http"
6
	"os"
7
	"time"
8

9
	"xelbot.com/reprogl/container"
10
	"xelbot.com/reprogl/models/repositories"
11
	"xelbot.com/reprogl/views"
12
)
13

14
func InfoAction(app *container.Application) http.HandlerFunc {
15
	return func(w http.ResponseWriter, r *http.Request) {
16
		templateData := views.NewInfoPageData()
17
		templateData.SetCanonical(container.GenerateAbsoluteURL("info-page"))
18
		err := views.WriteTemplateWithContext(r.Context(), w, "about.gohtml", templateData)
19
		if err != nil {
20
			app.ServerError(w, err)
21

22
			return
23
		}
24
	}
25
}
26

27
func StatisticsAction(app *container.Application) http.HandlerFunc {
28
	return func(w http.ResponseWriter, r *http.Request) {
29
		repo := repositories.CommentRepository{DB: app.DB}
30
		commentators, err := repo.GetMostActiveCommentators()
31
		if err != nil {
32
			app.ServerError(w, err)
33

34
			return
35
		}
36

37
		articleRepo := repositories.ArticleRepository{DB: app.DB}
38
		monthArticles, err := articleRepo.GetMostVisitedArticlesOfMonth()
39
		if err != nil {
40
			app.ServerError(w, err)
41

42
			return
43
		}
44

45
		allTimeArticles, err := articleRepo.GetMostVisitedArticles()
46
		if err != nil {
47
			app.ServerError(w, err)
48

49
			return
50
		}
51

52
		templateData := views.NewStatisticsPageData()
53
		templateData.Commentators = commentators
54
		templateData.MonthArticles = monthArticles
55
		templateData.AllTimeArticles = allTimeArticles
56
		templateData.SetCanonical(container.GenerateAbsoluteURL("statistics"))
57

58
		cacheControl(w, container.StatisticsTTL)
59
		err = views.WriteTemplate(w, "statistics.gohtml", templateData)
60
		if err != nil {
61
			app.ServerError(w, err)
62

63
			return
64
		}
65
	}
66
}
67

68
func RobotsTXTAction(w http.ResponseWriter, r *http.Request) {
69
	var body string
70

71
	if container.IsCDN(r) {
72
		body = "User-agent: *\n\nDisallow: /\n"
73
	} else {
74
		cfg := container.GetConfig()
75
		body = fmt.Sprintf(
76
			"User-agent: *\n\nSitemap: https://%s/sitemap.xml\n",
77
			cfg.Host)
78
	}
79

80
	cacheControl(w, container.RobotsTxtTTL)
81
	w.Header().Set("Content-Type", "text/plain")
82
	w.Write([]byte(body))
83
}
84

85
func HumansTXTAction(w http.ResponseWriter, r *http.Request) {
86
	var lastUpdateStr string
87
	lastUpdate, err := time.Parse(time.RFC3339, container.BuildTime)
88
	if err != nil {
89
		lastUpdateStr = container.BuildTime
90
	} else {
91
		lastUpdateStr = lastUpdate.Format("2006/01/02")
92
	}
93

94
	cfg := container.GetConfig()
95

96
	body := fmt.Sprintf(
97
		`/* TEAM */
98
	Developer: %s
99
	Contact: %s
100
	Telegram: @morontt
101
	From: %s
102

103
/* THANKS */
104
	Inspirer: Alex Edwards
105
	Site: https://www.alexedwards.net/
106
	From: Austria
107

108
	pxThemes: Anima Multipurpose Ghost Theme
109
	Site: https://themeforest.net/user/pxthemes
110
	Location: Poland
111

112
	Colorlib: Free 404 Error Page Templates
113
	Site: https://colorlib.com/wp/free-404-error-page-templates/
114
	Location: Latvia
115

116
	8biticon: Pixel Character Maker
117
	Site: https://8biticon.com/
118

119
/* SITE */
120
	Last update: %s
121
	Language: Russian
122
	Doctype: HTML5
123
	IDE: GoLand, VS Code, nano
124
	Server: NGINX + Varnish + Golang custom application
125
	Powered by: Go %s`,
126
		cfg.Author.FullName,
127
		cfg.Author.Email,
128
		cfg.AuthorLocationEn,
129
		lastUpdateStr,
130
		container.GoVersionNumbers,
131
	)
132

133
	cacheControl(w, container.StatisticsTTL)
134
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
135
	w.Write([]byte(body + "\n"))
136
}
137

138
func FavIconAction(w http.ResponseWriter, _ *http.Request) {
139
	body, err := os.ReadFile("./public/favicon.ico")
140
	if err != nil {
141
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
142
	}
143

144
	cacheControl(w, container.RobotsTxtTTL)
145
	w.Header().Set("Content-Type", "image/x-icon")
146
	w.Write(body)
147
}
148

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.