forked from getfider/fider
/
common.go
164 lines (144 loc) · 3.89 KB
/
common.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package handlers
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/web"
)
//Health always returns OK
func Health() web.HandlerFunc {
return func(c web.Context) error {
err := c.Engine().Database().Ping()
if err != nil {
return c.Failure(err)
}
return c.Ok(web.Map{})
}
}
//LegalPage returns a legal page with content from a file
func LegalPage(title, file string) web.HandlerFunc {
return func(c web.Context) error {
bytes, err := ioutil.ReadFile(env.Etc(file))
if err != nil {
return c.NotFound()
}
return c.Render(http.StatusOK, "legal.html", web.Props{
Title: title,
Data: web.Map{
"Content": string(bytes),
},
})
}
}
//Sitemap returns the sitemap.xml of current site
func Sitemap() web.HandlerFunc {
return func(c web.Context) error {
if c.Tenant().IsPrivate {
return c.NotFound()
}
posts, err := c.Services().Posts.GetAll()
if err != nil {
return c.Failure(err)
}
baseURL := c.BaseURL()
text := strings.Builder{}
text.WriteString(`<?xml version="1.0" encoding="UTF-8"?>`)
text.WriteString(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`)
text.WriteString(fmt.Sprintf("<url> <loc>%s</loc> </url>", baseURL))
for _, post := range posts {
text.WriteString(fmt.Sprintf("<url> <loc>%s/posts/%d/%s</loc> </url>", baseURL, post.Number, post.Slug))
}
text.WriteString(`</urlset>`)
c.Response.Header().Del("Content-Security-Policy")
return c.XML(http.StatusOK, text.String())
}
}
//RobotsTXT return content of robots.txt file
func RobotsTXT() web.HandlerFunc {
return func(c web.Context) error {
bytes, err := ioutil.ReadFile(env.Path("./robots.txt"))
if err != nil {
return c.NotFound()
}
sitemapURL := c.BaseURL() + "/sitemap.xml"
content := fmt.Sprintf("%s\nSitemap: %s", bytes, sitemapURL)
return c.String(http.StatusOK, content)
}
}
//Page returns a page without properties
func Page(title, description, chunkName string) web.HandlerFunc {
return func(c web.Context) error {
return c.Page(web.Props{
Title: title,
Description: description,
ChunkName: chunkName,
})
}
}
//BrowserNotSupported returns an error page for browser that Fider dosn't support
func BrowserNotSupported() web.HandlerFunc {
return func(c web.Context) error {
return c.Render(http.StatusOK, "browser-not-supported.html", web.Props{
Title: "Browser not supported",
Description: "We don't support this version of your browser",
})
}
}
//NewLogError is the input model for UI errors
type NewLogError struct {
Message string `json:"message"`
Data interface{} `json:"data"`
}
//LogError logs an error coming from the UI
func LogError() web.HandlerFunc {
return func(c web.Context) error {
input := new(NewLogError)
err := c.Bind(input)
if err != nil {
return c.Failure(err)
}
c.Logger().Errorf(input.Message, log.Props{
"Data": input.Data,
})
return c.Ok(web.Map{})
}
}
func validateKey(kind models.EmailVerificationKind, c web.Context) (*models.EmailVerification, error) {
key := c.QueryParam("k")
//If key has been used, return NotFound
result, err := c.Services().Tenants.FindVerificationByKey(kind, key)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
return nil, c.NotFound()
}
return nil, c.Failure(err)
}
//If key has been used, return Gone
if result.VerifiedAt != nil {
return nil, c.Gone()
}
//If key expired, return Gone
if time.Now().After(result.ExpiresAt) {
err = c.Services().Tenants.SetKeyAsVerified(key)
if err != nil {
return nil, c.Failure(err)
}
return nil, c.Gone()
}
return result, nil
}
func between(n, min, max int) int {
if n > max {
return max
} else if n < min {
return min
}
return n
}